0

I've got a little problem. I got a dll C library, a header file, and all other files needed to call this dll. I've tried calling this dll through third party programs and it is working. However, when I try calling it directly (dynamic linking at load and using the given header file) I always get the linker error 1136 with mydll.lib.

Using the header file:

#include "windows.h"
#include "mydll.h"

void main() {
    bool test;
    test = CallDll("MyArg");
}

With code in headerfile as below:

extern "C" bool _stdcall CallDll(char* MyArg);

Using dynamic linking at load time:

#include "windows.h"

bool(*CallDll)(char*);
HINSTANCE h = LoadLibrary((LPCSTR)"mydll");

void main() {
    CallDll = (bool(*)(char*))GetProcAddress(h, "CallDll");
    bool test;
    test = CallDll("MyArg");
}

Now what did I do wrong? I doubt the mydll.lib file is broken, because if this were the issue, I couldn't access the dll with a third party program.

Wandering Fool
  • 2,170
  • 3
  • 18
  • 48
Y.S
  • 311
  • 1
  • 10
  • you have to give the DLL, not the LIB, to LoadLibrary. – bmargulies Aug 13 '15 at 16:11
  • The linker error message says that mydll.lib is broken. Why do you doubt it? Did you just copy mydll.dll to mydll.lib and hope it would work? It won't. Get a good one from the author or vendor. – Hans Passant Aug 13 '15 at 17:44
  • If you use `LoadLibrary`, you don't have to link the `.lib` file – huysentruitw Aug 13 '15 at 19:37
  • You're mixing implicit and explicit linking: read more [here](http://www.codeguru.com/cpp/cpp/cpp_mfc/tutorials/article.php/c9855/DLL-Tutorial-For-Beginners.htm) – huysentruitw Aug 13 '15 at 19:43
  • @WouterHuysentruit Thanks for all your help, it worked when i removed the link to the .lib file in VS. Now I have another problem, the function pointer defined with typdef bool( * CallDll)(char *,double,double&) somehow only accepts one input, even thougth i specified 3, any idead what I did wrong? – Y.S Aug 18 '15 at 07:07

1 Answers1

0

Well it was a rather simple solution.

bool(*CallDll)(char*);
HINSTANCE h = LoadLibrary(L"mydll.dll");

void main() {
    CallDll = (bool(*)(char*))GetProcAddress(h, "CallDll");
    bool test;
    test = CallDll((char*)"MyArg");
}

Was all it needed...

Y.S
  • 311
  • 1
  • 10
  • ehm the solution has a wrong cast, this is the right one: `CallDll DoCallDll;` `DoCallDll=(bool(*)(char*))GetProcAddress(h,"CallDll");` `bool test=DoCallDll((char*)"MyArg");` – Y.S Sep 29 '15 at 10:18