C# wrapper and Callbacks

Posted by fergs on Stack Overflow See other posts from Stack Overflow or by fergs
Published on 2010-03-12T02:18:37Z Indexed on 2010/03/12 2:27 UTC
Read the original article Hit count: 546

Filed under:
|
|

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.

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)  

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

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.

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(ulong ulWindowHandle, string strIP, string strUser1, string strPwd1, string strUser2, string strPwd2, DallmeierCallback inDallmeierFunc

And (I think) the DallPParameters as a struct :

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

All of this is in my wrapper class. Am I heading in the right direction or is this completely wrong?

© Stack Overflow or respective owner

Related posts about c#

Related posts about wrapper