H
HiddenHandX
Guest
My goal is to make Java classes accessible to both C# and C++.
After doing what I did, the C# project cant see and use non-static methods the methods that the C++ project can't see and use anything.
1. How to make the methods viewable to the VC++ project?
2. How to use HRESULT and turn it into StubRequest so that I can use it?
---------- Initialiser.java ----------
package com.zzz.lib;
import cli.System.Runtime.InteropServices.*;
@ClassInterfaceAttribute.Annotation(ClassInterfaceType.__Enum.AutoDual)
public class Initialiser {
public static void initialise(){
System.out.println("init");
}
}
--------------------
---------- StubRequest.java ----------
package com.zzz.lib;
import cli.System.Runtime.InteropServices.*;
@ClassInterfaceAttribute.Annotation(ClassInterfaceType.__Enum.AutoDual)
public class StubRequest {
public StubRequest(){
System.out.println("new StubRequest");
}
public void doSomething(){
System.out.println("do something!!!");
}
}
--------------------
With these 2 testing Java files, I ran the batch file:
---------- buildTest.bat ----------
C:\ikvmc\bin\ikvmc\ikvmstub mscorlib
javac -source 1.7 -target 1.7 -cp mscorlib.jar;. src\com\zzz\lib\Initialiser.java
javac -source 1.7 -target 1.7 -cp mscorlib.jar;. src\com\zzz\lib\StubRequest.java
C:\ikvmc\bin\ikvmc -out:TestZ.dll src\com\zzz\lib\Initialiser.class src\com\zzz\lib\StubRequest.class -r:mscorlib.dll
"C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools\TlbExp.exe" TestZ.dll
C:\Windows\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe /codebase:TestZ.dll
copy /Y TestZ.dll C:\Users\Dev\Documents\Xamarin\TestDLL\ConsoleApplication1\Debug\TestZ.dll
copy /Y TestZ.dll C:\Users\Dev\Documents\Xamarin\TestDLL\ConsoleApplication1\TestZ.dll
copy /Y *.tlb C:\Users\Dev\Documents\Xamarin\TestDLL\ConsoleApplication1\Debug\*.tlb
copy /Y *.tlb C:\Users\Dev\Documents\Xamarin\TestDLL\ConsoleApplication1\*.tlb
--------------------
In the C# project, after importing TestZ.dll and IKVM's OpenJDK dlls...
---------- Program.cs ----------
using com.zzz.lib;
namespace CSTestZ
{
class Program
{
static void Main(string[] args)
{
Initialiser.initialise();
StubRequest stubRequest = new StubRequest();
stubRequest.doSomething();
}
}
}
--------------------
Meanwhile, in the C++ application, I am unable to get anything going.
---------- ConsoleApplication.cpp ----------
#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <string>
#include <windows.h>
#include "tchar.h"
#include "Initialiser.h" //empty file
#import "TestZ.tlb" raw_interfaces_only
using namespace TestZ;
int _tmain(int argc, _TCHAR* argv[])
{
// Initialize COM.
HRESULT hr = CoInitialize(NULL);
// Create the interface pointer.
//InitialiserPtr ptr(__uuidof(Initialiser));
_InitialiserPtr initPtr(__uuidof(Initialiser));
_StubRequestPtr stubPtr(__uuidof(StubRequest));
initPtr->initialise(); //error: class "TestZ::_Initialiser" has no member "initialise"
HRESULT stubRequest = stubPtr.CreateInstance(__uuidof(StubRequest));
//how to use stubRequest into a StubRequest Object?
CoUninitialize();
return 0;
}
--------------------
I see that there are
---------- TestZ.tlh ----------
// 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 11:01:50 - 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("0949a4d9-28bf-356f-a6bb-e2ee73ca0f71"))
/* 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("0949a4d9-28bf-356f-a6bb-e2ee73ca0f71"))
_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;
};
} // namespace TestZ
#pragma pack(pop)
--------------------
Continue reading...
After doing what I did, the C# project cant see and use non-static methods the methods that the C++ project can't see and use anything.
1. How to make the methods viewable to the VC++ project?
2. How to use HRESULT and turn it into StubRequest so that I can use it?
---------- Initialiser.java ----------
package com.zzz.lib;
import cli.System.Runtime.InteropServices.*;
@ClassInterfaceAttribute.Annotation(ClassInterfaceType.__Enum.AutoDual)
public class Initialiser {
public static void initialise(){
System.out.println("init");
}
}
--------------------
---------- StubRequest.java ----------
package com.zzz.lib;
import cli.System.Runtime.InteropServices.*;
@ClassInterfaceAttribute.Annotation(ClassInterfaceType.__Enum.AutoDual)
public class StubRequest {
public StubRequest(){
System.out.println("new StubRequest");
}
public void doSomething(){
System.out.println("do something!!!");
}
}
--------------------
With these 2 testing Java files, I ran the batch file:
---------- buildTest.bat ----------
C:\ikvmc\bin\ikvmc\ikvmstub mscorlib
javac -source 1.7 -target 1.7 -cp mscorlib.jar;. src\com\zzz\lib\Initialiser.java
javac -source 1.7 -target 1.7 -cp mscorlib.jar;. src\com\zzz\lib\StubRequest.java
C:\ikvmc\bin\ikvmc -out:TestZ.dll src\com\zzz\lib\Initialiser.class src\com\zzz\lib\StubRequest.class -r:mscorlib.dll
"C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools\TlbExp.exe" TestZ.dll
C:\Windows\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe /codebase:TestZ.dll
copy /Y TestZ.dll C:\Users\Dev\Documents\Xamarin\TestDLL\ConsoleApplication1\Debug\TestZ.dll
copy /Y TestZ.dll C:\Users\Dev\Documents\Xamarin\TestDLL\ConsoleApplication1\TestZ.dll
copy /Y *.tlb C:\Users\Dev\Documents\Xamarin\TestDLL\ConsoleApplication1\Debug\*.tlb
copy /Y *.tlb C:\Users\Dev\Documents\Xamarin\TestDLL\ConsoleApplication1\*.tlb
--------------------
In the C# project, after importing TestZ.dll and IKVM's OpenJDK dlls...
---------- Program.cs ----------
using com.zzz.lib;
namespace CSTestZ
{
class Program
{
static void Main(string[] args)
{
Initialiser.initialise();
StubRequest stubRequest = new StubRequest();
stubRequest.doSomething();
}
}
}
--------------------
Meanwhile, in the C++ application, I am unable to get anything going.
---------- ConsoleApplication.cpp ----------
#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <string>
#include <windows.h>
#include "tchar.h"
#include "Initialiser.h" //empty file
#import "TestZ.tlb" raw_interfaces_only
using namespace TestZ;
int _tmain(int argc, _TCHAR* argv[])
{
// Initialize COM.
HRESULT hr = CoInitialize(NULL);
// Create the interface pointer.
//InitialiserPtr ptr(__uuidof(Initialiser));
_InitialiserPtr initPtr(__uuidof(Initialiser));
_StubRequestPtr stubPtr(__uuidof(StubRequest));
initPtr->initialise(); //error: class "TestZ::_Initialiser" has no member "initialise"
HRESULT stubRequest = stubPtr.CreateInstance(__uuidof(StubRequest));
//how to use stubRequest into a StubRequest Object?
CoUninitialize();
return 0;
}
--------------------
I see that there are
---------- TestZ.tlh ----------
// 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 11:01:50 - 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("0949a4d9-28bf-356f-a6bb-e2ee73ca0f71"))
/* 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("0949a4d9-28bf-356f-a6bb-e2ee73ca0f71"))
_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;
};
} // namespace TestZ
#pragma pack(pop)
--------------------
Continue reading...