Using ExecuteInDefaultAppDomain to call the main function C# console application

1

Trying to load CLR from a unmanaged native c++ code and then use ExecuteInDefaultAppDomain function to call the function defined in win32 c# console application. Actually i am trying to call the static void Main(string [] args) i.e the entry point function.But on calling ExecuteInDefaultAppDomain as shown in below code it returns back 0x80131513(COR_E_MISSINGMETHOD).Please help me to solve the problem and my concepts are wrong then please guide me for the same.

Please find the c++ code pasted below:

#include "stdafx.h"
#include <metahost.h>
#include <mscoree.h>
#include <assert.h> 
//#include <Windows.h>

//#using <WpfWithoutService.dll>
//using namespace System;
//using namespace System::Threading;

//using namespace WpfWithoutService;

#pragma comment(lib, "mscoree.lib")
/*ref class MainWinClass
{
public:
    void MainForm()
    {
        MainWindow^ mainwin = gcnew MainWindow();
        //mainwin->Activate();
        //mainwin->InitializeComponent();
    }
};*/

int _tmain(int argc, _TCHAR* argv[])
{
    HRESULT hr; 
    //BOOL fLoadable;

    ICLRMetaHost *pMetaHost = NULL;
    LPCWSTR pwzVersion = (LPCWSTR)"v4.0.30319";


    //hr = CLRCreateInstance(CLSID_CLRMetaHost, IID_ICLRMetaHost, (LPVOID*)&pMetaHost); 
    hr = CLRCreateInstance(CLSID_CLRMetaHost, IID_PPV_ARGS(&pMetaHost));
    if (FAILED(hr)) 
    { 
        wprintf(L"CLRCreateInstance failed w/hr 0x%08lx\n", hr); 
    }

    ICLRRuntimeInfo *pRuntimeInfo = NULL; 
// Get the ICLRRuntimeInfo corresponding to a particular CLR version.
    //hr = pMetaHost->GetRuntime(L"v4.0.30319", IID_ICLRRuntimeInfo, (LPVOID *)&pRuntimeInfo); 
    hr = pMetaHost->GetRuntime(L"v4.0.30319", IID_PPV_ARGS(&pRuntimeInfo)); 
    if (FAILED(hr)) 
    { 
        wprintf(L"ICLRMetaHost::GetRuntime failed w/hr 0x%08lx\n", hr); 
        //goto Cleanup; 
    } 


/*Check if the specified runtime can be loaded into the process. This  
// method will take into account other runtimes that may already be  
// loaded into the process and set pbLoadable to TRUE if this runtime can  
// be loaded in an in-process side-by-side fashion.  

    hr = pRuntimeInfo->IsLoadable(&fLoadable); 
    if (FAILED(hr)) 
    { 
        wprintf(L"ICLRRuntimeInfo::IsLoadable failed w/hr 0x%08lx\n", hr); 
        //goto Cleanup; 
    } 


    if (!fLoadable) 
    { 
        wprintf(L".NET runtime %s cannot be loaded\n", "4.0.30319.18063"); 
        //goto Cleanup; 
    }*/


// Load the CLR into the current process and return a runtime interface  pointer. 
    ICLRRuntimeHost *pClrRuntimeHost = NULL;
    hr = pRuntimeInfo->GetInterface(CLSID_CLRRuntimeHost, IID_PPV_ARGS(&pClrRuntimeHost)); 
    if (FAILED(hr)) 
    { 
        wprintf(L"ICLRRuntimeInfo::GetInterface failed w/hr 0x%08lx\n", hr); 
    }


// Start the CLR.
    if (hr == S_OK)
    {
        hr = pClrRuntimeHost->Start(); 
        if (FAILED(hr)) 
        { 
            wprintf(L"CLR failed to start w/hr 0x%08lx\n", hr); 
            //goto Cleanup; 
        }
    }


/*Create a instance of the class declared in c# dll

    //MainWindow^ mainwin = gcnew MainWindow();

    //mainwin->SetupLogFiles("iptcom_debug_WpfWithoutService.txt");

    MainWinClass^ win = gcnew MainWinClass;

    ThreadStart^ threadDelegate = gcnew ThreadStart(win, &MainWinClass::MainForm);
    Thread^ newThread = gcnew Thread(threadDelegate, 0);
    newThread->SetApartmentState(ApartmentState::STA);
    newThread->Start();*/

//Load an assembly and call the required function
    if (hr == S_OK) // if CLR is started successfully
    {
        DWORD dwRet;
        hr = pClrRuntimeHost->ExecuteInDefaultAppDomain(L"D:\\IPTCom\\IPTComServiceTest_Canada\\IPTComServiceTests_rev2\\Unmanaged_App\\ConsoleApplication.exe", L"ConsoleApplication.Program", L"Main",L"", &dwRet); 
        if (FAILED(hr)) 
        { 
            wprintf(L"ExecuteInDefaultAppDomain failed to start w/hr 0x%08lx\n", hr); 
        } 
    }


    if (pMetaHost) 
    { 
        pMetaHost->Release(); 
        pMetaHost = NULL; 
    } 
    if (pRuntimeInfo) 
    { 
        pRuntimeInfo->Release(); 
        pRuntimeInfo = NULL; 
    } 
    if (pClrRuntimeHost) 
    { 
        // Please note that after a call to Stop, the CLR cannot be  
        // reinitialized into the same process. This step is usually not  
        // necessary. You can leave the .NET runtime loaded in your process. 
        //wprintf(L"Stop the .NET runtime\n"); 
        //if (pClrRuntimeHost->Stop() == S_OK) 
        if (1) 
        {
            pClrRuntimeHost->Release(); 
            pClrRuntimeHost = NULL; 
        }
        else
        {
            pClrRuntimeHost->Release(); 
            pClrRuntimeHost = NULL; 
        }
    } 


    return 0;
}
c#
c++
clr
asked on Stack Overflow Aug 7, 2015 by Atul

1 Answer

2

ExecuteInDefaultAppDomain can only call function with the signature:

static int pwzMethodName (String pwzArgument)

Since the program's Main() signature is different the call cannot find your function. You can solve this by wrapping a call to the Main function from a different function:

static int HostingMain(String args)
{
    Main(new string[] { args });
    return 0;
}

Then modify your call on the C code to:

        hr = pClrRuntimeHost->ExecuteInDefaultAppDomain(L"D:\\IPTCom\\IPTComServiceTest_Canada\\IPTComServiceTests_rev2\\Unmanaged_App\\ConsoleApplication.exe", L"ConsoleApplication.Program", L"HostingMain",L"", &dwRet); 
answered on Stack Overflow Dec 12, 2019 by Omer Yair

User contributions licensed under CC BY-SA 3.0