我如何从其他 C++ dll 中的 dll 调用 JNIEXPORT 函数

索鲁什

我用一个 JNIEXPORT 函数编写了 Visual c++ dll,如下所示:

extern "C"
{
    JNIEXPORT int JNICALL sum()
    {
        return 1;
    }
}

我想在其他 Visual C++ dll 中调用这个函数,如下所示:

typedef int(__stdcall *f_funci)();

HINSTANCE hGetProcIDDLL = LoadLibrary("C:/.../JNIdllTest.dll");

if (!hGetProcIDDLL) {

    std::cout << "could not load the dynamic library" << std::endl;

    return EXIT_FAILURE;
}

    f_funci funci = (f_funci)GetProcAddress(hGetProcIDDLL, "sum");

if (!funci) {

    std::cout << "could not locate the function" << std::endl;

    return EXIT_FAILURE;
}

std::cout << "funci() returned " << funci() << std::endl;

return EXIT_SUCCESS;

但是当我运行它时会发生这个错误

找不到函数

我应该怎么办?

萨米·萨利宁

看起来您的“sum”函数并不是真正从 JNIdllTest.dll 导出的 dll。您可以通过例如在dependency walker 中打开DLL 来查看从DLL 导出的函数:http : //www.dependencywalker.com/

因此,下载并检查导出视图是否具有 sum 函数,以及函数的名称是否只是简单的“sum”或是否以某种方式进行了修饰。(默认情况下,C++ 将返回值和函数参数的类型添加到函数名称中)。

它不应该被修饰,因为您的函数位于 extern "C" 块内,但如果是,请确保函数声明前面也有 extern "C"。

如果该函数确实不是从 DLL 导出的,请检查您对 JNIEXPORT 的定义,并尝试使用“__declspec(dllexport)”代替 - 请参阅https://msdn.microsoft.com/en-us/library/a90k134d。 ASPX

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章