C #. Net precautions for calling dynamic library DLL
This article mainly describes the technical difficulties and problems encountered by the author in his work, and finally proposes related solutions.
Scenario
The company has a business requirement involving ID card readers. Once an idcard reader clicks an idcard, the system automatically displays its ID card number, basic information of the idcard, and consumption records;
Problem
After purchasing the relevant ID Card Reader product, download its development kit, which contains the dynamic library DLL developed in C ++. The following problems occur during the call:
1. dll "our_idr.dll" cannot be loaded: the specified module cannot be found. (The exception is from hresult: 0x8007007e ).
2. Try to read or write the protected memory. This usually indicates that other memory is damaged.
Solution
1. The first problem is "unable to load DLL" our_idr.dll ": the specified module cannot be found. (The exception is from hresult: 0x8007007e ).", The solution is simple. The specific method is to copy the DLL to the application.ProgramIn the bin directory.
2,
I'm calling the followingCodeHour:
[Dllimport ( " Our_idr.dll " )]
Public Static Extern Byte [] Idr_read ( Byte [] Pserial );
The following error occurs: "try to read or write the protected memory. This usually indicates that other memory is damaged ".
The solution is to change byte [] to intptr, for example:
[Dllimport ( " Our_idr.dll " )]
Public Static Extern Intptr idr_read (intptr pserial );
When calling:
Intptr cardid = New Intptr ();
Intptr result = module. idr_read (cardid );
You can;
C # Pay attention to data type conversion when calling the C ++ dynamic library DLL
C # comparison with C ++ Data Types
C ++ C #
============================================
Word ushort
DWORD uint
Uchar int/byte can be replaced by INT in most cases. If strict alignment is required, bytebyte should be used.
Uchar * string/intptr
Unsigned char * [financialas (unmanagedtype. lparray)] Byte []/? (Intptr)
Char * string
Lpctstr string
Lptstr [financialas (unmanagedtype. lptstr)] string
Long int
Ulong uint
Handle intptr
Hwnd intptr
Void * intptr
Int int
Int * ref int
* Int intptr
Unsigned int uint
Colorref uint