Reputation: 2093
If I define a new ref class
in C++/CLI, can I then use this type as a standard unmanaged class, and if so, does the ref
keyword alter the memory layout of the datatype at all?
In my case, there is a class which is used all over the place in a very very large C++ application. We'll call it MyExampleThing
:
class MyExampleThing
{
int a = 0;
int b = 1;
}
We're looking at the possibility of slowly integrating more .net framework components into this application through C++/CLI. If I port this class to a ref class
would it (under normal circumstances) continue to work in other plain old unmanaged C++ code? Or would I have to leave it as is and write a ref class
wrapper to handle .net interop?
To be clear: I know I can't just pass an unmanaged pointer into managed code, I'm just asking if I can use the class separately as either managed or unmanaged.
Is the same true for value class
?
Also important: If I can create ref class
es on the unmanaged heap, is the memory layout the same as if it were a regular C++ class? (The unmanaged versions of this class get serialized byte for byte as binary data. Yes, I'm aware of endianness issues of doing it that way, no I didn't make that decision.)
Upvotes: 0
Views: 528
Reputation: 4535
It is possible to write a mixed mode DLL for both managed and native clients. Take a look here http://msdn.microsoft.com/en-us/magazine/cc300632.aspx.
Personally I have always ended up writing a wrapper in C++/CLI to expose C++ classes to C#.
Upvotes: 0
Reputation: 16197
ref
is not a keyword I'm familiar with so I'm going to go out on a limb and say it's probably not supported by unmanaged C++
If you have reusable classes for a library I would just make them in unmanaged C++ which can easily be imported and used by managed code and keep your .NET components and managed code separated. Prefer decoupled classes and interfaces over objects that do a little bit of everything whenever you can.
You can also make wrappers which marshal between managed and unmanaged code when you do need to have one side communicate with the other. My experience is limited to having a C# application calling into a C++ library I experimented with CLI at one point but didn't find it particularly useful myself.
Upvotes: 1