Note that there are some explanatory texts on larger screens.

plurals
  1. POCompile a DLL in C/C++, then call it from another program
    primarykey
    data
    text
    <p>I want to make a simple, simple DLL which exports one or two functions, then try to call it from another program... Everywhere I've looked so far, is for complicated matters, different ways of linking things together, weird problems that I haven't even <i>begun</i> to realize exist yet... I just want to get started, by doing something like so:</p> <p>Make a DLL which exports some functions, like,</p> <pre><code>int add2(int num){ return num + 2; } int mult(int num1, int num2){ int product; product = num1 * num2; return product; } </code></pre> <p>I'm compiling with MinGW, I'd like to do this in C, but if there's any real differences doing it in C++, I'd like to know those also. I want to know how to load that DLL into another C (and C++) program, and then call those functions from it. My goal here, after playing around with DLLs for a bit, is to make a VB front-end for C(++) code, by loading DLLs into visual basic (I have visual studio 6, I just want to make some forms and events for the objects on those forms, which call the DLL).</p> <p>I need to know how to call gcc (/g++) to make it create a DLL, but also how to write (/generate) an exports file... and what I can/cannot do in a DLL (like, can I take arguments by pointer/reference from the VB front-end? Can the DLL call a theoretical function in the front-end? Or have a function take a "function pointer" (I don't even know if that's possible) from VB and call it?) I'm fairly certain I can't pass a variant to the DLL...but that's all I know really.</p> <h2>update again</h2> <p>Okay, I figured out how to compile it with gcc, to make the dll I ran</p> <pre><code>gcc -c -DBUILD_DLL dll.c gcc -shared -o mydll.dll dll.o -Wl,--out-implib,libmessage.a </code></pre> <p>and then I had another program load it and test the functions, and it worked great, thanks so much for the advice, but I tried loading it with VB6, like this</p> <pre><code>Public Declare Function add2 Lib "C:\c\dll\mydll.dll" (num As Integer) As Integer </code></pre> <p>then I just called add2(text1.text) from a form, but it gave me a runtime error:</p> <p>"Can't find DLL entry point add2 in C:\c\dll\mydll.dll"</p> <p>this is the code I compiled for the DLL:</p> <pre><code>#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; } </code></pre> <p>calling it from the C program like this worked, though:</p> <pre><code>#include&lt;stdio.h&gt; #include&lt;windows.h&gt; int main(){ HANDLE ldll; int (*add2)(int); int (*mul)(int,int); ldll = LoadLibrary("mydll.dll"); if(ldll&gt;(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."); } } </code></pre> <p>any ideas?</p> <h2>solved it</h2> <p>To solve the previous problem, I just had to compile it like so:</p> <pre><code>gcc -c -DBUILD_DLL dll.c gcc -shared -o mydll.dll dll.o -Wl,--add-stdcall-alias </code></pre> <p>and use this API call in VB6</p> <pre><code>Public Declare Function add2 Lib "C:\c\dll\mydll" _ (ByVal num As Integer) As Integer </code></pre> <p>I learned not to forget to specify ByVal or ByRef explicitly--I was just getting back the address of the argument I passed, it looked like, -3048.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload