c - 在C程序中使用动态库dll

标签 c matlab dll dynamic-library

我想在我的 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。

最佳答案

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

#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;
}

关于c - 在C程序中使用动态库dll,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40305202/

相关文章:

c - 未找到 ESP8266 RTOS SDK 引脚 16 声明

c - 错误: expected declaration specifiers or ‘...’ before ‘stdin’

c - 从两个不同索引处的 2 个不同线程写入 float 组是否安全?

c++ - 性能:Matlab 与 C++ 矩阵 vector 乘法

Matlab logncdf 函数未产生预期结果

performance - 加速 MATLAB 中的随机数生成

c# - 故障模块这是什么意思,为什么会发生这种情况?

c - %d 和 %f 的不同值与相同的 var

c++ - DLL +导出类+模板成员func =无法解析的外部符号。有机会解决吗?

c# - P/Invoke 代码在 WinXP 上工作,在 Win2k8 上异常(exception)