Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/sample/vm/jvm-clr/invoker.cpp
38829 views
1
/*
2
* Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved.
3
*
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions
6
* are met:
7
*
8
* - Redistributions of source code must retain the above copyright
9
* notice, this list of conditions and the following disclaimer.
10
*
11
* - Redistributions in binary form must reproduce the above copyright
12
* notice, this list of conditions and the following disclaimer in the
13
* documentation and/or other materials provided with the distribution.
14
*
15
* - Neither the name of Oracle nor the names of its
16
* contributors may be used to endorse or promote products derived
17
* from this software without specific prior written permission.
18
*
19
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
*/
31
32
/*
33
* This source code is provided to illustrate the usage of a given feature
34
* or technique and has been deliberately simplified. Additional steps
35
* required for a production-quality application, such as security checks,
36
* input validation and proper error handling, might not be present in
37
* this sample code.
38
*/
39
40
41
#define _WIN32_WINNT 0x0400
42
#include "windows.h"
43
#include "Oleauto.h"
44
#include "stdio.h"
45
#include "mscoree.h"
46
#include "corerror.h"
47
#include "jni.h"
48
#include "invokerExp.h"
49
#include "invoker.h"
50
51
#import <mscorlib.tlb> raw_interfaces_only
52
53
using namespace mscorlib;
54
55
// The CLR assembly invocation function
56
57
int __stdcall invokeCLR( WCHAR* wszApplication){
58
59
//Initializes the COM library
60
61
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
62
63
ICorRuntimeHost* pHost = NULL;
64
IUnknown* pAppDomainThunk = NULL;
65
_AppDomain* pAppDomain = NULL;
66
long lReturn = 0;
67
68
// Load CLR into the process
69
70
HRESULT hr = CorBindToRuntimeEx(NULL,NULL,0,CLSID_CorRuntimeHost,IID_ICorRuntimeHost,(VOID**)&pHost);
71
72
if(!FAILED(hr)) {
73
74
// Start the CLR
75
76
hr = pHost->Start();
77
if(!FAILED(hr)) {
78
79
// Get the _AppDomain interface
80
81
hr = pHost->GetDefaultDomain(&pAppDomainThunk);
82
if(!FAILED(hr)) {
83
84
hr = pAppDomainThunk->QueryInterface(__uuidof(_AppDomain), (void**)&pAppDomain);
85
if(!FAILED(hr)) {
86
87
// Execute assembly
88
89
hr = pAppDomain->ExecuteAssembly_2(_bstr_t(wszApplication), &lReturn);
90
if (FAILED(hr)) {
91
92
printf("_AppDomain::ExecuteAssembly_2 failed with hr=0x%x.\n", hr);
93
lReturn = -1;
94
}
95
96
}else{
97
printf("Can't get System::_AppDomain interface\n");
98
lReturn = -2;
99
}
100
101
}else{
102
printf("ICorRuntimeHost->GetDefaultDomain failed with hr=0x%x.\n", hr);
103
lReturn = -3;
104
}
105
}else{
106
printf("ICorRuntimeHost->Start failed with hr=0x%x.\n", hr);
107
lReturn = -4;
108
}
109
110
}else{
111
printf("CorBindToRuntimeHost failed with hr=0x%x.\n", hr);
112
lReturn = -5;
113
}
114
115
// print the error message description if needed
116
117
if(FAILED(hr)){
118
LPVOID lpMsgBuf = NULL;
119
120
FormatMessage(
121
FORMAT_MESSAGE_ALLOCATE_BUFFER |
122
FORMAT_MESSAGE_FROM_SYSTEM |
123
FORMAT_MESSAGE_IGNORE_INSERTS,
124
NULL,
125
hr,
126
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
127
(LPTSTR) &lpMsgBuf,
128
0,
129
NULL );
130
if(lpMsgBuf != NULL)
131
printf("Message:%s\n",lpMsgBuf);
132
else
133
printf("No translation of 0x%x\n",hr);
134
}
135
136
// close COM library
137
138
CoUninitialize();
139
140
return lReturn;
141
}
142
143
// Wrapper function that allows to ASCIZ string to provide the assemble path
144
145
int __stdcall invokeCLR( const char* szApplication){
146
147
int nLength = strlen(szApplication)+1;
148
149
WCHAR* wszApplication = new WCHAR[nLength];
150
151
mbstowcs(wszApplication, szApplication, nLength);
152
153
int nReturn = invokeCLR( wszApplication);
154
155
delete[] wszApplication;
156
157
return nReturn;
158
}
159
160
// native method enter-point
161
162
JNIEXPORT jint JNICALL Java_invoker_invokeCLR( JNIEnv* pEnv,
163
jclass pClass,
164
jstring jsApplication) {
165
166
const char* szApplication = pEnv->GetStringUTFChars(jsApplication, NULL);
167
168
int nResult = invokeCLR( szApplication);
169
170
pEnv->ReleaseStringUTFChars(jsApplication,szApplication);
171
172
return nResult;
173
}
174
175