Unable to CoCreateInstance

  • Thread starter Thread starter HiddenHandX
  • Start date Start date
H

HiddenHandX

Guest
cpp
Code:
#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <string>
#include <windows.h>
#include "tchar.h"

#import "TestZ.tlb" raw_interfaces_only
using namespace TestZ;


int _tmain(int argc, _TCHAR* argv[])
{
    std::cout << "start" << std::endl;
    CoInitialize(NULL);
    try {
        _StubRequest *stubRequest = NULL;
        std::cout << stubRequest << std::endl;

        std::cout << "//////////" << std::endl;
        HRESULT result = CoCreateInstance(__uuidof(StubRequest), NULL, CLSCTX_INPROC_SERVER, __uuidof(_StubRequest), reinterpret_cast<void**> (&stubRequest));
        std::cout << "//////////" << std::endl;

        if (SUCCEEDED(result)) {
            std::cout << "success" << std::endl;
        }
        else {
            std::cout << "failed" << std::endl;
        }
        stubRequest->doSomething();
        std::cout << "ok" << std::endl;
    }
    catch (_com_error &e) {
        std::cout << "error: " << e.Error() << std::endl;
        std::cout << "error message: " << e.ErrorMessage() << std::endl;
        std::cout << "error info: " << e.ErrorInfo() << std::endl;
    }
    CoUninitialize();
    return 0;
}

tlh
Code:
// Created by Microsoft (R) C/C++ Compiler Version 14.14.26429.4 (e5d5499a).
//
// c:\users\dev\documents\xamarin\testdll\consoleapplication1\debug\testz.tlh
//
// C++ source equivalent of Win32 type library TestZ.tlb
// compiler-generated file created 06/22/18 at 15:47:05 - DO NOT EDIT!

//
// Cross-referenced type libraries:
//
//  #import "C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscorlib.tlb"
//  #import unregistered typelib, LIBID {60E2085D-39FC-30C5-9DFD-5B097D4A04BC}
//

#pragma once
#pragma pack(push, 8)

#include <comdef.h>

namespace TestZ {

//
// Forward references and typedefs
//

struct __declspec(uuid("ccd45295-11d4-37cf-b744-66bf56fc60ad"))
/* LIBID */ __TestZ;
struct /* coclass */ Initialiser;
struct /* coclass */ StubRequest;
struct __declspec(uuid("8a687914-a119-3874-a86b-a39d6a1bc48b"))
/* dual interface */ _Initialiser;
struct __declspec(uuid("845d446a-f39b-364a-a295-576aca2ef1e3"))
/* dual interface */ _StubRequest;

//
// Smart pointer typedef declarations
//

_COM_SMARTPTR_TYPEDEF(_Initialiser, __uuidof(_Initialiser));
_COM_SMARTPTR_TYPEDEF(_StubRequest, __uuidof(_StubRequest));

//
// Type library items
//

struct __declspec(uuid("1d680efc-90be-35c8-9caf-b44b96534b54"))
Initialiser;
    // [ default ] interface _Initialiser
    // interface _Object

struct __declspec(uuid("7a542aa1-0fb8-35b9-b38f-d848f4548b4b"))
StubRequest;
    // [ default ] interface _StubRequest
    // interface _Object

struct __declspec(uuid("8a687914-a119-3874-a86b-a39d6a1bc48b"))
_Initialiser : IDispatch
{
    //
    // Raw methods provided by interface
    //

      virtual HRESULT __stdcall get_toString (
        /*[out,retval]*/ BSTR * pRetVal ) = 0;
      virtual HRESULT __stdcall equals (
        /*[in]*/ VARIANT obj,
        /*[out,retval]*/ VARIANT_BOOL * pRetVal ) = 0;
      virtual HRESULT __stdcall hashCode (
        /*[out,retval]*/ long * pRetVal ) = 0;
      virtual HRESULT __stdcall GetType (
        /*[out,retval]*/ struct _Type * * pRetVal ) = 0;
      virtual HRESULT __stdcall get_toString_2 (
        /*[out,retval]*/ BSTR * pRetVal ) = 0;
      virtual HRESULT __stdcall hashCode_2 (
        /*[out,retval]*/ long * pRetVal ) = 0;
      virtual HRESULT __stdcall equals_2 (
        /*[in]*/ VARIANT obj,
        /*[out,retval]*/ VARIANT_BOOL * pRetVal ) = 0;
      virtual HRESULT __stdcall notifyAll ( ) = 0;
      virtual HRESULT __stdcall notify ( ) = 0;
      virtual HRESULT __stdcall wait ( ) = 0;
      virtual HRESULT __stdcall wait_2 (
        /*[in]*/ __int64 timeout ) = 0;
      virtual HRESULT __stdcall wait_3 (
        /*[in]*/ __int64 timeout,
        /*[in]*/ long nanos ) = 0;
      virtual HRESULT __stdcall getClass (
        /*[out,retval]*/ struct _Class * * pRetVal ) = 0;
      virtual HRESULT __stdcall get_ToString_3 (
        /*[out,retval]*/ BSTR * pRetVal ) = 0;
      virtual HRESULT __stdcall Equals_3 (
        /*[in]*/ VARIANT p1,
        /*[out,retval]*/ VARIANT_BOOL * pRetVal ) = 0;
      virtual HRESULT __stdcall GetHashCode (
        /*[out,retval]*/ long * pRetVal ) = 0;
      virtual HRESULT __stdcall GetType_2 (
        /*[out,retval]*/ struct _Type * * pRetVal ) = 0;
};

struct __declspec(uuid("845d446a-f39b-364a-a295-576aca2ef1e3"))
_StubRequest : IDispatch
{
    //
    // Raw methods provided by interface
    //

      virtual HRESULT __stdcall get_toString (
        /*[out,retval]*/ BSTR * pRetVal ) = 0;
      virtual HRESULT __stdcall equals (
        /*[in]*/ VARIANT obj,
        /*[out,retval]*/ VARIANT_BOOL * pRetVal ) = 0;
      virtual HRESULT __stdcall hashCode (
        /*[out,retval]*/ long * pRetVal ) = 0;
      virtual HRESULT __stdcall GetType (
        /*[out,retval]*/ struct _Type * * pRetVal ) = 0;
      virtual HRESULT __stdcall get_toString_2 (
        /*[out,retval]*/ BSTR * pRetVal ) = 0;
      virtual HRESULT __stdcall hashCode_2 (
        /*[out,retval]*/ long * pRetVal ) = 0;
      virtual HRESULT __stdcall equals_2 (
        /*[in]*/ VARIANT obj,
        /*[out,retval]*/ VARIANT_BOOL * pRetVal ) = 0;
      virtual HRESULT __stdcall notifyAll ( ) = 0;
      virtual HRESULT __stdcall notify ( ) = 0;
      virtual HRESULT __stdcall wait ( ) = 0;
      virtual HRESULT __stdcall wait_2 (
        /*[in]*/ __int64 timeout ) = 0;
      virtual HRESULT __stdcall wait_3 (
        /*[in]*/ __int64 timeout,
        /*[in]*/ long nanos ) = 0;
      virtual HRESULT __stdcall getClass (
        /*[out,retval]*/ struct _Class * * pRetVal ) = 0;
      virtual HRESULT __stdcall get_ToString_3 (
        /*[out,retval]*/ BSTR * pRetVal ) = 0;
      virtual HRESULT __stdcall Equals_3 (
        /*[in]*/ VARIANT p1,
        /*[out,retval]*/ VARIANT_BOOL * pRetVal ) = 0;
      virtual HRESULT __stdcall GetHashCode (
        /*[out,retval]*/ long * pRetVal ) = 0;
      virtual HRESULT __stdcall GetType_2 (
        /*[out,retval]*/ struct _Type * * pRetVal ) = 0;
      virtual HRESULT __stdcall doSomething ( ) = 0;
};

} // namespace TestZ

#pragma pack(pop)


The return value is

start
00000000
//////////
new StubRequest
//////////
failed

Unhandled Exception: System.InvalidCastException: Specified cast is not valid.

From the output, I can see that the StubRequest was created, because the application printed "new StubRequest". However, the HRESULT is always not successful, and there's an exception instead of printing the line from doSomething().

What must I do so that I can get the pointer to call doSomething() correctly?

Continue reading...
 
Back
Top