C #. Net precautions for calling dynamic library DLL

Source: Internet
Author: User
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

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.