【问题标题】:import a dll function in C++在 C++ 中导入 dll 函数
【发布时间】:2019-07-13 15:17:10
【问题描述】:

我有一个名为 swedll32.dll 的 Dll 文件,用于天文计算。 我已经在 C# 中导入了这些函数并正在使用它们。 但在 c++ 中,我尝试了所有可能的方法来导入这些函数,但它不起作用。

能否请人演示如何导入具有给定名称的函数?

int swe_calc_ut(double tjd_ut,int ipl,int iflag,double* xx,char* serr), 在哪里 tjd_ut =儒略日,世界时间 ipl =身体编号 iflag = 一个 32 位整数,包含指示需要哪种计算的位标志。

xx=经度、纬度、距离、长距离速度、纬度速度和距离速度的 6 个双精度数组。

serr[256] = 出现错误时返回错误信息的字符串。

【问题讨论】:

    标签: c++ c function dll import


    【解决方案1】:

    虽然以下内容仍然有效,但 stack overflow answer 也可能会有所帮助。

    article 包含一个从 DLL 导入函数的示例,但要点是:

    int CallMyDLL(void){ 
    
      /* get handle to dll */ 
     HINSTANCE hGetProcIDDLL = LoadLibrary("C:\\MyDLL.dll"); 
    
     /* get pointer to the function in the dll*/ 
     FARPROC lpfnGetProcessID = GetProcAddress(HMODULE (hGetProcIDDLL),"MyFunction"); 
    
     /* 
      Define the Function in the DLL for reuse. This is just prototyping the dll's 
      function. 
      A mock of it. Use "stdcall" for maximum compatibility. 
     */ 
     typedef int (__stdcall * pICFUNC)(char *, int); 
    
     pICFUNC MyFunction; 
     MyFunction = pICFUNC(lpfnGetProcessID); 
    
     /* The actual call to the function contained in the dll */ 
     char s[]= "hello";
     int intMyReturnVal = MyFunction(s, 5); 
    
     /* Release the Dll */ 
     FreeLibrary(hGetProcIDDLL); 
    
     /* The return val from the dll */ 
      return intMyReturnVal; 
    }
    

    【讨论】:

    • 我在回答这个问题之前试了一下,但是没有用。我稍后会发布错误
    • C2664 'int (char *,int)': 无法将参数 1 从 'const char [6]' 转换为 'char *' 错误 C2065 'returnintMyReturnVal': 未声明的标识符
    • 第一个错误意味着你说错了。向我们展示您的实际代码。二是笔误。 return后面必须有空格。
    • 在C++方法之前你能看到堆栈溢出链接吗?我认为这可能会解决您的问题。我发布的代码可能不是 C++11+。
    • 错误的调用是 "hello" 是一个只读文字,您的编译器(正确地)等同于 const 变量。我编辑了 Horatius 的示例来修复这个错误。
    猜你喜欢
    • 2016-05-23
    • 2013-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多