【问题标题】:why to mention not to mangle the name when calling a c++ function from other c++ dll为什么在从其他 c++ dll 调用 c++ 函数时不要提及不破坏名称
【发布时间】:2015-07-23 11:56:02
【问题描述】:

这是我在 dll1 中导出的函数

        extern"C" __declspec(dllexport) int FUN1(char* p){
        return p[0];
        }

    I am calling this FUN1 from other project.Below is the code

        #include
        #include
        using namespace std;
        typedef int (*MYFUN1)(char*);

        int main()

            {

                HMODULE hMod = LoadLibrary ("C:\\Users\\admin\\Documents\\Visual Studio 2010\\Projects\\CalledFun\\Debug\\CalledFun.exe");
                if(hMod != NULL)
                {
            MYFUN1 pCtor = (MYFUN1) GetProcAddress (hMod, "FUN1");
            int a = pCtor("calling a value") ;
            cout<<a;
                }
            }

如果我从 dll1 中删除 extern "C",那么在 dll2 中调用 GetProcAddress 时,该函数地址将返回为 NULL。由于两者都是用 c++ 编写的,我认为名称修改不会影响这一点。我认为如果我们结合使用 c 和 c++ 库,那么我们只需要提及 extern "C" ,任何人都可以帮助我摆脱这种困惑

提前致谢

【问题讨论】:

  • 叫我疯了,但用.exe 打电话给LoadLibrary 似乎...不正确。
  • 请注意——不管是DLL还是EXE。游戏规则是一样的。
  • @cmbasnett 您可以从 EXE 导出函数。这种情况很少见,但可以做到,因为 DLL 和 EXE 都被视为可执行模块。
  • 您原则上可以使用错位名称,但是GetProcAddress 也必须使用错位名称。这有点不方便。

标签: c++ visual-c++


【解决方案1】:

您需要禁用名称修改,因为您使用的是 Windows API,例如您提供函数名称的 LoadLibrary。 Windows API 和 LoadLibrary 对您的编译器名称修改一无所知,因此您需要将其关闭。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-21
    • 1970-01-01
    • 1970-01-01
    • 2011-12-24
    • 2011-06-11
    • 1970-01-01
    • 2011-01-25
    • 2020-05-13
    相关资源
    最近更新 更多