Data type for marshaling BYTE*

B

bluewing

Hi, everybody!
I have to use Unmanaged Code DLL for my .Net Application.

This Dll has a few functions that returns BYTE* value.
but you know that C# does not have pointer type....
so, what data type should i take for marshaling BYTE* ?




One of the function is like this....

__declspec(dllimport) bool System_Write(int nSys, BYTE* pByte);


in this case, How can i define a function for this dll function?
 
B

bluewing

Oh, I made a mistake...

The unmanaged dll function I have to marshing is not that, but this...

__declspec(dllimport) BYTE* System_Read(int nSys);

I want to use this function in my dotnet application with C#
 
N

Nicholas Paldino [.NET/C# MVP]

bluewing,

You will have to declare the function as returning IntPtr, and then you
can use the static methods on the Marshal class to read the bytes from
unmanaged memory into a managed array.

The thing is, how will you know how many bytes to marshal? The
signature doesn't have anything on it to indicate the length of the array.

You also have the issue of how to deallocate the memory for the array.
Once you marshal the bytes over, you would have to call some other method,
passing the IntPtr to deallocate the memory block that was allocated upon
return.

Hope this helps.
 
B

bluewing

Thank you.

Would you mind if i ask you some example code, or reference?
None of my C# books talk about accessing array by point and dealing
with array
between C# and unmanaged dll.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top