简体   繁体   中英

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 coding.

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 ".

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.

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#

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM