1
Answer

Calling CoCreateInstance in C# (C++ to C# migration)

Lean A

Lean A

14y
4.3k
1

Hi all,


I am migrating some code from C++ to a C# WebService, and specifically I use CoCreateInstance in the C++ code. I have two questions that interests me:


1) Is it possible to integrate a C++ code into the C# code?

2) If not, is there an equivalent CoCreateInstance in C#?

 

For more info, I import two files which are #include "SmsMsgAPI.h", #include "SmsMsgAPI_i.c". Then the CoCreateInstance looks like this:

 

sLocationRequest = sNewLocationRequest.AllocSysString();

        sMPIPAddress = sMPIP.AllocSysString();

        CoInitialize(0);

 

    _BEGIN

 

        //Create root messaging object

        _CHECKHR( ::CoCreateInstance(

                    CLSID_SmsMessaging,

                    NULL,

                    CLSCTX_INPROC,

                    IID_ISmsMessaging,

                    (LPVOID*)&pMessaging) );

 

        //Create message object for request

        _CHECKHR( pMessaging->CreateMessage(&pRequest) );

 

        //Set the target of the message to the Location Manager endpoint

        _CHECKHR( pRequest->SetTargetEndpoint(L"MP_LocationManager") );

 

        //Set the message body with the request

        _CHECKHR( pRequest->SetBodyFromString(sLocationRequest) );

 

        // Invoke the targeted endpoint on the local machine with the request,

        // and retrieve the reply.

        _CHECKHR( pMessaging->Invoke(sMPIPAddress, pRequest, &pReply) );

 

        // Extract body from reply message.

        _CHECKHR( pReply->GetBodyToString(&pszReplyMessage) );

 

        wprintf(L"\n\nReply from MP:\n%s", (wchar_t *) pszReplyMessage);

 

    _END

 

Thank you in advance!


Answers (1)