Wrapping C++ class w/MC++
-
I have seen examples of C++ unmanaged classes in extension DLLs, wrapped successfully with managed C++ classes, also in their own DLL, so that the managed classes can be called from C#. However, every example I have seen uses an unmanaged class that does not use any MFC. For example, I cannot find a single example (and have so far failed to create one myself) of an unmanaged C++ class that has some functions with CString arguments and/or return values, wrapped by a managed C++ class in a DLL, callable from C#. If anyone has such an example, I'd be forever grateful to receive it. When I try it, IJDW (it just doesn't work). I can get everything to compile file, but when I try to pass a string from the C# program down through the MC++ DLL to the unmanaged C++ DLL, I get an exception down there at the bottom saying that the object reference is not set to an instance of an object, even though I can see that the passed in string is just fine. Thanks for any assistance.
-
I have seen examples of C++ unmanaged classes in extension DLLs, wrapped successfully with managed C++ classes, also in their own DLL, so that the managed classes can be called from C#. However, every example I have seen uses an unmanaged class that does not use any MFC. For example, I cannot find a single example (and have so far failed to create one myself) of an unmanaged C++ class that has some functions with CString arguments and/or return values, wrapped by a managed C++ class in a DLL, callable from C#. If anyone has such an example, I'd be forever grateful to receive it. When I try it, IJDW (it just doesn't work). I can get everything to compile file, but when I try to pass a string from the C# program down through the MC++ DLL to the unmanaged C++ DLL, I get an exception down there at the bottom saying that the object reference is not set to an instance of an object, even though I can see that the passed in string is just fine. Thanks for any assistance.
Unfortunately, I don't use MFC in my coding projects, so my advice will be somewhat incomplete. We get this problem alot in Interop operations when passing data (often strings or structs) to native pre-.NET C++ functions in compiled DLLs. The basic problem is the way the Interop marshaler processes the data; the default behavior has limited applicability with user-defined data structures. This is particularly a problem in COM, as many of the data structures are defined as types that have no compatible type in .NET. The solution in .NET is to use attributes to control the marshaling behavior of the Interop when the default behavior is inappropriate, as in this case. I suggest that you review the MarshalAs(UnmanagedType) attribute documentation over at MSDN. In all honesty, I don't what the correct usage would be, but, hopefully you won't have to write a custom marshal routine (because it's such a pain in the ass). Try reading this: Deafult Behavior for Strings: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpcondefaultmarshalingforstrings.asp[^] ..or, this, the MarshalAs(UnmanagedType) Enumeration: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemruntimeinteropservicesunmanagedtypeclasstopic.asp[^]