使用ExecuteInDefaultAppDomain调用主函数C#控制台应用程序

使用ExecuteInDefaultAppDomain调用主函数C#控制台应用程序,c#,c++,clr,C#,C++,Clr,尝试从非托管本机C++代码中加载CLR,然后使用ExcUnEdTurnTrpAppDealEngress函数调用Win32 C·L控制台应用程序中定义的函数。 实际上,我正在尝试调用静态void Main(string[]args),即入口点函数。但在调用ExecuteInDefaultAppDomain时,如下面的代码所示,它返回0x80131513(COR_e_MISSINGMETHOD)。请帮助我解决问题,我的概念是错误的,然后请指导我 请找到下面粘贴的C++代码:< /P> #inclu

尝试从非托管本机C++代码中加载CLR,然后使用ExcUnEdTurnTrpAppDealEngress函数调用Win32 C·L控制台应用程序中定义的函数。 实际上,我正在尝试调用静态void Main(string[]args),即入口点函数。但在调用ExecuteInDefaultAppDomain时,如下面的代码所示,它返回0x80131513(COR_e_MISSINGMETHOD)。请帮助我解决问题,我的概念是错误的,然后请指导我

请找到下面粘贴的C++代码:< /P>
#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;
}
#包括“stdafx.h”
#包括
#包括
#包括
//#包括
//#使用
//使用名称空间系统;
//使用名称空间系统::线程;
//使用名称空间WpfWithoutService;
#pragma注释(lib,“mscoree.lib”)
/*ref类主WinClass
{
公众:
void MainForm()
{
MainWindow ^mainwin=gcnew MainWindow();
//mainwin->Activate();
//mainwin->InitializeComponent();
}
};*/
int _tmain(int argc,_TCHAR*argv[]
{
HRESULT-hr;
//布尔可浮;
ICLRMetaHost*pMetaHost=NULL;
LPCWSTR pwzVersion=(LPCWSTR)“v4.0.30319”;
//hr=CLRCreateInstance(CLSID\u CLRMETHOST、IID\u ICLRMetaHost(LPVOID*)和PMETHOST);
hr=CLRCreateInstance(CLSID_CLRMetaHost,IID_PPV_ARGS(&pMetaHost));
如果(失败(小时))
{ 
wprintf(L“CLRCreateInstance失败w/hr 0x%08lx\n”,hr);
}
ICLRRuntimeInfo*pRuntimeInfo=NULL;
//获取对应于特定CLR版本的ICLRRuntimeInfo。
//hr=pMetaHost->GetRuntime(L“v4.0.30319”,IID_iclruntimeinfo,(LPVOID*)和pRuntimeInfo);
hr=pMetaHost->GetRuntime(L“v4.0.30319”,IID_PPV_参数(&pRuntimeInfo));
如果(失败(小时))
{ 
wprintf(L“ICLRMetaHost::GetRuntime失败w/hr 0x%08lx\n”,hr);
//去清理;
} 
/*检查是否可以将指定的运行时加载到进程中。此
//方法将考虑可能已经存在的其他运行时
//加载到进程中,并将pbLoadable设置为TRUE(如果此运行时可以)
//以过程中并排方式加载。
hr=pRuntimeInfo->IsLoadable(&fLoadable);
如果(失败(小时))
{ 
wprintf(L“ICLRRuntimeInfo::IsLoadable failed w/hr 0x%08lx\n”,hr);
//去清理;
} 
如果(!可浮动)
{ 
无法加载wprintf(L“.NET运行时%s”\n“4.0.30319.18063”);
//去清理;
}*/
//将CLR加载到当前进程并返回运行时接口指针。
ICLRRuntimeHost*pClrRuntimeHost=NULL;
hr=pRuntimeInfo->GetInterface(CLSID_CLRRuntimeHost,IID_PPV_ARGS(&pClrRuntimeHost));
如果(失败(小时))
{ 
wprintf(L“ICLRRuntimeInfo::GetInterface失败w/hr 0x%08lx\n”,hr);
}
//启动CLR。
如果(hr==S_正常)
{
hr=pClrRuntimeHost->Start();
如果(失败(小时))
{ 
wprintf(L“CLR无法启动w/hr 0x%08lx\n”,hr);
//去清理;
}
}
/*创建在c#dll中声明的类的实例
//MainWindow ^mainwin=gcnew MainWindow();
//mainwin->SetupLogFiles(“iptcom_debug_WpfWithoutService.txt”);
MainWinClass ^win=gcnew MainWinClass;
ThreadStart ^threadDelegate=gcnewthreadstart(win,&MainWinClass::MainForm);
线程^newThread=gcnewthread(threadDelegate,0);
newThread->SetApartmentState(ApartmentState::STA);
newThread->Start()*/
//加载程序集并调用所需的函数
if(hr==S_OK)//如果CLR成功启动
{
德沃德·德雷特;
hr=pClrRuntimeHost->ExecuteInDefaultAppDomain(L“D:\\IPTCom\\IPTComServiceTest\u Canada\\IPTComServiceTests\u rev2\\Unmanaged\\u App\\ConsoleApplication.exe”、L“ConsoleApplication.Program”、L“Main”、L“和dwRet);
如果(失败(小时))
{ 
wprintf(L“ExecuteInDefaultAppDomain无法启动w/hr 0x%08lx\n”,hr);
} 
}
如果(pMetaHost)
{ 
pMetaHost->Release();
pMetaHost=NULL;
} 
if(pRuntimeInfo)
{ 
pRuntimeInfo->Release();
pRuntimeInfo=NULL;
} 
if(pClrRuntimeHost)
{ 
//请注意,在调用Stop之后,CLR不能被调用
//重新初始化到同一进程中。此步骤通常不可用
//必要。您可以在进程中保留加载的.NET运行时。
//wprintf(L“停止.NET运行时\n”);
//如果(pClrRuntimeHost->Stop()==S_OK)
如果(1)
{
pClrRuntimeHost->Release();
pClrRuntimeHost=NULL;
}
其他的
{
pClrRuntimeHost->Release();
pClrRuntimeHost=NULL;
}
} 
返回0;
}

ExecuteInDefaultAppDomain只能调用具有以下签名的函数:

static int pwzMethodName (String pwzArgument)
由于程序的Main()签名不同,因此调用无法找到您的函数。您可以通过从其他函数包装对主函数的调用来解决此问题:

static int HostingMain(String args)
{
    Main(new string[] { args });
    return 0;
}
然后将对C代码的调用修改为:

        hr = pClrRuntimeHost->ExecuteInDefaultAppDomain(L"D:\\IPTCom\\IPTComServiceTest_Canada\\IPTComServiceTests_rev2\\Unmanaged_App\\ConsoleApplication.exe", L"ConsoleApplication.Program", L"HostingMain",L"", &dwRet);