Importing a function from a DLL compiled using another language

Started by jm, Yesterday at 07:50:22 PM

Previous topic - Next topic

jm

This is purely a research exercise on my part with limited knowledge.  I'd be grateful if someone could tell me if there's a way in which I can import a function from a Windows DLL that I created using a non-C language compiler on Windows.

The other language compiler has understandably given me only a .DLL and a .LIB but the examples I've seen, where a DLL is used within C, appear to show a .H header is needed.  What should I create in the .H file, or is there another way?

The DLL I created is called JDL.DLL and it contains a function called myfunction() to accept a float value and return the result also as a float.  Can anyone help with this please, thanks.

Vortex

Hi jm,

The .h file, the header file contains the prototypes of the functions exported by your DLL. Strictly, you don't need a header file. All what you need is to translate correctly your function prototypes in that language to C.

As an example, consider the source code of this simple FreeBASIC DLL named testdll.dll :

Function sum Cdecl Alias "sum" (Byval x As Integer, Byval y As Integer) As Integer Export
   
    Return(x+y)
   
End Function

Function subs Cdecl Alias "subs" (Byval x As Integer, Byval y As Integer) As Integer Export
   
    Return(x-y)
   
End Function

Pelles C code with the function prototypes :

#include <stdio.h>

extern int __cdecl sum(int x,int y);

extern int __cdecl subs(int x,int y);

int main(void)
{
printf("70 + 30 = %d\n",sum(70,30));
printf("70 - 30 = %d",subs(70,30));
return 0;
}

Code it... That's all...

jm

Many thanks indeed Vortex, that worked very well.  As I'd expected, it was necessary for me to put my .DLL and .LIB into the project's folder and also go to Project — Project options — Linker, then add the .LIB filename to the Library and Object Files box.

Is there a way to inform the compiler/linker that the .DLL and .LIB files are in a specific path outside the project folder?  I tried specifying the full path of the .LIB but it wouldn't compile, presumbly because it also requires the path to the .DLL.  I couldn't find a way of doing that.

I wasn't able to return a float or double with the expected value, but I expect that's due to calling methods, so I need to experiment further with that.

TimoVJL

For libraries, use Project options -> General -> Folders -> Libraries

If a your JDL.DLL exports function, a calling convention had to know at least in x86, as x64 have only one calling convention.
like:

float __cdecl myfunc(float);  // _myfuncor
float __stdcall myfunc(float); // _myfunc@4
May the source be with you

jm

It's all fixed, thank you very much for your help.  I realise my ridiculous mistake now — while I can put the .LIB in a separate library folder as you pointed out, the .DLL at least has to be in the path when the executable runs.

Incidentally, the float return was also my error, it wasn't a problem at all in the end.  My DLL is in fact 64-bit code, but if I change the calling convention to float __stdcall, it warns me :

...\MyDLL\pelles\pelles.c(3): warning #2203: Function 'my_function' can't be __stdcall, changed to __cdecl.

Interesting to see that.