blob: 7b0c7e0138c32a8bc227163257b8f5cf8ad2b77a [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2006 Sun Microsystems, Inc. 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 Sun Microsystems 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#define _WIN32_WINNT 0x0400
33#include "windows.h"
34#include "Oleauto.h"
35#include "stdio.h"
36#include "mscoree.h"
37#include "corerror.h"
38#include "jni.h"
39#include "invokerExp.h"
40#include "invoker.h"
41
42#import <mscorlib.tlb> raw_interfaces_only
43
44using namespace mscorlib;
45
46// The CLR assembly invocation function
47
48int __stdcall invokeCLR( WCHAR* wszApplication){
49
50 //Initializes the COM library
51
52 CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
53
54 ICorRuntimeHost* pHost = NULL;
55 IUnknown* pAppDomainThunk = NULL;
56 _AppDomain* pAppDomain = NULL;
57 long lReturn = 0;
58
59 // Load CLR into the process
60
61 HRESULT hr = CorBindToRuntimeEx(NULL,NULL,0,CLSID_CorRuntimeHost,IID_ICorRuntimeHost,(VOID**)&pHost);
62
63 if(!FAILED(hr)) {
64
65 // Start the CLR
66
67 hr = pHost->Start();
68 if(!FAILED(hr)) {
69
70 // Get the _AppDomain interface
71
72 hr = pHost->GetDefaultDomain(&pAppDomainThunk);
73 if(!FAILED(hr)) {
74
75 hr = pAppDomainThunk->QueryInterface(__uuidof(_AppDomain), (void**)&pAppDomain);
76 if(!FAILED(hr)) {
77
78 // Execute assembly
79
80 hr = pAppDomain->ExecuteAssembly_2(_bstr_t(wszApplication), &lReturn);
81 if (FAILED(hr)) {
82
83 printf("_AppDomain::ExecuteAssembly_2 failed with hr=0x%x.\n", hr);
84 lReturn = -1;
85 }
86
87 }else{
88 printf("Can't get System::_AppDomain interface\n");
89 lReturn = -2;
90 }
91
92 }else{
93 printf("ICorRuntimeHost->GetDefaultDomain failed with hr=0x%x.\n", hr);
94 lReturn = -3;
95 }
96 }else{
97 printf("ICorRuntimeHost->Start failed with hr=0x%x.\n", hr);
98 lReturn = -4;
99 }
100
101 }else{
102 printf("CorBindToRuntimeHost failed with hr=0x%x.\n", hr);
103 lReturn = -5;
104 }
105
106 // print the error message description if needed
107
108 if(FAILED(hr)){
109 LPVOID lpMsgBuf = NULL;
110
111 FormatMessage(
112 FORMAT_MESSAGE_ALLOCATE_BUFFER |
113 FORMAT_MESSAGE_FROM_SYSTEM |
114 FORMAT_MESSAGE_IGNORE_INSERTS,
115 NULL,
116 hr,
117 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
118 (LPTSTR) &lpMsgBuf,
119 0,
120 NULL );
121 if(lpMsgBuf != NULL)
122 printf("Message:%s\n",lpMsgBuf);
123 else
124 printf("No translation of 0x%x\n",hr);
125 }
126
127 // close COM library
128
129 CoUninitialize();
130
131 return lReturn;
132}
133
134// Wrapper function that allows to ASCIZ string to provide the assemble path
135
136int __stdcall invokeCLR( const char* szApplication){
137
138 int nLength = strlen(szApplication)+1;
139
140 WCHAR* wszApplication = new WCHAR[nLength];
141
142 mbstowcs(wszApplication, szApplication, nLength);
143
144 int nReturn = invokeCLR( wszApplication);
145
146 delete wszApplication;
147
148 return nReturn;
149}
150
151// native method enter-point
152
153JNIEXPORT jint JNICALL Java_invoker_invokeCLR( JNIEnv* pEnv,
154 jclass pClass,
155 jstring jsApplication) {
156
157 const char* szApplication = pEnv->GetStringUTFChars(jsApplication, NULL);
158
159 int nResult = invokeCLR( szApplication);
160
161 pEnv->ReleaseStringUTFChars(jsApplication,szApplication);
162
163 return nResult;
164}