Unmanaged code into C#

  • Thread starter Thread starter Saya
  • Start date Start date
S

Saya

Hi,
Please help with the following:
How to implement a C++ code like this:
MediaFunction( char* filename,
void* info,
int* size,
void** buffer,
int first,
int* last);

into a C# syntax? (via Delegate - how?)
MediaFunction is a callback function.
Please provide some explanatory code. Thank you!
 
Saya said:
How to implement a C++ code like this:
MediaFunction( char* filename,
void* info,
int* size,
void** buffer,
int first,
int* last);

into a C# syntax? (via Delegate - how?)
MediaFunction is a callback function.
Please provide some explanatory code. Thank you!

You havent given us enough info. Do you only want to translate the arguments?
The only tricky part is the buffer, you'll need to rethink that. It depends
on your use of hte function.


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Get your ASP.NET in gear with IntraWeb!
http://www.atozed.com/IntraWeb/
 
Saya,

If you want to transform this into a callback function, you have to
declare the delegate in the following manner (by the way, this assumes a
return type of void, since you didn't specify one):

public delegate void MediaFunction(
[MarshalAs(UnmanagedType.LPStr)] string filename,
IntPtr info,
ref int size,
ref IntPtr buffer,
int first,
ref int last);

Once you have that, you will have to marshal the info and buffer
arguments yourself.

Hope this helps.
 
Nicholas, GREAT to have helping people like you around! (Kudzu, also
thanks for responding).

I've now made a 'giant' leap in my C# code (I'm new to it!!): the
callback is now invoked properly and data read from the file is also put
correctly into the specified buffer.

I'm still struggling though ...: the code-line calling the unmanaged
stuff ultimately ends up in an exception error:
"System.NullReferenceException: Object reference not set to an instance
of an object". Now this is heavy stuff for me ....!
I'm not sure you are still willing to help me, but in case of a yes, I
attached the relevant code hereafter.
My C#-app is using a third party legacy-DLL, one of the functions is a
ordinary OpenFile command:
------------------------------------------------------------int MC_Open_File (
int AppID,
int FileID,
void* info,
int (*YourCallbackFunction)
[DllImport("mc3adv.dll")]
public static extern int MC_Open_File(
int AppID,
int FileID,
CBinfo cbi,
MediaToFileDelegate MediaFunction);
typedef struct CALLBACKINFO
{
FILE* stream;
int messageID;
unsigned char buffer[8*1024];
} CBinfo;
[StructLayout(LayoutKind.Sequential)]
public class CBinfo
{
public int stream;
public int messageID;
public byte[] buffer;
}
CBinfo cbi = new CBinfo();
cbi.buffer = new byte[8*1024];

try
{
mcStatus=MC_Open_File(
nAppID,
nFileID,
cbi,
new MediaFunctionDelegate(MediaFunction));
}time.
 
Saya,

I think it is the declaration of the callback info structure that is
giving you a problem. It should be:

[StructLayout(LayoutKind.Sequential)]
public class CBinfo
{
public IntPtr stream;
public int messageID;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=8192)]
public byte[] buffer;
}

This tells the marshaler to include the array as part of the structure,
instead of treating it as a pointer. Also, when passing pointers across the
managed/unmanaged boundary, you should use IntPtr to represent the native
pointer size on the machine that is running the code.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Saya said:
Nicholas, GREAT to have helping people like you around! (Kudzu, also
thanks for responding).

I've now made a 'giant' leap in my C# code (I'm new to it!!): the
callback is now invoked properly and data read from the file is also put
correctly into the specified buffer.

I'm still struggling though ...: the code-line calling the unmanaged
stuff ultimately ends up in an exception error:
"System.NullReferenceException: Object reference not set to an instance
of an object". Now this is heavy stuff for me ....!
I'm not sure you are still willing to help me, but in case of a yes, I
attached the relevant code hereafter.
My C#-app is using a third party legacy-DLL, one of the functions is a
ordinary OpenFile command:
------------------------------------------------------------int MC_Open_File (
int AppID,
int FileID,
void* info,
int (*YourCallbackFunction)
[DllImport("mc3adv.dll")]
public static extern int MC_Open_File(
int AppID,
int FileID,
CBinfo cbi,
MediaToFileDelegate MediaFunction);
typedef struct CALLBACKINFO
{
FILE* stream;
int messageID;
unsigned char buffer[8*1024];
} CBinfo;
[StructLayout(LayoutKind.Sequential)]
public class CBinfo
{
public int stream;
public int messageID;
public byte[] buffer;
}
CBinfo cbi = new CBinfo();
cbi.buffer = new byte[8*1024];

try
{
mcStatus=MC_Open_File(
nAppID,
nFileID,
cbi,
new MediaFunctionDelegate(MediaFunction));
}time.
 
Nicholas,
Again, thanks for the instructions - It did structurize things in the
right way for me although I'm still confronted with the annoying
NullReferenceException error!
The issue now is (I think ..) how to reach the buffer (fourth argument
in MediaFunction); like Kudzu pointed out, it is indeed a tricky part!
(void** buffer --> ref IntPtr buffer).

I'm now juggling in MSDN to get the translation correctly from 'void**'
into the 'Marshal.PtrToStructure' thing:

What I need: a code construction involving the MediaFunction callback:
public int MediaFunction(
string filename,
IntPtr info,
ref int size,
ref IntPtr buffer, <-------
int first,
ref int last);

I have to attach the incoming 'buffer' IntPtr to the CBinfo struct,
something like:
CBinfo cbi = Marshal.PtrToStructure(buffer, typeof(CBinfo));

Result of this line is a <undefined value> for cbi, at debug time ...
..
;-((
Regards.
 
Back
Top