Summary of problems encountered when c # Calls c/c ++ dll,

Source: Internet
Author: User

Summary of problems encountered when c # Calls c/c ++ dll,

Some time ago, the company had a winform program and needed to call the dll of c to read the program of the card number. During this period, we encountered some problems. Let's share the following:

1. dll path Problems

I believe many developers will encounter this problem. I have summarized three ways to solve this problem;

1. Put it directly under the bin, in the same folder as the exe file,

Ps: When debugging the code, if it is in debug mode, put it in bin/debug mode, and put it in bin/debug mode in the same way. If this method is not feasible, try the second method.

2. Place it in C: \ Windows \ System32;

3. If none of the above methods works, you can only write physical paths. Example

[System.Runtime.InteropServices.DllImportAttribute(@"E:\Source\GPTagReaderDll", EntryPoint = "OpenPort", CallingConvention = CallingConvention.Cdecl)]        [return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]        public static extern bool OpenPort(string pPortname, uint dBaud);

This method is only suitable for calling one dll. If the called dll depends on other dll, this method will not work. This method also has limitations. if released, you have to ask someone else's machine to have this file under this physical path, but this is too unrealistic.

In addition to the three methods, I have also heard of writing them to environment variables. However, I have not tried this method and I will not mention it here.

Ii. type conversion problems

The following is the structure of c:

typedef struct{    unsigned char DeviceIdentify[30];//Greenpow Usb IC card reader    unsigned short res; }FindDeviceAck_Struct;

We need to convert it to the structure of c:

    [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential,        CharSet = System.Runtime.InteropServices.CharSet.Ansi,Pack =1)]    public struct FindDeviceAck_Struct    {        /// unsigned char[30]        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr,            SizeConst = 30)]        public string DeviceIdentify;        /// unsigned short        public ushort res;    }

For how c # Corresponds to c/c ++, see http://www.cnblogs.com/ausoldier/archive/2007/12/07/986141.html;

Here, DeviceIdentify is received using String, but garbled characters appear, and then changed

    [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential,        CharSet = System.Runtime.InteropServices.CharSet.Ansi,Pack =1)]    public struct FindDeviceAck_Struct    {        /// unsigned char[30]        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValArray,            SizeConst = 30)]        public byte[] DeviceIdentify;               /// unsigned short        public ushort res;    }

String corresponds to ByValTStr, byte [] corresponds to ByValArray, sizeconst refers to the size, pack = 1 indicates to be aligned by 1 byte

3. Call methods in c

The method in c is BOOL FindDevice (FindDeviceAck_Struct & finddeviceack );

At the beginning, I wrote this directly,

        [System.Runtime.InteropServices.DllImportAttribute("GPTagReaderDll", EntryPoint = "FindDevice", CallingConvention = CallingConvention.Cdecl)]        public static extern bool FindDevice(FindDeviceAck_Struct finddeviceack);

When calling

 FindDeviceAck_Struct findDeviceAck=new FindDeviceAck_Struct();               var result= NativeMethods.FindDevice(findDeviceAck);

The following error is reported: Try to read or write a protected program. It usually indicates that other memory already exists.

My colleague reminded me later that BOOL FindDevice (FindDeviceAck_Struct & finddeviceack) parameter & finddeviceack has '&'.

Changed

        [System.Runtime.InteropServices.DllImportAttribute("GPTagReaderDll", EntryPoint = "FindDevice", CallingConvention = CallingConvention.Cdecl)]        public static extern bool FindDevice(ref FindDeviceAck_Struct finddeviceack);

The call is successful.

 

Related Article

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.