I recently worked on a project that used vc's dll, but it needed to be called in .net. I studied how to call unmanaged dll in c#.
Let’s first introduce the dll file
1.Dll file
Dynamic link library (also called DLL, short for "Dynamic Link Library") is one of the most important components of Microsoft Windows. Open the Windows system folder and you will find that there are many DLL files in the folder. Windows is the Some major system functions are implemented in the form of DLL modules.
The dynamic link library cannot be executed directly, nor can it receive messages. It is just an independent file that contains functions (methods) that can be called by programs or other DLLs to complete certain operations. Note: Generally called "methods" in C#) , but these functions are not part of the execution program itself, but are loaded on demand according to the needs of the process, and can only play a role at this time.
The DLL is loaded into the virtual space of the process by the system only when the application needs it, and becomes part of the calling process. At this time, the DLL can only be accessed by the thread of the process, and its handle can be used by the calling process, and the calling process The process handle can also be used by the DLL. In the memory, a DLL has only one instance, and its preparation has nothing to do with the specific programming language and compiler, so mixed language programming can be achieved through DLL. Any objects (including variables) created by code within a DLL function are owned by the thread or process that calls it.
2. Calling DLL
Each programming language has different methods of calling DLL. Here we only introduce the method of calling DLL using C#. First, you need to understand what is managed and what is unmanaged. Generally speaking, it can be thought that unmanaged code is mainly DLL and activeX components developed based on the win 32 platform, while managed code is developed based on the .net platform.
(1) General method of calling unmanaged functions in DLL
First, the external method should be declared in the C# language source program. Its basic form is:
[DLLImport("DLL file")]
Modifier extern returns variable type method name (parameter list)
in:
DLL file: contains a library file that defines external methods.
Modifiers: Access modifiers, modifiers other than abstract that can be used when declaring methods.
Return variable type: The return variable type of the method you need to call in the DLL file.
Method name: The name of the method you need to call in the DLL file.
Parameter list: The list of methods you need to call in the DLL file.
Note: The System.Runtime.InteropServices namespace needs to be used in the program declaration.
DllImport can only be placed on method declarations.
The DLL file must be located in the current directory of the program or the query path defined by the system (ie: the path set by Path in the system environment variable).
The return variable type, method name, and parameter list must be consistent with the definitions in the DLL file.
If you want to use other function names, you can use the EntryPoint property setting, such as:
[DllImport("user32.dll", EntryPoint="MessageBoxA")]
static extern int MsgBox(int hWnd, string msg, string caption, int type);
Other optional DllImportAttribute attributes:
CharSet indicates the character set used in the entry point, such as: CharSet=CharSet.Ansi;
SetLastError indicates whether the method retains the Win32 "last error", such as: SetLastError=true;
ExactSpelling indicates whether the EntryPoint must exactly match the spelling of the indicated entry point, such as: ExactSpelling=false;
PreserveSig indicates whether the signature of the method should be preserved or converted, such as: PreserveSig=true;
CallingConvention indicates the calling convention of the entry point, such as: CallingConvention=CallingConvention.Winapi;