Skip to content

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

Lean A

Asked May 27, 2010, 4:05 AM EDT1 answer
Share
LinkedInX

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#?

 

Code
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,

C#
                    (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!

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.