unmanaged .dll easiest way to use it  
Author Message
abcdefgqwerty2





PostPosted: Visual C++ Language, unmanaged .dll easiest way to use it Top

I have .net 2005 and I have a video library in c++ with 3 unmanaged .dlls and .libs. I dont want to waste a month writing a wrapper for 60 functions to import them into c# they are full of function pointers and callbacks. I am still a little confused on what types of applications are managed and what are unmanaged. Is a win32 app unmanaged by default I would rather use a c++ project where it is unmanaged so I can just include and use the .dlls. I also get the error message saying the .dlls are not valid files when trying to import them into c++ win form managed application. I linked in the .libs but how do I add the .dll to the project
Thanks


Visual C++7  
 
 
Alex Farber





PostPosted: Visual C++ Language, unmanaged .dll easiest way to use it Top

Create C++/CLI class library and add these libraries to it by C++ way - all .lib files to the Linker dependencies. Write C++/CLI class which is used as wrapper for unmanaged functions. In this class call unmanaged functions directly. C++/CLI allows to call unmanaged code directly from managed code. Use this C++/CLI class in C# client.
 
 
abcdefgqwerty2





PostPosted: Visual C++ Language, unmanaged .dll easiest way to use it Top

Ah ok I see. Do I # include the .h files For each unmanaged function do I just call it and put it inside my wrapper function I dont need to do any interop stuff

 
 
Simple Samples





PostPosted: Visual C++ Language, unmanaged .dll easiest way to use it Top

Perhaps it would help to understand how things work using the traditional way before managed code and such. It might help to read the documentation in the Platrorm SDK.

Note that most of the Platform SDK are functions in DLLs. To call a function in other DLLs, a header is often used that prototypes the function and imports it. Then a library (.lib file) is used by the linker to obtain the information the linker needs. The compiler and the linker don't need to know anything about the DLL; the library file provides everything needed. The DLL, including the actual location, is only needed during execution. When linking, the linker needs two peices of information that is usually provided separately; the name of the library and the directory it is in. That information can be provided in various ways but I won't get into that; that is explained very well elsewhere.



 
 
Alex Farber





PostPosted: Visual C++ Language, unmanaged .dll easiest way to use it Top

Yes, you need to include unmanaged SDK files to C++/CLI project. Any unmanaged SDK can be used by C++/CLI client exactly by the same way as by unmanaged C++ client.
You need interop stuff in the case if you need to translate some unmanaged types to managed. For example, unmanaged function returns char*, and you need to convert it to String^ for C# client:

String^ GetName()
{
    char* name = GetNameFunction();   // let's say that GetNameFunction is unmanaged function
    return gcnew String(name);    // String class has constructor which accepts char*
}

Managed primitive types like int, char etc. are mapped directly to unmanaged types. For example, if unmanaged function returns int, you can return it directly as .NET int to C# client.
You need interop also to convert managed types passed by C# client to unmanaged for calling native functions.


 
 
abcdefgqwerty2





PostPosted: Visual C++ Language, unmanaged .dll easiest way to use it Top

Ok I see. There are a lot of functions I would have to convert. I think ill just make the whole thing in an unmanaged win32 app. Ill really miss the .net object libraries but doing it unmanaged is a lot easier and faster then converting the 3rd party api to managed code.