C source code to DLL for use in a C# application
I have a C source code written in VC++6.0 and I need that code to be used in a C# application which I am c开发者_Go百科oding.
I have done few things to make that happen, I have run that C source file in release mode and have generated the intermediate.manifest file and the other files there, can I use any of these file to make my problem solved.
What you want to do is import the dll like this:
[DllImport("user32.dll")]//This is where you identify your dll...
public static extern int MessageBoxA( //This would be an object from your dll
int h, string m, string c, int type);
public static int Main()
{
return MessageBoxA(0, "Hello World!", "My Message Box", 0);
}
The keywords that you need to search on to learn about this are "running unmanaged code" and "PInvoke".
Another alternative is to wrap the C code in managed C++. Microsoft calls it C++/CLI. Anyways, you then compile an 'assembly' from that wrapper. An assembly is just a DLL that contains managed code. Then you can 'reference' (the managed equivalent of linking) that 'wrapper' from a fully managed C# application. So the dependency could look like this:
C -> Managed C++ -> C#
If you are comfortable enough wrapping up c code in c++ you could easily make a dll by following this microsoft guide. It details how to go about creating a dll in C++. If you need a more in-depth explanation though feel free to get back to me, and I will try and knock up a quick how-to.
Also let me know if you use a different IDE or environment and I can taylor the answer to suit your needs more precisely.
Might be later on today though as am pressed for time at the moment.
精彩评论