Unable to CoCreateInstance.

cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#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;

		//HRESULT from CoCreateInstance is 0x80004002

		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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// 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?
Last edited on
If CoCreateInstance fails there is no point in going any further.
Check the return value and see why it fails.
The HRESULT is 0x80004002
It means 0x80004002 => E_NOINTERFACE => No such interface supported
Are you sure the COM object is correctly registered ?
Did it come with an installer?
How do I check if a COM object is correctly registered? I did run `regasm.exe /codebase:TestZ.dll` if that's what you're asking.

There's no installer. The dll was generated by IKVM - it's a tool that converts jar to dll.
Last edited on
If regasm didn't fail you can assume that it's correctly registered.
One thing to try would be to create a C# console app and then add a reference to your component.
In Solution explorer right-click on references, and in the com section browse to your component and click ok.
The you can inspect it in the Object Browser and see what you actually got.
Yes, the C# project works perfectly.

In C#, TestZ#doSomething() can be called, and the typical Java methods for the Object are all there.

I ran "regasm.exe /tlb /codebase TestZ.dll" successfully.
Last edited on
Is there another way to use the dll and instantiate the TestZ Object?
Didn't you mention in a previous thread that you could use C++ / CLR ?
If it works in C# it will work with C++ / CLR as well
You are absolutely right, thank you very much. It works in CLR.

Unfortunately, I have to get it to work with non-CLR also.

I got it to work with an example application.

Class1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace MyInterOp
{
    [StructLayout(LayoutKind.Sequential)]
    public struct MyStruct
    {
        [MarshalAs(UnmanagedType.BStr)]
        public string name;
        [MarshalAs(UnmanagedType.BStr)]
        public string surname;
        public int age;
    }
    
    
    public interface ITest
    {        
        MyStruct[] GetData        
        {
            get;
        }
    }
    public class Test : ITest
    {
        
        MyStruct[] st = new MyStruct[2];
        public Test()
        {
            st[0].name = "abc";
            st[0].surname = "def";
            st[0].age = 10;

            st[1].name = "qwe";
            st[1].surname = "rty";
            st[1].age = 20;
        }
 
        public MyStruct[] GetData
        {
            get
            {
                return st;
            }

        } 


        
    }
}


After generating the DLL, I ran:
1
2
3
4
5
6
7
8
9
"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\bin\Hostx86\x86\dumpbin.exe" MyInterOp.dll > MyInterOp.def

"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\bin\Hostx86\x86\lib.exe" /def:MyInterOp.def /out:MyInterOp.lib

C:\Windows\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe /codebase /tlb MyInterOp.dll

copy /Y MyInterOp.def C:\Users\Dev\Documents\Xamarin\TestZ\TestMyInterOp\MyInterOp.def
copy /Y MyInterOp.lib C:\Users\Dev\Documents\Xamarin\TestZ\TestMyInterOp\MyInterOp.lib
copy /Y MyInterOp.exp C:\Users\Dev\Documents\Xamarin\TestZ\TestMyInterOp\MyInterOp.exp


With all the files inside the project folder, I configured the Linker > Additional Dependencies to include MyInterOp.lib.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <string>
#include<string.h>
#include <windows.h>
#include "tchar.h"

#import "MyInterOp.tlb"

using namespace MyInterOp;

#include <string>
using namespace std;


int main(int argc, char* argv[])
{
	HRESULT hr = CoInitialize(NULL);
	std::cout << "CoInitialise(): " << hr << std::endl;
	ITest* pTest = NULL;
	hr = CoCreateInstance(__uuidof(Test), NULL, CLSCTX_INPROC_SERVER, __uuidof(ITest), (void**)&pTest);
	std::cout << "CoCreateInstance(): " << hr << std::endl;
	return 0;
}


The output printed on the console is:

1
2
CoInitialise(): 0
CoCreateInstance(): 0
Last edited on
Also having the same problem looking for help!
Topic archived. No new replies allowed.