// JP opened flex table

Click to See Complete Forum and Search --> : Marshalling problem (managed to unmanaged)


s196675m
March 3rd, 2008, 01:15 PM
HOPEFULLY someone can help me.

I am trying to access a function from an unmanaged DLL from my visual c++ managed GUI.


Declaration of the function I am trying to import from unmanaged (native) DLL is:

extern "C" __declspec(dllexport) TGrayImage<int>* init_seg(TGrayImage<float>* pImg, TMonoImage* monoImg, int nClass)



Even though there are many functons in the DLL, only this function is exported.


Now In my GUI, I have added the following line to import that functon so that I can call that functon from GUI.

At the top of the file after using namespaces


[System::Runtime::InteropServices::DllImport("segment.dll")]
TGrayImage<int> * init_seg(TGrayImage<float>* pimg, TMonoImage* mono, int nNum);


I didn't do any Marshalling parameter. I have no idea how to Marshall a pointer to a Template class or pointer to a class. I searched before posting but I didn't get clear understanding about it.

Just for clarification, pimg and mono I am trying to pass is Managed varialble created within managed GUI.
TGrayImage<float>* pimg = new TGrayImage<float>* (width,height);// used new not gcnew
TmonoImage* mono = new TMonoImage(width, height);

I have added the proper library (compiled with pure:MSIL) in my my GUI which supplied TGrayImage,TMonoImage.

Header file for TGrayImage and TMonoImage already included in the GUI.


When I call init_seg function from GUI, I got the following message:
An unhandled exception of type 'System.AccessViolationException' occurred in GUI.exe
Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.


Please help me to Marshall these parameter correctway. I will appreciate your big help.
Thank you.

darwen
March 3rd, 2008, 04:18 PM
You don't need to use DllImport. Use the ordinary cli project setting (just /clr not /clr: pure or /clr:safe - just the ordinary one) and you can then call the method directly as if writing native C++.

This is one of the main reasons why to use C++/CLI rather than C# - the fact you can write native and managed code side by side and it will work.

Oh and as to


I searched before posting but I didn't get clear understanding about it.


The answer about the marshalling is - you can't marshal templated classes into managed code. There is no such thing as C++ templating in .NET so no way of marshalling them into verifyable .NET code. Generics are NOT templates.

Darwen.

s196675m
March 3rd, 2008, 05:11 PM
Hi
Thank you for your suggession.

I keep my native code to a seperate DLL . I did it for performance.


If I can successfully call this imported function from managed GUI then I my problem will be solve.

I just like to know three things:

(1) Did I imported the function correctly ?
(2) Do I need to marshall the parameters before passing to the function?
(3) How Can I marshall the first two parameters?

darwen
March 4th, 2008, 07:45 AM
Please read my post again.


you can't marshal templated classes into managed code


And as to


keep my native code to a seperate DLL


You're not keeping your native code seperate from managed because you're #including the .h files anyway. Which are NATIVE classes.

Darwen.

//JP added flex table