简体   繁体   中英

How to create a c# wrapper for c++ function?

Hello as title says i need to create a wrapper to use it in my c# code from C.But the truth is that i have no knowledge of C but i'm much better c#.

C

    HRESULT ( STDMETHODCALLTYPE *Connect )( 
        ExCtrl * This,
        /* [in] */ EX_CONNECT *pConnect);

    HRESULT ( STDMETHODCALLTYPE *Disconnect )( 
        ExCtrl * This);

    HRESULT ( STDMETHODCALLTYPE *IsConnected )( 
        ExCtrl * This);

    HRESULT ( STDMETHODCALLTYPE *GetStatus )( 
        ExCtrl * This,
        /* [out] */ ExStatus **ppStatus);
HRESULT ( STDMETHODCALLTYPE *GetPreview )( 
        ExCtrl * This,
        /* [out] */ HBITMAP *phbm);

I managed to write for "Connect","Disconnect","IsConnected"

C++

HRESULT Connect(String^ ServerIP)
{
    IntPtr ptr = Runtime::InteropServices::Marshal::StringToBSTR(ServerIP);//
    CComBSTR bstr;                                                                                 //Converting ip string 
    bstr.Attach(static_cast<BSTR>(ptr.ToPointer()));    



    if (IsConnected())
        return E_UNEXPECTED;        // already connected

    ExCtrl *piCtrl = NULL;
    HRESULT hr = Ex_CreateController(__uuidof(ExCtrl), __uuidof(ExCtrl), (void **)&piCtrl);
    if (FAILED(hr)) return hr;

    EX_CONNECT connection = {0};                    // Connection data

    connection.appType = APP_TYPE;                      
    connection.bstrPrimaryName=CComBSTR(bstr);              // 
    connection.layerId = (int)LAYER0 + m_iLayerNum; 
    connection.masterServer = SERVER_PRIMARY;
    TIME_ZONE_INFORMATION tz;
    connection.pTz = &tz;

    hr = piCtrl->Connect(&connection);
    if (FAILED(hr)) {
        piCtrl->Release();       // connection failed - clear the controller
    } else
        m_piCtrl = piCtrl; // succefully connected

    return hr;
}


bool IsConnected()
{
    return (m_piCtrl != NULL);
}

HRESULT Disconnect()
{
    if (!IsConnected())
        return E_UNEXPECTED;        // not connected

    HRESULT hr = m_piCtrl->Disconnect();
    if (SUCCEEDED(hr)) m_piCtrl->Release();
    m_piCtrl = NULL;
    return hr;
}

The problem is for "GetPreview" i dont know how to convert HBitmap to Bitmap. I tried like this :

    HRESULT GetImagePreview(BITMAP bm)
{   
    HBITMAP hB =NULL;

    tExCtrl *piCtrl = NULL;
    HRESULT hr = Ex_CreateController(__uuidof(ExCtrl), __uuidof(ExCtrl), (void **)&piCtrl);


hr = piCtrl->GetPreview(&hB);
hr = GetObject(hB, sizeof(BITMAP), &bm);
 return hr;
}

But without luck

Thanks,

The C code looks like part of a COM interface generated by the MIDL compiler, so you can probably use it from C# directly without any wrappers (just add a reference to the COM component). But if you choose to use a C++/CLI wrapper, throw exceptions instead of returning HRESULTs.

As for your GetImagePreview method, you only get half of the job done (you're not even returning any Bitmap object). Just google for "HBITMAP to Bitmap" and you'll get many results.

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