Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/sample/vm/jvm-clr/invoker.cpp
38829 views
/*1* Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6*7* - Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9*10* - Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* - Neither the name of Oracle nor the names of its15* contributors may be used to endorse or promote products derived16* from this software without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS19* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,20* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR21* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR22* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,23* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,24* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR25* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF26* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING27* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS28* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.29*/3031/*32* This source code is provided to illustrate the usage of a given feature33* or technique and has been deliberately simplified. Additional steps34* required for a production-quality application, such as security checks,35* input validation and proper error handling, might not be present in36* this sample code.37*/383940#define _WIN32_WINNT 0x040041#include "windows.h"42#include "Oleauto.h"43#include "stdio.h"44#include "mscoree.h"45#include "corerror.h"46#include "jni.h"47#include "invokerExp.h"48#include "invoker.h"4950#import <mscorlib.tlb> raw_interfaces_only5152using namespace mscorlib;5354// The CLR assembly invocation function5556int __stdcall invokeCLR( WCHAR* wszApplication){5758//Initializes the COM library5960CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);6162ICorRuntimeHost* pHost = NULL;63IUnknown* pAppDomainThunk = NULL;64_AppDomain* pAppDomain = NULL;65long lReturn = 0;6667// Load CLR into the process6869HRESULT hr = CorBindToRuntimeEx(NULL,NULL,0,CLSID_CorRuntimeHost,IID_ICorRuntimeHost,(VOID**)&pHost);7071if(!FAILED(hr)) {7273// Start the CLR7475hr = pHost->Start();76if(!FAILED(hr)) {7778// Get the _AppDomain interface7980hr = pHost->GetDefaultDomain(&pAppDomainThunk);81if(!FAILED(hr)) {8283hr = pAppDomainThunk->QueryInterface(__uuidof(_AppDomain), (void**)&pAppDomain);84if(!FAILED(hr)) {8586// Execute assembly8788hr = pAppDomain->ExecuteAssembly_2(_bstr_t(wszApplication), &lReturn);89if (FAILED(hr)) {9091printf("_AppDomain::ExecuteAssembly_2 failed with hr=0x%x.\n", hr);92lReturn = -1;93}9495}else{96printf("Can't get System::_AppDomain interface\n");97lReturn = -2;98}99100}else{101printf("ICorRuntimeHost->GetDefaultDomain failed with hr=0x%x.\n", hr);102lReturn = -3;103}104}else{105printf("ICorRuntimeHost->Start failed with hr=0x%x.\n", hr);106lReturn = -4;107}108109}else{110printf("CorBindToRuntimeHost failed with hr=0x%x.\n", hr);111lReturn = -5;112}113114// print the error message description if needed115116if(FAILED(hr)){117LPVOID lpMsgBuf = NULL;118119FormatMessage(120FORMAT_MESSAGE_ALLOCATE_BUFFER |121FORMAT_MESSAGE_FROM_SYSTEM |122FORMAT_MESSAGE_IGNORE_INSERTS,123NULL,124hr,125MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),126(LPTSTR) &lpMsgBuf,1270,128NULL );129if(lpMsgBuf != NULL)130printf("Message:%s\n",lpMsgBuf);131else132printf("No translation of 0x%x\n",hr);133}134135// close COM library136137CoUninitialize();138139return lReturn;140}141142// Wrapper function that allows to ASCIZ string to provide the assemble path143144int __stdcall invokeCLR( const char* szApplication){145146int nLength = strlen(szApplication)+1;147148WCHAR* wszApplication = new WCHAR[nLength];149150mbstowcs(wszApplication, szApplication, nLength);151152int nReturn = invokeCLR( wszApplication);153154delete[] wszApplication;155156return nReturn;157}158159// native method enter-point160161JNIEXPORT jint JNICALL Java_invoker_invokeCLR( JNIEnv* pEnv,162jclass pClass,163jstring jsApplication) {164165const char* szApplication = pEnv->GetStringUTFChars(jsApplication, NULL);166167int nResult = invokeCLR( szApplication);168169pEnv->ReleaseStringUTFChars(jsApplication,szApplication);170171return nResult;172}173174175