Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

interop question (c# c++)

Status
Not open for further replies.

yeller2

Programmer
Feb 8, 2007
69
US
I'm trying to write an image file to memory in c++, then pass the reference to my c# code so that I can then use the image. I'm having an issue writing to a data type that can then be passed back to c#.

Any ideas?
 
You may want to consider converting it to an array of bytes and finding a way in C# to turn that array into a stream and loading the image from the stream.

Can I ask why you are using C++ to open the file instead of C#?

 
I'm working with a jpeg2000 library that needs to be handled with c++.
 
Can you display sample code for how to convert the file to a byte array in c++ - my c++ is a little rusty.
 
You can use the IntPtr MarshalAs reference. I'm working on a similar issue at the moment but I'm not at the point of transfering it to the C# app. When I get there I can post my code if you still need it.

Using the IntPtr, you'll need to either have a null terminator or pass out the length of the image. The IntPtr will return a memory pointer to the beginning of the object. Then you just tell your app that the image memory begins at that point.

There's a bunch of examples running around on the net if you look for MarshalAs IntPtr
 
Here's where I'm at. I can call the c++ method (Convert) successfully. About this method: Convert is the c++ function, I pass it a byte [] that I've read a .jp2 image file into). In this method, using provided code, I'm able to create a bitmap file (I can successfully open it in windows). I seem to have read the bitmap into a char [], but getting info to c# enabling it to read the bitmap in memory appears problematic.

//Here's my c# code
IntPtr i1 = j2kstuff.Convert(b1, (int)fs1.Length); //c++ method
byte[] b2 = new byte[135190]; // I've gotten the array length from a .txt file that I write to from the c++
Marshal.Copy(i1, b2, 0, 135190);// copy intptr to buffer
MemoryStream ms1 = new MemoryStream(b2);//create memory stream from buffer
Bitmap b3 = new Bitmap(ms1); //Create bitmap from memorystreamI get an argumentexception from this line - with Troubleshooting tips: "Parameter is not valid".

Here's my c++:
fstream in("decoded.bmp");
ofstream out("length.txt");
string s1;

char c;
int i = 0;
while(in.get(c))
{
i++;
}
in.close();
fstream in2("decoded.bmp");
char buffer [33];
itoa (i,buffer,10); //put int into char []
char s2;
out.write(buffer, 33);
char *c1 = new char;
ostringstream ss(c1);
while(in2.get(c)) //read one character, puts one character
{
ss.put(c);
}
j2k_image.close();
return c1;

Any ideas? Why I'm getting the "Parameter is not valid" error?

Thanks
 
Hi,

This is kinda dumb suggestion but maybe try flushing first the ms1 data to a file, and open it again with Bitmap(string file) overload?

It seems the returned pointer is the decoded.bmp itself (in memory) unprocessed? I wonder if the image file you have can be actually loaded with the Bitmap class' default constructor...

my 2 cents
[wink]
 
The first thought that I have is that the Marshal.Copy's 4th parameter is a length. Try reducing your entry by 1 to see if that helps.

Next, I would put a breakpoint in the C# code on the line that you create the memory stream. Check to make sure that the byte[] (b2) is not null. If it's not, then there's a problem in transferring it over. If it is, add the SetLastError = true in your DllImport attribute and call the GetLastError to see what's going on.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top