0N/A/*
2362N/A * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
0N/A *
0N/A * Redistribution and use in source and binary forms, with or without
0N/A * modification, are permitted provided that the following conditions
0N/A * are met:
0N/A *
0N/A * - Redistributions of source code must retain the above copyright
0N/A * notice, this list of conditions and the following disclaimer.
0N/A *
0N/A * - Redistributions in binary form must reproduce the above copyright
0N/A * notice, this list of conditions and the following disclaimer in the
0N/A * documentation and/or other materials provided with the distribution.
0N/A *
2362N/A * - Neither the name of Oracle nor the names of its
0N/A * contributors may be used to endorse or promote products derived
0N/A * from this software without specific prior written permission.
0N/A *
0N/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
0N/A * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
0N/A * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
0N/A * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
0N/A * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
0N/A * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
0N/A * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
0N/A * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
0N/A * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
0N/A * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
0N/A * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0N/A */
0N/A
4378N/A/*
4378N/A * This source code is provided to illustrate the usage of a given feature
4378N/A * or technique and has been deliberately simplified. Additional steps
4378N/A * required for a production-quality application, such as security checks,
4378N/A * input validation and proper error handling, might not be present in
4378N/A * this sample code.
4378N/A */
4378N/A
4378N/A
0N/A#define _WIN32_WINNT 0x0400
0N/A#include "windows.h"
0N/A#include "Oleauto.h"
0N/A#include "stdio.h"
0N/A#include "mscoree.h"
0N/A#include "corerror.h"
0N/A#include "jni.h"
0N/A#include "invokerExp.h"
0N/A#include "invoker.h"
0N/A
0N/A#import <mscorlib.tlb> raw_interfaces_only
0N/A
0N/Ausing namespace mscorlib;
0N/A
0N/A// The CLR assembly invocation function
0N/A
0N/Aint __stdcall invokeCLR( WCHAR* wszApplication){
0N/A
0N/A //Initializes the COM library
0N/A
0N/A CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
0N/A
0N/A ICorRuntimeHost* pHost = NULL;
0N/A IUnknown* pAppDomainThunk = NULL;
0N/A _AppDomain* pAppDomain = NULL;
0N/A long lReturn = 0;
0N/A
0N/A // Load CLR into the process
0N/A
0N/A HRESULT hr = CorBindToRuntimeEx(NULL,NULL,0,CLSID_CorRuntimeHost,IID_ICorRuntimeHost,(VOID**)&pHost);
0N/A
0N/A if(!FAILED(hr)) {
0N/A
0N/A // Start the CLR
0N/A
0N/A hr = pHost->Start();
0N/A if(!FAILED(hr)) {
0N/A
0N/A // Get the _AppDomain interface
0N/A
0N/A hr = pHost->GetDefaultDomain(&pAppDomainThunk);
0N/A if(!FAILED(hr)) {
0N/A
0N/A hr = pAppDomainThunk->QueryInterface(__uuidof(_AppDomain), (void**)&pAppDomain);
0N/A if(!FAILED(hr)) {
0N/A
0N/A // Execute assembly
0N/A
0N/A hr = pAppDomain->ExecuteAssembly_2(_bstr_t(wszApplication), &lReturn);
0N/A if (FAILED(hr)) {
0N/A
0N/A printf("_AppDomain::ExecuteAssembly_2 failed with hr=0x%x.\n", hr);
0N/A lReturn = -1;
0N/A }
0N/A
0N/A }else{
0N/A printf("Can't get System::_AppDomain interface\n");
0N/A lReturn = -2;
0N/A }
0N/A
0N/A }else{
0N/A printf("ICorRuntimeHost->GetDefaultDomain failed with hr=0x%x.\n", hr);
0N/A lReturn = -3;
0N/A }
0N/A }else{
0N/A printf("ICorRuntimeHost->Start failed with hr=0x%x.\n", hr);
0N/A lReturn = -4;
0N/A }
0N/A
0N/A }else{
0N/A printf("CorBindToRuntimeHost failed with hr=0x%x.\n", hr);
0N/A lReturn = -5;
0N/A }
0N/A
0N/A // print the error message description if needed
0N/A
0N/A if(FAILED(hr)){
0N/A LPVOID lpMsgBuf = NULL;
0N/A
0N/A FormatMessage(
0N/A FORMAT_MESSAGE_ALLOCATE_BUFFER |
0N/A FORMAT_MESSAGE_FROM_SYSTEM |
0N/A FORMAT_MESSAGE_IGNORE_INSERTS,
0N/A NULL,
0N/A hr,
0N/A MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
0N/A (LPTSTR) &lpMsgBuf,
0N/A 0,
0N/A NULL );
0N/A if(lpMsgBuf != NULL)
0N/A printf("Message:%s\n",lpMsgBuf);
0N/A else
0N/A printf("No translation of 0x%x\n",hr);
0N/A }
0N/A
0N/A // close COM library
0N/A
0N/A CoUninitialize();
0N/A
0N/A return lReturn;
0N/A}
0N/A
0N/A// Wrapper function that allows to ASCIZ string to provide the assemble path
0N/A
0N/Aint __stdcall invokeCLR( const char* szApplication){
0N/A
0N/A int nLength = strlen(szApplication)+1;
0N/A
0N/A WCHAR* wszApplication = new WCHAR[nLength];
0N/A
0N/A mbstowcs(wszApplication, szApplication, nLength);
0N/A
0N/A int nReturn = invokeCLR( wszApplication);
0N/A
0N/A delete wszApplication;
0N/A
0N/A return nReturn;
0N/A}
0N/A
0N/A// native method enter-point
0N/A
0N/AJNIEXPORT jint JNICALL Java_invoker_invokeCLR( JNIEnv* pEnv,
0N/A jclass pClass,
0N/A jstring jsApplication) {
0N/A
0N/A const char* szApplication = pEnv->GetStringUTFChars(jsApplication, NULL);
0N/A
0N/A int nResult = invokeCLR( szApplication);
0N/A
0N/A pEnv->ReleaseStringUTFChars(jsApplication,szApplication);
0N/A
0N/A return nResult;
0N/A}