在C++中导入 DLL 函数

import a dll function in C++

本文关键字:DLL 函数 导入 C++      更新时间:2023-10-16

我有一个名为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] =字符串,用于在出现错误时返回错误消息。

虽然以下内容仍然有效,但此堆栈溢出答案也可能有所帮助。

本文包含一个从 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; 
}