C++ interop /CLR confusion


A

ajtaylor

Hello,

I wonder if anyone can help lift the cloud of confusion hanging over me
at the moment ;-)

I have a load of unmanaged code in a project that I am compiling NOT
USING /CLR

I have some managed code and I want to call some of the functions in
the unmanaged code. So I create a file with mixed C++ and C++/CLI code

Now, the function signatures of the unamaged code contain
structures/classes that are declared in their own headers

//Unmanaged....
struct someData
{
int a;
double b;
};

struct bigstruct
{
//lots of data......
};

funcDoSomething(std::vector<somedata>& data, bigStruct* bigstruct)
{
.....
}

In the managed/mixed code I #include the appropriate headers and go
about constructing the data into the unmanged C++ structs [from managed
data types say from a Form] so that I can call the functions....

this will involve push_back calls onto a vector and newing in a struct
on the unmanaged HEAP.

What my question is - if I compile the code with /CLR - what actually
is going on behind the scenes - is the compiler turning the C++ code
into MSIL?? If so how does this fit in with using things like a call to
"new" and std::vector.

Do I have to pin the vector before passing it to the function??

Is it ok to just pass in the pointer to bigStruct??

Is there a performance hit from following this approach?

Is this indeed the correct approach to take?

Many thanks for any help you can offer me.
 
Ad

Advertisements

J

jiangsheng[MVP]

You can write managed wrapper classes for native classes and redirect all
method calls. The function calls are marshalled so you can call a native
function (with proper parameter and return value marshalling) from managed
code.
--
Regards
Sheng Jiang

Microsoft Most Valuable Professional in Visual C++
http://www.jiangsheng.net
http://blog.joycode.com/jiangsheng/
 
A

ajtaylor

jiangsheng said:
You can write managed wrapper classes for native classes and redirect all
method calls. The function calls are marshalled so you can call a native
function (with proper parameter and return value marshalling) from managed
code.
Many thanks for your reply.

The thing is all I really want to do is call the umanaged code - I dont
really want to create a managed wrapper for it as its not going to be
used by other managed classes so its not really worth the effort.

My enquiry is to what goes on "behind the scenes" if I construct
unmanaged C++ classes in a mixed code file compiled with /clr and call
unmanaged functions. In particular how I handle things on the unmanaged
heap and STL classes like vector.
 
Ad

Advertisements

J

jiangsheng[MVP]

compiled with /clr does not move your code to managed world automatically.
Only managed classes (ref class, interface class, etc) and functions after
a #pragra managed statement will be compiled into IL and visible to other
managed assemblies. So you need to write your interface functions (much like
DLL exports) and /or classes for managed assemblies.

--
Regards
Sheng Jiang

Microsoft Most Valuable Professional in Visual C++
http://www.jiangsheng.net
http://blog.joycode.com/jiangsheng/
 

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

Similar Threads

CLR and C ?? 5
C++ Interop 0
C++ Interop Memory 1
C++ interop help 4
managed c++ and native c++ interop 4
C++ interop & P/Invoke 1
C++ interop & reliability contracts 0
MC++ interop with C# error 0

Top