Convert VARIANT to ...?

Note:

  • Attempting to call an interface method from which the return type is _variant_t

The code:

 _variant_t resultsDataString; _bstr_t simObjectNames; simObjectNames = SysAllocString (L"TEST example 3"); resultsDataString = pis8->GetSimObject (simObjectNames); 

the built-in function is shown below, contained in the .tli FILE:

 inline _variant_t IS8Simulation::GetSimObject ( _bstr_t Name ) { VARIANT _result; VariantInit(&_result); HRESULT _hr = get_SimObject(Name, &_result); if (FAILED(_hr)) _com_issue_errorex(_hr, this, __uuidof(this)); return _variant_t(_result, false); } 

Note:

  • resultsDataString has a struct tagVARIANT :
    • VARTYPE vt - 9 (unsigned)
    • IDispatch interface pointer

Question

Edit:

Note:

  • We suggest converting the following Visual Basic code to Visual C ++

  • MFC or ATL, if necessary

  • Ideally, pure C ++

Visual Basic equivalent :

 Public example1, example2 As SIMUL8.S8SimObject Dim numberOfexamples As Variant Dim resultString As Variant Set example1 = MySimul8.SimObject("Example 1") Set example2 = MySimul8.SimObject("Example 2") numberOfexamples = example1.CountContents + example2.CountContents resultString = CStr(numberOfexamples) & "*" 
+2
c ++ windows visual-studio-2008 com
source share
4 answers

It looks like you are using C ++ as a COM client, relying on the native COM support of the VC ++ compiler. To make coding “easier” for the client, you used #import to create C ++ shell classes that try to hide all COM data from you, or at least simplify COM data. This way you are not using the COM SDK directly, but using the client-side platform (I think it looks like a lightweight COM-only platform similar to ATL or MFC).

However, your sample code apparently mixes the direct low-level COM SDK ( VARIANT s, BSTR , SysAllocString ) with the #import COM infrastructure ( _variant_t , _bstr_t , XXXXPtr ). COM from C ++ is complicated at first - so in an ideal world, I would suggest getting to know the basics of COM before going too far ahead.

However, if you just want something to work, I would guess this is the version of the #import-style-of-COM version of the VB code that you provided:

 _variant_t example1Var; _variant_t example1Var; SIMUL8::S8SimObjectQIPtr example1; // I'm guessing at this type-name from the VB code SIMUL8::S8SimObjectQIPtr example2; example1Var = pis8->GetSimObject(_bstr_t(L"Example 1")); example2Var = pis8->GetSimObject(_bstr_t(L"Example 2")); if (example1Var.vt == VT_DISPATCH && example2Var.vt == VT_DISPATCH) { // **UPDATE** to try to spoon feed the QI ptr... example1 = IDispatchPtr((IDispatch*)example1Var); example2 = IDispatchPtr((IDispatch*)example2Var); // Does this screw-up reference counting? int numberOfexamples = example1->CountContents + example2->CountContents; } 

UPDATE:
#import documentation This makes it easier to use COM with C ++, but one more thing to find out ...

+2
source share

Take a look at the MSDN docs for _ variant_t

There is a ChangeType method, as well as Extractors .

Edit: If you have an IDispatch pointer, you need to use QueryInterface to get a specific object type with elements and methods, or use IDispatch::Invoke . You can see the MSDN Docs for IDispatch . In any case, you need to know the type of object returned by calling IS8Simulation::GetSimObject .

Edit # 2: Based on your VB code update, you want to use the same code for C ++, i.e. use the S8SimObject type in your C ++ form (see generated. tlh for _COM_SMARTPTR_TYPEDEF ). Then you can do the same with CountContents . Otherwise, you will need to search for CountContents DISPID using IDispatch::GetIDsOfNames and then invoke invoke.

+2
source share

You do not need to convert _variant_t . It contains an IDispatch interface pointer, so you can get it like this:

 if (v.vt == VT_DISPATCH) { IDispatchPtr pDispatch = v.pdispVal; // Do something with pDispatch. // eg assign it to a strongly-typed interface pointer. } 
+1
source share

There is a concept of "value" for an object (DISPID_VALUE).

Here is a codeproject.com article on VARIANT resolution that may help.

0
source share

All Articles