S
Stephen Cawood
I'm trying to use a C++ .lib from C# (I tried the Interop group will no
results).
I have a working wrapper DLL (I can get back simple things like int), but
I'm having issues dealing with an array of bytes.
For example, the .lib contains this function:
int create(int id, int scale, unsigned char *image);
In the wrapper DLL I have this function:
WIN32DLL_API int create_wrapped(int id, int scale, unsigned char *image)
{
return create(id, scale, image);
}
I also tried to return the unsigned char* like this:
WIN32DLL_API int create_wrapped_returnimage(int id, int scale, unsigned char
*image)
{
create(id, scale, image);
return image;
}
In C#, I've tried to access the functions like this:
[DllImport("Win32DLL.dll", EntryPoint="create_wrapped")]
public static extern int create_wrapped(int id, int scale,[In, Out] IntPtr
image);
[DllImport("Win32DLL.dll", EntryPoint="create_wrapped_returnimage")]
public static extern IntPtr create_wrapped_returnimage(int id, int
scale,[In] IntPtr image);
Then I try to manage the array like this:
IntPtr ptrArg = new IntPtr();
IntPtr unmanagedArray = create_wrapped_returnimage(0,5,ptrArg);
byte [] newArray = new byte[500];
Marshal.Copy(unmanagedArray, newArray, 0, 500);
I then checked what I have and get an error that this returns null:
label1.Text = unmanagedArray.GetType().ToString();
Eventually, I'll be converting the original byte array into an image, but
clearly I need to get this working first. I've tried various data types to
try and deal with the "unsigned char *image," but I haven't found a
solution.
Can someone suggest the best way to deal with this? Should I be converting
the unsigned char to a different type within the C++ wrapper function? If
so, some sample code would be helpful.
Thanks in advance.
results).
I have a working wrapper DLL (I can get back simple things like int), but
I'm having issues dealing with an array of bytes.
For example, the .lib contains this function:
int create(int id, int scale, unsigned char *image);
In the wrapper DLL I have this function:
WIN32DLL_API int create_wrapped(int id, int scale, unsigned char *image)
{
return create(id, scale, image);
}
I also tried to return the unsigned char* like this:
WIN32DLL_API int create_wrapped_returnimage(int id, int scale, unsigned char
*image)
{
create(id, scale, image);
return image;
}
In C#, I've tried to access the functions like this:
[DllImport("Win32DLL.dll", EntryPoint="create_wrapped")]
public static extern int create_wrapped(int id, int scale,[In, Out] IntPtr
image);
[DllImport("Win32DLL.dll", EntryPoint="create_wrapped_returnimage")]
public static extern IntPtr create_wrapped_returnimage(int id, int
scale,[In] IntPtr image);
Then I try to manage the array like this:
IntPtr ptrArg = new IntPtr();
IntPtr unmanagedArray = create_wrapped_returnimage(0,5,ptrArg);
byte [] newArray = new byte[500];
Marshal.Copy(unmanagedArray, newArray, 0, 500);
I then checked what I have and get an error that this returns null:
label1.Text = unmanagedArray.GetType().ToString();
Eventually, I'll be converting the original byte array into an image, but
clearly I need to get this working first. I've tried various data types to
try and deal with the "unsigned char *image," but I haven't found a
solution.
Can someone suggest the best way to deal with this? Should I be converting
the unsigned char to a different type within the C++ wrapper function? If
so, some sample code would be helpful.
Thanks in advance.