0

I have a Dll file named swedll32.dll which is used for astronomical calculations. I have imported those functions in C# and am using them. but in c++ i have tried every way possible to import these functions but it doesn't work.

could please somebody demonstrate how to import a function for example with given name?

int swe_calc_ut(double tjd_ut,int ipl,int iflag,double* xx,char* serr), where tjd_ut =Julian day, Universal Time ipl =body number iflag =a 32 bit integer containing bit flags that indicate what kind of computation is wanted.

xx=array of 6 doubles for longitude, latitude, distance, speed in long., speed in lat., and speed in dist.

serr[256] =character string to return error messages in case of error.

Gewure
  • 1,208
  • 18
  • 31
Wolf
  • 3
  • 4

1 Answers1

2

While the below is still valid, this stack overflow answer might help as well.

This article contains an example for importing a function from a DLL, but the gist is:

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; 
}
Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
  • I tried this just a moment before answer this question but it didn't work . i will post errors some moment later – Wolf Jul 13 '19 at 15:24
  • C2664 'int (char *,int)': cannot convert argument 1 from 'const char [6]' to 'char *' Error C2065 'returnintMyReturnVal': undeclared identifier – Wolf Jul 13 '19 at 15:30
  • The first error means you are calling it wrong. Show us your actual code. The second is a typo. There must be a space after `return`. – Paul Ogilvie Jul 13 '19 at 15:39
  • Were you able to see the stack overflow link before the C++ method? I think that might resolve your issue. The code I posted might not be C++11+. – Horatius Cocles Jul 13 '19 at 15:39
  • The wrong call is that `"hello"` is a read-only literal that your compiler (correctly) equates to a `const` variable. I edited Horatius' example to fix this error. – Paul Ogilvie Jul 13 '19 at 15:41
  • i used my dll but as always access violation exception thrown [Exception thrown at 0x00000000 in ConsoleApplication4.exe: 0xC0000005: Access violation executing location 0x00000000.] – Wolf Jul 13 '19 at 16:41