可以! 使用C++调用托管的代码,可以使用CLR via C#里边现成的例子,代码如下: #include <Windows.h> #include <MSCorEE.h> #include <stdio.h> void main(int argc, WCHAR **argv) { // Load the CLR ICLRRuntimeHost *pClrHost; HRESULT hr = CorBindToRuntimeEx( NULL, // desired CLR version (NULL=latest) NULL, // desired GC flavor (NULL=workstation) 0, // desired startup flags CLSID_CLRRuntimeHost, // CLSID of CLR IID_ICLRRuntimeHost, // IID of ICLRRuntimeHost (PVOID*) &pClrHost); // returned COM interface // (This is where you would set Host managers) // (This is where you could get CLR managers) // Initialize and start the CLR pClrHost->Start(); // Load an assembly and call a static method that // takes a String and returns an Int32 DWORD retVal; hr = pClrHost->ExecuteInDefaultAppDomain( L"SomeMgdAssem.dll", L"Wintellect.SomeType", L"SomeMethod", L"Jeff", &retVal); // Show the result returned from managed code wprintf(L"Managed code returned %d", retVal); // Terminate this process (destroying the CLR loaded in it) } 你在使用的时候,只要把c++里边的全局变量传到调用方法里边就可以了。 MSDN里边应该有详细的说明。
使用C++调用托管的代码,可以使用CLR via C#里边现成的例子,代码如下:
#include <Windows.h>
#include <MSCorEE.h>
#include <stdio.h>
void main(int argc, WCHAR **argv) {
// Load the CLR
ICLRRuntimeHost *pClrHost;
HRESULT hr = CorBindToRuntimeEx(
NULL, // desired CLR version (NULL=latest)
NULL, // desired GC flavor (NULL=workstation)
0, // desired startup flags
CLSID_CLRRuntimeHost, // CLSID of CLR
IID_ICLRRuntimeHost, // IID of ICLRRuntimeHost
(PVOID*) &pClrHost); // returned COM interface
// (This is where you would set Host managers)
// (This is where you could get CLR managers)
// Initialize and start the CLR
pClrHost->Start();
// Load an assembly and call a static method that
// takes a String and returns an Int32
DWORD retVal;
hr = pClrHost->ExecuteInDefaultAppDomain(
L"SomeMgdAssem.dll",
L"Wintellect.SomeType", L"SomeMethod", L"Jeff", &retVal);
// Show the result returned from managed code
wprintf(L"Managed code returned %d", retVal);
// Terminate this process (destroying the CLR loaded in it)
}
你在使用的时候,只要把c++里边的全局变量传到调用方法里边就可以了。
MSDN里边应该有详细的说明。