Hi All,
I have a C++ DLL which basically calls a legacy C library which
decrypts some data.
I have defined the DLL as below.
extern "C" __declspec(dllexport) void __stdcall init()
{
init_decoder();
return;
}
extern "C" __declspec(dllexport) short * __stdcall decode(unsigned
char serial[10])
{
short * decoded_value = new short[80];
decoder(serial, decoded_value); //This is where I call my legacy
library
return decoded_value;
}
I define these in my C# code as
[DllImport("decoder.dll")]
private static extern char init();
[DllImport("decoder.dll")]
private static extern char decode(????);
public Test()
{
init();
byte [] DataToDecode = {57,100,182,27,75,192,103,99,56};
byte [] DecodedData;
//Want to pass the byte array to the decode function and have it
pass back the decoded data
//Pseudo code
DecodedData = decode(DataToDecode); ///???? How do I do this
}
I can call init() successfully so I know the DLL is working OK. I just
need to know how to call the decode part of it. What I need to do is
pass in a 10 character byte array and have the decoded data which
would be an array of shorts of length 80. If anybody can show me how
to do this I would be very grateful.
Many thanks,
Mattias Sjögren - 02 Jun 2005 13:44 GMT
>extern "C" __declspec(dllexport) short * __stdcall decode(unsigned
>char serial[10])
[quoted text clipped - 5 lines]
>
> return decoded_value;
I hope your library exposes a function to delete the decoded_value
buffer, or you'll leak memory.
>[DllImport("decoder.dll")]
>private static extern char init();
The return type should be void, just like in the native declaration.
>[DllImport("decoder.dll")]
>private static extern char decode(????);
Try
private static extern IntPtr decode(byte[] serial);
Use Marshal.Copy to dereference the returned pointer to a short[].
Mattias

Signature
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Erik - 02 Jun 2005 19:32 GMT
Hi Mattias,
Thanks very much. Did everything you said and it all works perfectly!!
Many thanks
>>extern "C" __declspec(dllexport) short * __stdcall decode(unsigned
>>char serial[10])
[quoted text clipped - 24 lines]
>
>Mattias