Register Callback function to C++ DLL from C#

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Henrik Pedersen

    #1

    Register Callback function to C++ DLL from C#



    Hi

    I am developing a wrapper for a C++ SDK DLL built for a video capture
    card. After opening all channels on the card i call the function
    StartVideoCaptu re(IntPtr channelHandle) what is supposed to happen after
    that is that when the data stream is reday the DLL should raise an event
    through a callback that i have registerd with the DLL. In that function
    i can then access the data stream. The problem is just that the C++ Dll
    uses void* pointers and i haven´t got the slightest clue on how to
    Marshal that. I have the full source code of a C++ demo project and all
    the DLL specifications. The function specs in the DLL lokks like this:

    1.
    typedef int (*STREAM_DIRECT _READ_CALLBACK) (ULONG channelNumber,v oid
    *DataBuf,DWORD Length,int FrameType,void *context);

    2.
    DLLEXPORT_API int __stdcall
    RegisterStreamR eadCallback(STR EAM_READ_CALLBA CK StreamReadCallb ack, void
    *Context);


    I have tried with something like this but with no luck.

    public delegate short STREAM_DIRECT_R EAD_CALLBACK( uint
    channelNumber,[MarshalAs(Unman agedType.IUnkno wn)]object DataBuf,uint
    Length,int FrameType,[MarshalAs(Unman agedType.IUnkno wn)]object Context);

    [DllImport("DsSd k.dll", CharSet=CharSet .Ansi)]
    public static extern short
    RegisterStreamD irectReadCallba ck(STREAM_DIREC T_READ_CALLBACK
    StreamDirectRea dCallback,[MarshalAs(Unman agedType.IUnkno wn)]object
    Context);

    And then:
    RegisterStreamD irectReadCallba ck(new
    STREAM_DIRECT_R EAD_CALLBACK(th is.StreamDirect ReadCallback),
    this.Handle);

    But it keeps crashing hard every time i try to run it.
    I have struggeld with this for a while now and desperatly need help..


    /Henrik Pedersen
    Sweden

    *** Sent via Developersdex http://www.developersdex.com ***
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Register Callback function to C++ DLL from C#

    Henrik,

    Your delegate should look like this:

    public delegate int STREAM_DIRECT_R EAD_CALLBACK(
    uint channelNumber,
    IntPtr DataBuf,
    uint Length,
    int FrameType,
    IntPtr Context);

    And your method declaration to register the callback should look like
    this:

    [DllImport("DsSd k.dll", CharSet=CharSet .Ansi)]
    public static extern int RegisterStreamD irectReadCallba ck(
    STREAM_DIRECT_R EAD_CALLBACK StreamDirectRea dCallback,
    IntPtr Context);

    Basically, void pointers are marshaled as IntPtr instance. Of course,
    this means that you have to marshal whatever you want to pass yourself. If
    you know that the context is always going to be of a more particular type,
    then you can be more specific with the declaration.

    Also, int in C++ marshals as an int in .NET.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m
    [color=blue]
    > I am developing a wrapper for a C++ SDK DLL built for a video capture
    > card. After opening all channels on the card i call the function
    > StartVideoCaptu re(IntPtr channelHandle) what is supposed to happen after
    > that is that when the data stream is reday the DLL should raise an event
    > through a callback that i have registerd with the DLL. In that function
    > i can then access the data stream. The problem is just that the C++ Dll
    > uses void* pointers and i haven´t got the slightest clue on how to
    > Marshal that. I have the full source code of a C++ demo project and all
    > the DLL specifications. The function specs in the DLL lokks like this:
    >
    > 1.
    > typedef int (*STREAM_DIRECT _READ_CALLBACK) (ULONG channelNumber,v oid
    > *DataBuf,DWORD Length,int FrameType,void *context);
    >
    > 2.
    > DLLEXPORT_API int __stdcall
    > RegisterStreamR eadCallback(STR EAM_READ_CALLBA CK StreamReadCallb ack, void
    > *Context);
    >
    >
    > I have tried with something like this but with no luck.
    >
    > public delegate short STREAM_DIRECT_R EAD_CALLBACK( uint
    > channelNumber,[MarshalAs(Unman agedType.IUnkno wn)]object DataBuf,uint
    > Length,int FrameType,[MarshalAs(Unman agedType.IUnkno wn)]object Context);
    >
    > [DllImport("DsSd k.dll", CharSet=CharSet .Ansi)]
    > public static extern short
    > RegisterStreamD irectReadCallba ck(STREAM_DIREC T_READ_CALLBACK
    > StreamDirectRea dCallback,[MarshalAs(Unman agedType.IUnkno wn)]object
    > Context);
    >
    > And then:
    > RegisterStreamD irectReadCallba ck(new
    > STREAM_DIRECT_R EAD_CALLBACK(th is.StreamDirect ReadCallback),
    > this.Handle);
    >
    > But it keeps crashing hard every time i try to run it.
    > I have struggeld with this for a while now and desperatly need help..
    >
    >
    > /Henrik Pedersen
    > Sweden
    >
    > *** Sent via Developersdex http://www.developersdex.com ***[/color]


    Comment

    Working...