This is the forum to discuss the Wrox book Professional C# 4.0 and .NET 4 by Christian Nagel, Bill Evjen, Jay Glynn, Karli Watson, Morgan Skinner; ISBN: 9780470502259
You are currently viewing the BOOK: Professional C# 4.0 and .NET 4 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
I have an unmanaged C++ DLL, I have the source to. I am trying to extend its functionality. I have successfully called existing methods in the DLL in a brute force way. I simply added the DLL as a project reference to my managed C# project and did a new on the desired class and called the methods.
As I have been studying up on this topic, I ran across the System.RunTime.Interop.Marshal set of static methods. Also, PInvoke. It is not clear to me when I should use one approach over another. I was wondering if someone here could tell me more about them. I am interested in knowing when should you use any of these approaches? when should you not?
My application is a managed wrapper around the unmanaged c++ DLL. The unmanaged DLL is not using COM or ATL. It is just plain C++. I am passing binary data. This data is transformed into another binary data using a complex algorithm and returned. Speed & multithreading is important, since it will be called by several threads.
As you've added the DLL as a project reference, created an instance and called the methods - are your sure not having a COM library?
Platform Invoke is used on unmanaged DLLs that export functions (see dumpbin /exports lib.dll).
The Marshal class has several helper methods for managed/unmanaged interop, both for DllImport and COM interop scenarios.
I'm not exactly sure how to tell if it uses COM or not. I know I didn't register the DLL or anything like that.
Within my managed C# project
1. I made a Project Reference to the unmanaged C++ project
2. Added a Using statement to that DLL
3. Then created an instance of the class
4. Accessed a method on the class
That is all I did. I did not use PInvoke, DllImport or anything like that.
There's also a way to use COM without registration. However, you referenced the unmanaged C++ project from the C# project? So you have the C++ project opened within the same solution? If you build the C++ project with Visual Studio, it might also do the registration of the COM server.
Can you do a "tblimp comserver.dll"? In that case a type library is included with the unmanaged C++ DLL and it's definitely a COM server. The type library could also be in a different file from the DLL.
You can use this C++ class like a .NET class because it is a .NET class. You're not using COM.
The ref keyword declares a managed reference type with C++/CLR. With the C++ compiler settings you will see the /clr option enabled. public ref class Foo is a managed reference type (class in C#), public value class Foo is a managed value type (struct in C#).
How do you see that you cannot make references to managed objects with the C++ project? It should be possible to reference .NET assemblies with the C++ project and use the public types there without problems. Just the syntax to use these types is a little different to normal C++.
With the Pro C# book you can find mapping between C++/CLR and C# with one of the downloadable chapters.