简体   繁体   中英

Call a function in a c++ dll from c#

I want call a Function in a c++ dll from c# but i can't get it work. That is the Dokumentation from the Function :

VOID WINAPI EnumerateHidDevices( PVOID pContext, ENUM_HID_PROC Callback);

Function description: Enumerates all of the EETI PCAP HID touch devices in the system.

Parameters:

pContext :The pointer point to caller prepared memory. This address will be passed to the callback routine.

Callback :Caller prepared callback routine. This callback routine will be called whenever the API enumerates a PCAP touch device.

ENUM_HID_PROC :Prototype of device enumeration callback routine.

typedef BOOL (WINAPI *ENUM_HID_PROC)(PVOID pContext,LPCTSTR szSymbolicName, LPCTSTR szProduct, ULONG ulInLength, ULONG ulOutLength);

pContext :Pointer to the caller prepared buffer for the callback routine.

szSymbolicName :The device symbolic Link Name the device enumerator enumerated. This string need be used to open the device with OpenHIDDevice.

szProduct :Identifies product name

ulInLength :Maximum size of input report in bytes. This parameter need be used to open the device with OpenHIDDevice.

ulOutLength : Maximum size of output report in bytes. This parameter need be used to open the device with OpenHIDDevice.

And that how its look at the moment in c#:

    public HID_DEVICE_INFO m_HIDDevInfo = new HID_DEVICE_INFO();
    public struct HID_DEVICE_INFO
    {
        public string strDeviceName;
        public uint m_ulInLength;
        public uint m_ulOutLength;
        public ushort m_VID;
        public ushort m_PID;
    }

    public delegate int ENUM_HID_PROC(object pContext, string szSymbolicName, string szProduct, uint ulInLength, uint ulOutLength);

    [DllImport("HIDdAPI.dll")]
    public static extern void EnumerateHidDevices(object pContext, ENUM_HID_PROC Callback);

    public MainView1()
    {
        InitializeComponent();
        EnumerateHidDevices(m_HIDDevInfo, EnumUsbHidCallback);
    }

    public static int EnumUsbHidCallback(object pContext, string szDeviceName, string szProduct, uint ulInLength, uint ulOutLength)
    {
        HID_DEVICE_INFO pInfo = (HID_DEVICE_INFO)pContext;

        pInfo.strDeviceName = szDeviceName;
        Console.Write("\r\n");
        Console.Write("Enumerate EETI HID Device: {0}\r\n", pInfo.strDeviceName);
        Console.Write("\r\n");
        pInfo.m_ulInLength = ulInLength;
        pInfo.m_ulOutLength = ulOutLength;

        return 1;
    }

This ends in an ArgumentException at EnumerateHidDevices(m_HIDDevInfo, EnumUsbHidCallback):

System.ArgumentException occurred

HResult=-2147024809

Message=Value does not fall within the expected range.

Source=mscorlib

StackTrace: at System.StubHelpers.ObjectMarshaler.ConvertToNative(Object objSrc, IntPtr pDstVariant) InnerException:

Where is the problem ?

Thanks for your help.

Imho your code is unsafe. I can't see you pinning your object anywhere in your snippet. I believe the correct way to do what you want is to pass IntPtr to the method and get a pointer to struct via Marshal.StructureToPtr .

You may also want to check this question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM