0

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);

}
4
  • You might take a look at msdn.microsoft.com/en-us/library/aa910805.aspx, where you can find several com interop conform variant types. I would guess you should use VT_I4. But keep in mind that the length of your C++ integer type can differ from the default C# 32-Bit integer. (This depends on the compiler.) But usually it is handled by the definition of your VARIANT structure Commented Sep 30, 2013 at 10:05
  • Thanks.But ".tlh" file(generated by compiler) has diferent type defined. virtual HRESULT __stdcall myValue( /*[out,retval]*/ long * pRetVal ) = 0; Commented Sep 30, 2013 at 10:17
  • So you know that you need a long x; and call it with IMyPointer->myValue(&x); Commented Sep 30, 2013 at 10:25
  • Thanks a Lot @HansPassant.It works. :-) Commented Sep 30, 2013 at 11:31

1 Answer 1

1

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);

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.