从代码块中的非托管调用托管代码 [英] calling managed code from unmanaged in codeblocks

查看:89
本文介绍了从代码块中的非托管调用托管代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从vb dotnet dll调用函数(来自cpp dll),

这是我的代码

I tried calling a function from a vb dotnet dll(from a cpp dll),
this is my code

#include "main.h"
#include <windows.h>
#include <mscoree.h>
#pragma comment(lib,"mscoree.lib")
void DLL_EXPORT SomeFunction()
{
        ICLRRuntimeHost *pClrHost = NULL;
        HRESULT hr = CorBindToRuntimeEx(
        NULL, L"wks", 0, CLSID_CLRRuntimeHost,
        IID_ICLRRuntimeHost, (PVOID*)&pClrHost);
        hr = pClrHost->Start();
        DWORD dwRet = 0;
        hr = pClrHost->ExecuteInDefaultAppDomain(
             L"message.dll",
             L"message.Class1", L"message", L"Hello", &dwRet);
}

extern "C" DLL_EXPORT BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
    switch (fdwReason)
    {
        case DLL_PROCESS_ATTACH:
            // attach to process
            // return FALSE to fail DLL load
            break;

        case DLL_PROCESS_DETACH:
            // detach from process
            break;

        case DLL_THREAD_ATTACH:
            // attach to thread
            break;

        case DLL_THREAD_DETACH:
            // detach from thread
            break;
    }
    return TRUE; // succesful
}



.netnet代码是


the dotnet code is

Public Class Class1
    Function message(ByVal a As String)
        MsgBox(a)
    End Function
End Class



当我尝试使用ExecDLL.exe调用SomeFunction时没有任何反应

我正在使用codeblocks

编译器是MS visual C ++ 2005/2008


when i try calling SomeFunction using ExecDLL.exe nothing happens
am using codeblocks
compiler is MS visual C++ 2005/2008

推荐答案

您好b $ b

为什么你没有使用C ++ / CLI目的?我建议您使用if,因为CLI技术为同时使用本机代码和托管代码提供了非常好的环境和虚假。有关它的更多信息,请关注链接:



使用MFC应用程序中的.net 4库 [ ^ ]



您可以使用MFC或C ++中的.net表单和控件,如果您想获得有关它的信息,可以使用:



如何在Windows用户控制中托管Windows窗体 [ ^ ]

在MFC对话框中托管Windows窗体用户控件 [ ^ ]



最好的问候。
Hi
Why you don`t used C++/CLI for this purpose? I suggest to you use if because CLI technology provide a really good environment and falsity to the simultaneous use of native and managed code. for getting more information about it please follow of links:

Using .net 4 library from MFC applications[^]

and you can use .net forms and controls from MFC or C++ if you want get the information about it you can go:

How to Host Windows Form in Windows User Control[^]
Hosting Windows Forms User Control in MFC Dialogs[^]

Best Regards.


这篇关于从代码块中的非托管调用托管代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆