简体   繁体   中英

Register DLL c++ events to C#

I've c++ DLL implementation example and i want to build a GUI and web services in C#, working with Interop.

This is example of C++ implementation:

//DLL headers (only what we need)
extern  DWORD   __stdcall   DIVA_Mgmt_RegisterEventInterfaceEx ( HANDLE hInstance, HANDLE hAdapter, DWORD dwEventMask, 
                                                                   void *pEventInterfaceEx );

//code of exe sample
class   CDivaEventNotify
{
public:
//    virtual ~CDivaEventNotify () {}   see comment at CDivaCardStatistics

    virtual void    StateLayer1Changed ( DWORD dwNewState ) = 0;
    virtual void    StateLayer2Changed ( DWORD dwNewState ) = 0;
    virtual void    CallsChanged ( BOOL bInbound ) = 0;
    virtual void    LineStateChanged ( DWORD dwChannel, BOOL bUp, B_CHANNEL_INFO *pChannelInfo ) = 0;
};

/*
 * extended event interface for analog card support
 */
class   CDivaEventNotifyEx : public CDivaEventNotify
{
public:
//    virtual ~CDivaEventNotifyEx () {}   see comment at CDivaCardStatistics

    virtual void    PotsLineStateChanged ( DWORD dwNewState, DWORD dwLine ) = 0;
    virtual void    AlarmChanged ( PRI_ALARM alarm, BOOL bNewState ) = 0;
};
class CMyEventHandler : public CDivaEventNotifyEx
{
 //omit implementation...
}
int main(){
    HANDLE hInstance;

    CMyEventHandler *pHandlerList = NULL;
    static const DWORD dwEvMask = DIVA_EVENT_MASK_ALL;
    DIVA_Mgmt_RegisterInstance( DIVA_MGMT_IF_VERSION, &hInstance );


    while (DIVA_Mgmt_EnumAdapter( hInstance, &hEnum, &info ) == IDI_SUCCESS)
    {
        CMyEventHandler *pNew = new CMyEventHandler( hInstance, &info );
        if (!pNew)
            continue;

        DIVA_Mgmt_RegisterEventInterfaceEx( hInstance, info.hAdapter, dwEvMask, pNew );

        pNew->pNext = pHandlerList;
        pHandlerList = pNew;
    }
}

This code works fine in c++ environment. This DLL is not COM compliance and i call it in c# with Interop P/Invoke.

[DllImport("dimgmtif.dll", SetLastError = true)]
public static extern IntPtr DIVA_Mgmt_RegisterEventInterfaceEx(IntPtr hInstance, IntPtr hAdapter, UInt32 dwEventMask,ref IntPtrp EventInterfaceEx);

Other extern call to C++ DLL with C# works fine,but in this call I have to pass " EventInterfaceEx " and I do not know how to do. It is possible to translate "CDivaEventNotify" and "CDivaEventNotifyEx" implementation to c# and passing it to extern dll Interop DIVA_Mgmt_RegisterEventInterfaceEx?

It is not possible to implement CDivaEventNotify or CDivaEventNotifyEx in C#. If these were COM compatible interfaces then they could be implemented. But they are not.

Frankly this API is not very easy to call at all. It has not really been designed with interop in mind. It relies on the implementor of the interfaces to lay out the vtable in the same way as the native code.

Your best hope of solving this problem lies in implementing the interfaces in a mixed mode C++/CLI assembly. You can wrap up the functionality there, and expose it as a ref class to be consumed by your C# code.

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