如何在c ++中调用DLL方法。 Dll是用C#构建的 [英] How to call DLL methods in c++. Dll is build in C#

查看:58
本文介绍了如何在c ++中调用DLL方法。 Dll是用C#构建的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

typedef int (FAR PASCAL *RUNSCRIPT_FUNCTION)(int, LPCSTR);

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdParam, int nCmdShow)
{
    RUNSCRIPT_FUNCTION  RunScript;
    int  iReturn;

    // Load the DLL
    HINSTANCE hinstLib = LoadLibrary("C:\\Program Files\\Example.dll");
    if(hinstLib == NULL)
    {
        MessageBox(NULL, "Unable to load library", "Error", MB_OK|MB_ICONERROR);
        return 0;
    }

    // Get the function pointer
    RunScript = (RUNSCRIPT_FUNCTION)GetProcAddress(hinstLib, "RunScript");
    if(RunScript == NULL)
    {
        FreeLibrary(hinstLib);
       
        return 0;
    }
    
    // Call the function with '1' as a parameter
    iReturn = (*RunScript)(1, "Hello");

    // Unload the DLL
    FreeLibrary(hinstLib);
    
    return iReturn;
}





我的尝试:



我无法调用visual c ++中的方法,类和方法都存在于dll中,而dll是不受管理的。



What I have tried:

I am not able to call methods in visual c++,class and methods are present in dll and dll is unmanaged.

推荐答案

那不应该是大问题,但你必须注意字符串(和内存)问题。用于调用你的dll _bstr_t。用法解释这篇文章



如果您在C#运行时使用字符串,则需要复制。



提示:您可以设置断点如果dll是调试版本,则在C#dll中进行调试。
That shouldnt be a big problem, but you must pay attention to string (and memory) issue. Use for the call of your dll a _bstr_t. Ihe usage is explained this article.

If you work with the string in the C# runtime you need to make a copy.

Tip: you can set breakpoints in the C# dll for debugging if the dll is a debug build.


这篇关于如何在c ++中调用DLL方法。 Dll是用C#构建的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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