非托管C++访问 C#编写的DLL (备忘)
2010-11-30 16:16
253 查看
整理:
对于使用 .NET Framework 编写的App,装载CLR是完全透明的。如果将托管代码编译为 .exe程序,则在运行 .exe 时,mscoree.dll 会自动启动运行时。但是,非托管应用程序也可通过装载CLR受益。运行时为扩展应用程序(如 Microsoft IIS 和 Microsoft SQL Server 2005)提供了框架。
.NET Framework App无论是通过托管 .exe 程序集自动调用的,还是使用非托管宿主 API 加载的,都需要一段称为运行时主机的代码。运行时主机会将运行时加载到进程中,在进程中创建应用程序域,然后在这些应用程序域内加载和执行用户代码。
#include <mscoree.h>
#pragma comment(lib,"mscoree.lib")
void CTestInvokeCSdllDlg::OnBnClickedButton1()
{
// TODO: Add your control notification handler code here
ICLRRuntimeHost *pClrHost;
HRESULT hr = CorBindToRuntimeEx(NULL,
NULL,0,
CLSID_CLRRuntimeHost,
IID_ICLRRuntimeHost,
(PVOID*)&pClrHost);
//对于 1.0 和 1.1 版本,请使用 CLSID_CorRuntimeHost 和 IID_ICorRuntimeHost 来获取 ICorRuntimeHost 接口。
//启动CLR
pClrHost->Start();
DWORD retVal=0;
hr = pClrHost->ExecuteInDefaultAppDomain(L"SPWSInvokeDll.dll",L"SPWSInvokeDll.Class1",L"TestMethod",
L"TestStringParam",&retVal);
CString strRetVal;
strRetVal.Format(L"%d", retVal);
if(S_OK==hr)
AfxMessageBox(strRetVal);
else
AfxMessageBox(L"error");
}
这里有个缺陷,似乎不能将CLR unload,ICLRRuntimeHost提供的stop函数没有解决任何问题——参考(2)。
参考:
(1) http://msdn.microsoft.com/zh-cn/vstudio/9x0wh2z3(VS.90).aspx
(2) http://social.msdn.microsoft.com/Forums/en-NZ/netfxtoolsdev/thread/3e51f21e-ee4b-4a72-812e-ef6b4c3bb0dc
对于使用 .NET Framework 编写的App,装载CLR是完全透明的。如果将托管代码编译为 .exe程序,则在运行 .exe 时,mscoree.dll 会自动启动运行时。但是,非托管应用程序也可通过装载CLR受益。运行时为扩展应用程序(如 Microsoft IIS 和 Microsoft SQL Server 2005)提供了框架。
.NET Framework App无论是通过托管 .exe 程序集自动调用的,还是使用非托管宿主 API 加载的,都需要一段称为运行时主机的代码。运行时主机会将运行时加载到进程中,在进程中创建应用程序域,然后在这些应用程序域内加载和执行用户代码。
#include <mscoree.h>
#pragma comment(lib,"mscoree.lib")
void CTestInvokeCSdllDlg::OnBnClickedButton1()
{
// TODO: Add your control notification handler code here
ICLRRuntimeHost *pClrHost;
HRESULT hr = CorBindToRuntimeEx(NULL,
NULL,0,
CLSID_CLRRuntimeHost,
IID_ICLRRuntimeHost,
(PVOID*)&pClrHost);
//对于 1.0 和 1.1 版本,请使用 CLSID_CorRuntimeHost 和 IID_ICorRuntimeHost 来获取 ICorRuntimeHost 接口。
//启动CLR
pClrHost->Start();
DWORD retVal=0;
hr = pClrHost->ExecuteInDefaultAppDomain(L"SPWSInvokeDll.dll",L"SPWSInvokeDll.Class1",L"TestMethod",
L"TestStringParam",&retVal);
CString strRetVal;
strRetVal.Format(L"%d", retVal);
if(S_OK==hr)
AfxMessageBox(strRetVal);
else
AfxMessageBox(L"error");
}
这里有个缺陷,似乎不能将CLR unload,ICLRRuntimeHost提供的stop函数没有解决任何问题——参考(2)。
参考:
(1) http://msdn.microsoft.com/zh-cn/vstudio/9x0wh2z3(VS.90).aspx
(2) http://social.msdn.microsoft.com/Forums/en-NZ/netfxtoolsdev/thread/3e51f21e-ee4b-4a72-812e-ef6b4c3bb0dc
相关文章推荐
- VS2010使用C#调用C++编写的dll,在其他电脑上无法运行的解决办法
- C# 访问 C DLL
- C#编写的键盘记录程序备忘
- 使用OPCDAAuto.dll编写C# OPC采集程序
- 在C#.net中调用VC 6.0编写的DLL文件
- [C#]DLL继承IConfigurationSectionHandler即可访问宿主的.exe.config
- 在VS2010上使用C#调用非托管C++生成的DLL文件
- c#编写的高并发数据库控制访问代码
- c#编写ARP欺骗,无法加载 DLL“wpcap”: 找不到指定的模块。loadlibrary("wpcap.dll")失败-找不到指定的模块
- [C#]DLL继承IConfigurationSectionHandler即可访问宿主的.exe.config
- C# 如何调用VB6.0编写的dll
- c#编写dll 供其他语言调用问题
- c#调用c++编写的DLL
- c#调用非托管C++生成的dll
- 使用clr 调用C#编写的dll中的方法的全解释
- 如何在C#中加载自己编写的动态链接库(DLL)
- [转帖]如何在C#中加载自己编写的DLL
- 转:C# .NET中调用VB编写的DLL代码事例(我做了些修改)
- C++如何调用C#编写的DLL
- c#编写的基于Socket的异步通信系统封装DLL--SanNiuSignal.DLL