call a dll

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
Export the functions from the DLL, then import them into your application.

Exporting: __declspec(dllexport) -OR- .DEF files
Importing: LoadLibrary & GetProcAddress
could you give me an example how to use those functions?
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.
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?
Did your build generate .lib and .dll files?
it did
The linker needs to find the .lib, the .dll is used at runtime.
Topic archived. No new replies allowed.