【问题标题】:Use a dynamic library dll in C program在 C 程序中使用动态库 dll
【发布时间】:2016-10-29 03:21:16
【问题描述】:

我想在我的 C 代码中使用 dll 文件,但对语法很困惑。

我的故事:我在 Matlab ( f(x1,x2)=x1*x2 ) 中创建了一个简单的函数,使用“Matlab Coder”将它翻译成 C 代码并生成了一个 exe,我可以从带有参数的终端。现在我生成了一个 dll 而不是 exe 并想使用该 dll。

因为现在我无法解释代码,所以我用谷歌搜索,为我工作。我在http://en.cppreference.com/w/ 中查找语法,但令我惊讶的是,甚至没有条目例如GetProcAddress 或 LoadLirbary。

这是我想在其中使用 dll 的 C 代码:

#include <stdio.h>
#include <stdlib.h>

/*
* In my dream I would load the dll function here
* with something like Load(mytimes4.dll)
*/

int main(int argc, char *argv[]) {

double x1,x2,myresult;
//Load Arguments from Terminal
sscanf(argv[1], "%lf", &x1);
sscanf(argv[2], "%lf", &x2);

// Use and print the function from mytimes4.dll
myresult = mytimes4(x1,x2);
printf("%3.2f\n",myresult);

return 0;
}

生成 dll 后,Matlab 给了我以下文件夹: "dll-folder" produced by Matlab

有人可以给我一个最简单但最完整的代码,可以与我的示例一起使用吗?需要哪些文件(可能是 .def 或 .exp)?对于使用 dll 所涉及的行的解释,我将不胜感激。或者,如果没有,您可能有一些使复杂语法合理的背景知识。提前致谢!

系统信息:Windows 7 Pro 64、Matlab 64 2016b、gcc cygwin 64、eclipse ide。

【问题讨论】:

  • 我对 cppreference 没有 OpenLibrary 和 GetProcAddress 条目并不感到惊讶——这些函数是 Windows API 的一部分(不是 C++ 标准的一部分)。有关 OpenLibrary 的描述,请参阅 msdn.microsoft.com/en-us/library/windows/desktop/…,例如用法,请参阅 msdn.microsoft.com/en-us/library/windows/desktop/…,了解使用 OpenLibrary 和 GetProcAddress 的示例。
  • 你可以查看我对该帖子Link to a DLL in Pelles C提出的解决方案。
  • mytimes4.hmytimes4.def的内容是什么?
  • @thurizas,很好的链接!而且我不知道它是windows-C-Code。 @J.Piquard,感谢您的帮助。我想你的问题在我自我回复之后现在不需要答案了。
  • @crx 如果您需要在 Linux 上执行此操作(我怀疑是 Unix 和 Mac,但我没有测试它们),相应的函数是 dlopen 和 @987654330 @ 和 dlclose。快乐编码

标签: c matlab dll dynamic-library


【解决方案1】:

通过 thurizas 的链接,我可以解决我的问题。 https://msdn.microsoft.com/en-us/library/windows/desktop/ms686944(v=vs.85).aspx 我从侧面复制了代码。您可以在下面看到带有我的其他 cmets 的代码,并且在我看来,更清楚地命名。因此,对于像我这样的初学者来说,它可能更容易使用。

#include <stdio.h>
#include <stdlib.h>
#include <windows.h> 

/*Declaration of the function,contained in dll, as pointer with the arbitrary pointer name
 "*MYFUNCTIONPOINTER" (not sure if it has to be in big letters).
In my case the function means simply f(x1,x2) = x1*x2 and is thus as double declared*/
typedef double (*MYFUNCTIONPOINTER)(double, double);

int main() {

    HINSTANCE hinstLib;
    //"myfunction" is the arbitrary name the function will be called later
    MYFUNCTIONPOINTER myfunction;
    BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;

    //Tell the dll file
    hinstLib = LoadLibrary(TEXT("mypersonal.dll"));

    if (hinstLib != NULL)
        {
        /* At this line "myfunction" gets its definition from "MYFUNCTIONPOINTER"
         and can be used as any other function.The relevant function in the dll has
         to be told here.*/
        myfunction = (MYFUNCTIONPOINTER) GetProcAddress(hinstLib, "mydllfunction");

            // If the function address is valid, call the function.
            if (NULL != myfunction)
            {
                fRunTimeLinkSuccess = TRUE;

             // The function can be used.
             double  myoutput;
             myoutput = myfunction(5,7);
             printf("%f\n",myoutput);
             getchar();
            }
            // Free the DLL module.

            fFreeResult = FreeLibrary(hinstLib);
        }

        // If unable to call the DLL function, use an alternative.
        if (! fRunTimeLinkSuccess)
            printf("Message printed from executable\n");

    return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-08
    • 2014-07-07
    • 1970-01-01
    相关资源
    最近更新 更多