Example #1
0
int coreclr_execute_assembly(
            void* hostHandle,
            unsigned int domainId,
            int argc,
            const char** argv,
            const char* managedAssemblyPath,
            unsigned int* exitCode)
{
    if (exitCode == NULL)
    {
        return HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER);
    }
    *exitCode = -1;

    ICLRRuntimeHost2* host = reinterpret_cast<ICLRRuntimeHost2*>(hostHandle);

    ConstWStringArrayHolder argvW;
    argvW.Set(StringArrayToUnicode(argc, argv), argc);
    
    ConstWStringHolder managedAssemblyPathW = StringToUnicode(managedAssemblyPath);

    HRESULT hr = host->ExecuteAssembly(domainId, managedAssemblyPathW, argc, argvW, (DWORD *)exitCode);
    IfFailRet(hr);

    return hr;
}
Example #2
0
HRESULT ExecuteAssembly(
            LPCSTR exePath,
            LPCSTR coreClrPath,
            LPCSTR appDomainFriendlyName,
            int propertyCount,
            LPCSTR* propertyKeys,
            LPCSTR* propertyValues,
            int argc,
            LPCSTR* argv,
            LPCSTR managedAssemblyPath,
            LPCSTR entryPointAssemblyName,
            LPCSTR entryPointTypeName,
            LPCSTR entryPointMethodName,
            DWORD* exitCode)
{
    if (exitCode == NULL)
    {
        return HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER);
    }
    *exitCode = -1;

    DWORD error = PAL_InitializeCoreCLR(exePath, coreClrPath, true);
    HRESULT hr = HRESULT_FROM_WIN32(error);

    // If PAL initialization failed, then we should return right away and avoid
    // calling any other APIs because they can end up calling into the PAL layer again.
    if (FAILED(hr))
    {
        return hr;
    }

    ReleaseHolder<ICLRRuntimeHost2> host;

    hr = CorHost2::CreateObject(IID_ICLRRuntimeHost2, (void**)&host);
    IfFailRet(hr);

    hr = host->SetStartupFlags((STARTUP_FLAGS)
                               (STARTUP_FLAGS::STARTUP_LOADER_OPTIMIZATION_SINGLE_DOMAIN |
                                STARTUP_FLAGS::STARTUP_SINGLE_APPDOMAIN));
    IfFailRet(hr);

    hr = host->Start();
    IfFailRet(hr);
    
    ConstWStringHolder appDomainFriendlyNameW = StringToUnicode(appDomainFriendlyName);
    
    ConstWStringArrayHolder propertyKeysW;
    propertyKeysW.Set(StringArrayToUnicode(propertyCount, propertyKeys), propertyCount);
    
    ConstWStringArrayHolder propertyValuesW;
    propertyValuesW.Set(StringArrayToUnicode(propertyCount, propertyValues), propertyCount);

    DWORD domainId;

    hr = host->CreateAppDomainWithManager(
        appDomainFriendlyNameW,
        // Flags:
        // APPDOMAIN_ENABLE_PLATFORM_SPECIFIC_APPS
        // - By default CoreCLR only allows platform neutral assembly to be run. To allow
        //   assemblies marked as platform specific, include this flag
        //
        // APPDOMAIN_ENABLE_PINVOKE_AND_CLASSIC_COMINTEROP
        // - Allows sandboxed applications to make P/Invoke calls and use COM interop
        //
        // APPDOMAIN_SECURITY_SANDBOXED
        // - Enables sandboxing. If not set, the app is considered full trust
        //
        // APPDOMAIN_IGNORE_UNHANDLED_EXCEPTION
        // - Prevents the application from being torn down if a managed exception is unhandled
        //
        APPDOMAIN_ENABLE_PLATFORM_SPECIFIC_APPS |
        APPDOMAIN_ENABLE_PINVOKE_AND_CLASSIC_COMINTEROP |
        APPDOMAIN_DISABLE_TRANSPARENCY_ENFORCEMENT,
        NULL,                    // Name of the assembly that contains the AppDomainManager implementation
        NULL,                    // The AppDomainManager implementation type name
        propertyCount,
        propertyKeysW,
        propertyValuesW,
        &domainId);
    IfFailRet(hr);

    ConstWStringArrayHolder argvW;
    argvW.Set(StringArrayToUnicode(argc, argv), argc);
    
    if (entryPointAssemblyName == NULL || entryPointTypeName == NULL || entryPointMethodName == NULL)
    {
        ConstWStringHolder managedAssemblyPathW = StringToUnicode(managedAssemblyPath);

        hr = host->ExecuteAssembly(domainId, managedAssemblyPathW, argc, argvW, exitCode);
        IfFailRet(hr);
    }
    else
    {
        ConstWStringHolder entryPointAssemblyNameW = StringToUnicode(entryPointAssemblyName);
        ConstWStringHolder entryPointTypeNameW = StringToUnicode(entryPointTypeName);
        ConstWStringHolder entryPointMethodNameW = StringToUnicode(entryPointMethodName);

        HostMain pHostMain;

        hr = host->CreateDelegate(
            domainId,
            entryPointAssemblyNameW,
            entryPointTypeNameW,
            entryPointMethodNameW,
            (INT_PTR*)&pHostMain);
        
        IfFailRet(hr);

        *exitCode = pHostMain(argc, argvW);
    }
    
    hr = host->UnloadAppDomain(domainId,
                               true); // Wait until done
    IfFailRet(hr);

    hr = host->Stop();

    // The PAL_Terminate is not called here since it would terminate the current process.

    return hr;
}
Example #3
0
HRESULT ExecuteAssembly(
            LPCSTR exePath,
            LPCSTR coreClrPath,
            LPCSTR appDomainFriendlyName,
            int propertyCount,
            LPCSTR* propertyKeys,
            LPCSTR* propertyValues,
            int argc,
            LPCSTR* argv,
            LPCSTR managedAssemblyPath,
            LPCSTR entryPointAssemblyName,
            LPCSTR entryPointTypeName,
            LPCSTR entryPointMethodName,
            DWORD* exitCode)
{
    if (exitCode == NULL)
    {
        return HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER);
    }
    *exitCode = -1;

    ReleaseHolder<ICLRRuntimeHost2> host; //(reinterpret_cast<ICLRRuntimeHost2*>(hostHandle));
    DWORD domainId;

    HRESULT hr = coreclr_initialize(exePath, appDomainFriendlyName, propertyCount, propertyKeys, propertyValues, &host, &domainId);
    IfFailRet(hr);

    ConstWStringArrayHolder argvW;
    argvW.Set(StringArrayToUnicode(argc, argv), argc);
    
    if (entryPointAssemblyName == NULL || entryPointTypeName == NULL || entryPointMethodName == NULL)
    {
        ConstWStringHolder managedAssemblyPathW = StringToUnicode(managedAssemblyPath);

        hr = host->ExecuteAssembly(domainId, managedAssemblyPathW, argc, argvW, exitCode);
        IfFailRet(hr);
    }
    else
    {
        ConstWStringHolder entryPointAssemblyNameW = StringToUnicode(entryPointAssemblyName);
        ConstWStringHolder entryPointTypeNameW = StringToUnicode(entryPointTypeName);
        ConstWStringHolder entryPointMethodNameW = StringToUnicode(entryPointMethodName);

        HostMain pHostMain;

        hr = host->CreateDelegate(
            domainId,
            entryPointAssemblyNameW,
            entryPointTypeNameW,
            entryPointMethodNameW,
            (INT_PTR*)&pHostMain);
        
        IfFailRet(hr);

        *exitCode = pHostMain(argc, argvW);
    }

    hr = coreclr_shutdown(host, domainId);

    return hr;
}