Interoperability C++/C#

Joined
May 5, 2009
Messages
1
Reaction score
0
I have a function in UNMANAGED C++ which creates a bitmap and passes the raw bitmap data back as a char* (bmpData) the method signature is:

Code:
 [/font]
 
[font=Arial]int GetImage(int x, int y, char* bmpData) [/font]
 
[font=Arial]


The return value (int) returns 0 or 1 to indicate whether the Bitmap creation was successful.

I need to call this method from MANAGED C#. Currently I am using the following code to do this:

Code:
 [/size]
[size=2]byte[] bmp = new byte[size];[/size]
[size=2]//size is calculated based on maximum size of image[/size]
[size=2]private static extern int GetImage(int x, int y, byte[] bmp); [/size]
 
//[size=2]I then pass the byte[] to a MemoryStream to get the Image[/size]
 
[size=2]


However this gives me an ‘Access Violation’. I think this is because of the way in which the bitmap data is marshaled from c++ to c#. Unfortunately I am unable to change the c++ (as it was not written by me) so the problem must be solved in the c#. Basically I need to know how to correctly Marshal the c++ char* to a byte array in c#. I hope you can help! Thanks.
 

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