I want to create a simple, simple DLL that exports one or two functions, and then try to call it from another program ... Wherever I looked so far, these are complex things, different ways of connecting things, strange problems that I even didn't start to realize exist yet ... I just want to start by doing something like this:
Create a DLL that exports some functions, for example
int add2(int num){ return num + 2; } int mult(int num1, int num2){ int product; product = num1 * num2; return product; }
I am compiling with MinGW, I would like to do it in C, but if there are any real differences in C ++, I would also like to know that. I want to know how to load this DLL into another C (and C ++) program, and then call these functions from it. My goal here, after playing around with the DLL a bit, is to make a VB interface for C (++) code by loading the DLLs in visual basic (I have visual studio 6, I just want to make some forms and events for objects on those forms, which cause the DLL).
I need to know how to call gcc (/ g ++) so that it creates a DLL, but also how to write (/ generate) an export file ... and what I can / cannot do in a DLL (for example, can I accept arguments a pointer / link from the front-end of VB? Can a DLL call a theoretical function in an interface? Or have a function-pointer function (I don’t even know if this is possible) from VB and name it?) I'm sure I can’t pass the option DLL ... but that's all I really know.
update again
Ok, I figured out how to compile it with gcc to run the DLL
gcc -c -DBUILD_DLL dll.c gcc -shared -o mydll.dll dll.o -Wl,--out-implib,libmessage.a
and then I had another program loading it and checking the functions, and it worked fine, Thanks a lot for the advice, but I tried downloading it using VB6, like this
Public Declare Function add2 Lib "C:\c\dll\mydll.dll" (num As Integer) As Integer
then I just called add2 (text1.text) from the form, but this gave me a runtime error:
"Cannot find add2 DLL entry point in C: \ c \ dll \ mydll.dll"
this is the code i compiled for the dll:
#ifdef BUILD_DLL #define EXPORT __declspec(dllexport) #else #define EXPORT __declspec(dllimport) #endif EXPORT int __stdcall add2(int num){ return num + 2; } EXPORT int __stdcall mul(int num1, int num2){ return num1 * num2; }
called it from C program, how it worked, though:
#include<stdio.h> #include<windows.h> int main(){ HANDLE ldll; int (*add2)(int); int (*mul)(int,int); ldll = LoadLibrary("mydll.dll"); if(ldll>(void*)HINSTANCE_ERROR){ add2 = GetProcAddress(ldll, "add2"); mul = GetProcAddress(ldll, "mul"); printf("add2(3): %d\nmul(4,5): %d", add2(3), mul(4,5)); } else { printf("ERROR."); } }
any ideas?
I decided
To solve the previous problem, I just had to compile it like this:
gcc -c -DBUILD_DLL dll.c gcc -shared -o mydll.dll dll.o -Wl,--add-stdcall-alias
and use this API call in VB6
Public Declare Function add2 Lib "C:\c\dll\mydll" _ (ByVal num As Integer) As Integer
I learned not to forget to explicitly specify ByVal or ByRef - I just returned the address of the argument that I passed, it looked like -3048.