Devshan
Devshan

Reputation: 361

Passing data from C# to unmanaged C++ (using COM Interop)

I'm using Com Interop method to communicate with unmanaged C++ and C#.

I need to send data to unmanaged C++ from C#.

Im already sending "bool" values values from C# & accessing it through "VARIANT_BOOL*" in c++.

I need to send a integer from C#. How can i access that integer value in unmanaged c++ side ?

for example:

C#

 public int myValue()
        {
            return 5;
        }

Unmanaged C++

CoInitialize(NULL);
MyNSpace::MyClassPtr IMyPointer;

 HRESULT  hRes =  IMyPointer.CreateInstance(MyNSpace::CLSID_MyClass);

if (hRes == S_OK)
{
//// ??? define x type

IMyPointer->myValue(x);

}

Upvotes: 0

Views: 223

Answers (1)

borx
borx

Reputation: 411

COM allows to use plain (native) integer types, for example LONG. COM LONG stands for 32-bit signed integer in C++. For example,

HRESULT myValue([out, retval] LONG* nOutVal);

In client (c++) code you just have to declare an ordinal int variable:

if (hRes == S_OK)
{
    int x;
    hRes = IMyPointer->myValue(x);

}

Upvotes: 1

Related Questions