call a dll
Nov 8, 2010 at 9:42pm UTC
ok i made a dll with some functions inside it, it looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
using namespace std;
void myfunc();
int main()
{
myfunc();
return 0;
}
void myfunc()
{
cout << "this is from a dll" ;
}
how do i access these functions from an application
Nov 9, 2010 at 1:20am UTC
Export the functions from the DLL, then import them into your application.
Exporting: __declspec(dllexport) -OR- .DEF files
Importing: LoadLibrary & GetProcAddress
Nov 10, 2010 at 1:09am UTC
could you give me an example how to use those functions?
Nov 10, 2010 at 5:28pm UTC
In a DLL you must export the names of data and functions you wish to be available outside of the DLL. eg:
1 2 3
// mydll.cpp
#include <iostream>
void __declspec(dllexport) myfunc(void ) { std::cout << "In myfunc()" << std::endl; }
When you build the DLL it should create an import library(mydll.lib) that you link your application with for early binding. For late binding you use LoadLibrary/GetProcAddress/FreeLibrary. For early binding:
1 2 3 4 5 6 7 8
// myprogram.cpp
void __declspec(dllimport) myfunc(void );
int main(void )
{
myfunc();
return 0;
}
Whether you use early binding or late binding the DLL must be in the DLL search path which will likely be the directory of your application, the directory of your source, or a system directory.
Nov 11, 2010 at 2:45am UTC
my program now looks like this
1 2 3 4 5 6 7 8 9 10
#include <Windows.h>
void __declspec(dllimport) mydll();
int main()
{
myfunc();
return 0;
}
an my dll looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <iostream>
using namespace std;
void __declspec(dllexport) myfunc()
{
cout << "In myfunc()" ;
}
int main()
{
return 0;
}
my dll compiles flawlessly but my program says it cannot find "mydll.lib", it is in the debug directory am i supposed to put it there?
Nov 11, 2010 at 1:57pm UTC
Did your build generate .lib and .dll files?
Nov 11, 2010 at 8:46pm UTC
it did
Nov 11, 2010 at 10:11pm UTC
The linker needs to find the .lib, the .dll is used at runtime.
Topic archived. No new replies allowed.