C#的包装和回调 [英] C# wrapper and Callbacks

查看:218
本文介绍了C#的包装和回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写Dallmeier的通用API光一个C#包装的过程中(相机放大器; Surviellance系统)和我以前从未编写过的包装,但我已经使用了佳能EDSDK C#包装。所以我使用佳能的包装为指导,以书面Dallmeier的包装。

I'm in the process of writing a C# wrapper for Dallmeier Common API light (Camera & Surviellance systems) and I've never written a wrapper before, but I have used the Canon EDSDK C# wrapper. So I'm using the Canon wrapper as a guide to writing the Dallmeier wrapper.

我目前具有包装回调的问题。在API手册,它具有以下内容:

I'm currently having issues with wrapping a callback. In the API manual it has the following:

dlm_connect

int(unsigned long uLWindowHandle,   
    const char * strIP,  
    const char* strUser1,  
    const char* strPwd1,  
    const char* strUser2,  
    const char* strPwd2,   
    void (*callback)(void *pParameters, void *pResult, void *pInput),   
    void * pInput)  

<强>参数结果,
- ulWindowhandle - 即传递给ViewerSDK显示视频和信息的窗口的句柄有结果,
- strUser1 / 2 - 的用户的名称登录如果只使用单一用户登录strUser2是结果,
- 空结果,
- strPwd1 / 2 - 这两个密码的用户。如果strUser2为NULL strPwd2被忽略。

Arguments
- ulWindowhandle - handle of the window that is passed to the ViewerSDK to display video and messages there
- strUser1/2 - names of the users to log in. If only single user login is used strUser2 is
- NULL
- strPwd1/2 - passwords of both users. If strUser2 is NULL strPwd2 is ignored.

返回结果
这个函数创建一个具有传递

Return
This function creates a SessionHandle that has to be passed

回调结果
pParameters的结构将:结果
- 无符号长ulFunctionID结果
- 无符号长ulSocketHandle,//处理对已建立的连接结果
插座 - 无符号长ulWindowHandle,结果
- 诠释SessionHandle,//会话创建结果
的会话句柄 - 为const char *地带,结果
- 为const char * strUser1,结果
- 为const char * strPwd1,结果
- 为const char * strUser2,结果
- 为const char * strPWD2结果
pResult是指向的整数,表示该操作的结果。零上的成功。
负值是错误代码

Callback
pParameters will be structured:
- unsigned long ulFunctionID
- unsigned long ulSocketHandle, //handle to socket of the established connection
- unsigned long ulWindowHandle,
- int SessionHandle, //session handle of the session created
- const char * strIP,
- const char* strUser1,
- const char* strPwd1,
- const char* strUser2,
- const char * strPWD2
pResult is a pointer to an integer, representing the result of the operation. Zero on success. Negative values are error codes.

所以从我读过的Net和堆栈溢出 - C#使用委托回调的目的。所以,我创建了一个我的回调函数:

So from what I've read on the Net and Stack Overflow - C# uses delegates for the purpose of callbacks. So I create a my Callback function :

public delegate uint DallmeierCallback(DallPparameters pParameters, IntPtr pResult, IntPtr pInput);



我创建了连接功能。

I create the connection function

[DllImport("davidapidis.dll")]
public extern static int dlm_connect(int ulWindowHandle, string strIP, string strUser1, string strPwd1, string strUser2, string strPwd2, DallmeierCallback inDallmeierFunc

和(我认为)的DallPParameters为结构:

And (I think) the DallPParameters as a struct :

[StructLayout(LayoutKind.Sequential)]
public struct DallPParameters
{
  public int ulfunctionID;
  public int ulsocketHandle;
  public int ulWindowHandle;
  ...
}

这一切在我的包装类结果
我在正确的方向前进,或者这是完全错误的结果
然后在我的Dallmeier的类我有以下几点:?

All of this is in my wrapper class.
Am I heading in the right direction or is this completely wrong?
Then in my Dallmeier class I have the following:

    private DallmeierAPI dallmeierAPI;  
    private DallmeierSDK.DallmeierCallback dallCallbackHandler;  
    private GCHandle handle;  
    private IntPtr pResult;  
    private IntPtr pInput;  

    internal Dallmeier()
    {
        this.dallmeierAPI = DallmeierAPI.Instance;

        registerEvents();
    }

    private void registerEvents()
    {
        // Register Callback Events
        dallCallbackHandler = new DallmeierSDK.DallmeierCallback(pParameters, pResult, pInput); // Error: Method Name expected

        handle = GCHandle.Alloc(dallCallbackHandler);
    }

    private void unregisterEvents()
    {
        handle.Free();
    }

    private DallmeierSDK.DallPParameters pParameters(int ulfunctionID, int ulSocketHandl, int ulWindowHandle, int SessionHandle, string strIP, string strUser1, string strPwd1, string strUser2, string strPwd)
    {
        // what goes in here : Error not all code paths return a value
    }


}

所以,当我注册回调的说法方法名称预期?结果
和pParameters期待一个返回值?

So when I register the callback its saying a Method Name expected?
and pParameters is expecting a return value?

推荐答案

您是大多是在正确的轨道上。

You're mostly on the right track.

不过,C 是(我相信)32位,并映射到秒。C# INT 结果
此外,调用该函数后,你必须保持有管理的参考,你传递给确保该委托不被回收委托实例。

However, C longs are (I believe) 32-bit, and map to C# ints.
Also, after calling the function, you must keep a managed reference to the delegate instance that you passed to make sure that the delegate doesn't get garbage collected.

这篇关于C#的包装和回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆