C#寄存器外部dll回调

C#寄存器外部dll回调,c#,pointers,dll,callback,C#,Pointers,Dll,Callback,我使用Visual Studio Community 2015和Visual C。 我有麻烦是为了有一个签名本可以用。 该焊盘通过USB连接,并提供“dll”以获取签名 我习惯于与WinDev合作,但我认为回调有问题。。。 所以我决定试试C#,但因为我是初学者,所以有麻烦了 我创建了一个项目,允许使用“不安全”代码,并编写了我的第一个函数,即初始化Pad。 它工作得很好,因为这个调用没有复杂的类型。 它通过回调指针变厚 以下是制造商提供的程序声明的文件摘录: ---有效的方法是: Format:

我使用Visual Studio Community 2015和Visual C。 我有麻烦是为了有一个签名本可以用。 该焊盘通过USB连接,并提供“dll”以获取签名

我习惯于与WinDev合作,但我认为回调有问题。。。 所以我决定试试C#,但因为我是初学者,所以有麻烦了

我创建了一个项目,允许使用“不安全”代码,并编写了我的第一个函数,即初始化Pad。 它工作得很好,因为这个调用没有复杂的类型。 它通过回调指针变厚

以下是制造商提供的程序声明的文件摘录:
---有效的方法是:

Format: BYTE uSign300_OpenHid(UINT auiVid, UINT auiPid)
Parameter: auiVid The Vendor ID
auiPid The Product ID
Return: BYTE : 0=FAIL, 1=SUCCESS (these are constants defined earlier)
Example: uSign300_OpenHid(0x0ACD,0x1320);
---给我带来麻烦的两个

Function: uSign300_AddPointHandle
Description: Register a call-back function for StartCapture function, the function will be called when receiving signature data
Format: **BYTE uSign300_AddPointHandle(PSIGN_FUNC func,LPVOID pParam)**
Parameter: func The name of call-back function
The format of PSIGN_FUNC is typedef void **(WINAPI *PSIGN_FUNC)(int*,int, LPVOID)**
The first parameter is data buffer
The second parameter is the length of data.
Please see demo code for more information.
pParam The current pointer
Return: 0=FAIL
Example: uSign300_AddPointHandle(point_handle,this);
我的代码是:

namespace uSign300ns
{

    public static class retcode
    {
        public const byte SUCCESS = 0x01;
        public const byte FAIL = 0x01;
        public static char sdialog;
    }

    unsafe public class uSign300
    {
        const string _dllLocation = "uSign300Kit.dll";
        [DllImport(_dllLocation)]
        public static extern byte uSign300_OpenHid(uint auiVid, uint auiPid);
        [DllImport(_dllLocation)]
        public static extern bool uSign300_Close();
        [DllImport(_dllLocation)]
        public static extern byte uSign300_ClearSignature();
        [DllImport(_dllLocation)]
        public static extern byte uSign300_AddPointHandle(void* func, void* pParam);
        [DllImport(_dllLocation)]
        public static extern byte uSign300_StartCapture(byte f_Mode, byte f_Interval, byte s_Red, byte s_Green, byte s_Blue, byte b_Red, byte b_Green, byte b_Blue);
        [DllImport(_dllLocation)]
        public static extern byte uSign300_ExitCapture();
        [DllImport(_dllLocation)]
        public static extern byte uSign300_ControlLED(byte leftLED, byte rightLED);

        //
        // --- Méthode d'initialisation
        public static bool __uSign_000_Connect()
        {
            bool bRet = false;
            byte nRet;
            nRet = uSign300_OpenHid(0x0ACD, 0x1320);
            if (nRet == retcode.SUCCESS)
            {
                bRet = true;
            }
            if (bRet)
            {
                // on éteint les LEDs
                uSign300_ControlLED(0x00, 0x00);
            }

            return bRet;
        }


        private void __uSign_callback(void* __ptr_data, int datalen, void* __ptr_dialog)
        {
            MessageBox.Show("Callback appelée");
        }


        public static bool __uSign_100_StartThreadedCapture()
        {
            bool bRet;
            byte nRet;
            //void* __ptr_callback;
            //string sChaine;
            nRet = uSign300_ClearSignature();

            // enregistrer la callback
            fixed (char* __ptr_dialog = &retcode.sdialog) {
                uSign300_AddPointHandle(__uSign_callback, __ptr_dialog);
            }

           

            bRet = true;
            return bRet;
        }
    }
}
这里是制造商提供的C语言示例代码

void __stdcall point_handle (int *buf, int rev, LPVOID pParam)
{   

    for(int i = 0; i < rev;)
    {
            int point_x;
            int point_y;
            point_x = buf[i++];
            point_y = buf[i++]; 
    }
}

void OnStartcapturing() 
{
    uSign300_AddPointHandle(point_handle,this); 
    AfxBeginThread(ThreadProc_Capture, this);
}
void\u标准调用点\u句柄(int*buf,int rev,LPVOID pParam)
{   
对于(int i=0;i

我无法让编译正常工作。

至少有一个错误,但我不能保证这是问题的根源

要传递回调函数,请传递委托,而不是void*:

delegate void PSIGN_FUNC(int* buf, int rev, void* pParam);
然后将dll调用声明为:

[DllImport(_dllLocation)]
public static extern byte uSign300_AddPointHandle(PSIGN_FUNC func, void* pParam);

如果答案有用,就接受它。