C calls function methods in an unmanaged dynamic library

  • 2020-12-21 18:08:53
  • OfStack

How can C# call a function in an unmanaged dynamic library, such as one written in VC6? In short, C# calls the dynamic library much more quickly and easily than Java calls the DLL dynamic library. Here is an example of the process.

1. Create an unmanaged dynamic library

The code is as follows:


   // this 1 Declare dynamic library output 1 A function prototype that can be uncalled . 
   extern   "C"  __declspec(dllexport)  int  add( int ,  int );     int  add( int  a, int  b)  
   { 
        // Implement this function returna+b; 
   }

Note the above code, 1 must add extern"C", cannot generate dynamic library export function name will not be add, like ? add @@YAHHH @Z looks like, after just using the function name add to locate the function entry will be a problem.

Save as C or CPP, then use the command cl (provided by VC6) to compile and generate a dynamic library. The command is as follows:


C:\>cl /LD MyLib.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8804 for 80x86
Copyright (C) Microsoft Corp 1984-1998. All rights reserved. MyLib.cpp
Microsoft (R) Incremental Linker Version 6.00.8447
Copyright (C) Microsoft Corp 1992-1998. All rights reserved. /out:MyLib.dll
/dll
/implib:MyLib.lib
MyLib.obj
Creating library MyLib.lib and object MyLib.exp

You can see that MyLib.Dll is generated under the C root directory, along with MyLib.lib, ES34en.obj, MyLib.exp files are generated. The above command cl parameter /LD is generated dynamic library file

2, write C# program to call the dynamic library


   using  System; 
   using  System.Runtime.InteropServices; // This is used DllImport Time to introduce the package  
   public   class  InvokeDll{         [DllImport( "MyLib.dll" , CharSet=CharSet.Auto)] 
       staticexternint add( int  a, int  b); // Declare external standard dynamic libraries , with Win32API is 1 sample .          public   static   void  Main() 
       { 
         Console.WriteLine(add(10,30)); 
       } 
   }

Save as InvokeDll.cs file in the same 1 directory as MyLib.dll and compile the file.

C:\>csc InvokeDll.cs
Microsoft (R) Visual C# .NET Compiler version 7.10.3052.4
Used for Microsoft (R) .NET Framework version 1.1.4322
All rights reserved (C) Microsoft Corporation 2001-2002 . All rights reserved.
Will be generated Invokedll.exe, This file can be executed . C:\>InvokeDll
40

We see that C# calls the function add, which is an unmanaged dynamic library. Ensure that ES55en.dll is on a path that can be loaded by the InvokeDll program before execution.

In return, if we did not add extern"C" in ES60en.cpp, then C cannot locate the export method by the function name add (because the function name has changed in the dynamic library), the following error occurs when invokeDll is executed.


C:\>InvokeDll Unhandled exceptions: System.EntryPointNotFoundException: Can't in DLL MyLib.dll Found in add The entry point.
at InvokeDll.add(Int32 a, Int32 b)
at InvokeDll.Main()

For dynamic libraries that are not prototype generated with extern "C", we have to call them in a different way, which I don't know yet. One more question. The above example just shows how simple a function can be in a dynamic library. What if the function passes a pointer to an argument, or a more complex data type? We'll get into that later.

[Note:] This paper refers to an article on the Internet: ES74en-ES75en calls the standard dynamic library, but the operation directly based on the original text is unable to locate the error of the entry point of add, so it is slightly modified.


Related articles: