Tuesday, July 17, 2007

DLL creation and calling by VC++6

In Yahoo Kimo knowledge base, there is a very good example to learn the dll creation and calling in VC++6. I do use this example to create the dll file for my own project and it runs well.

Here I borrow some of the content from the website and add some of my interpretation.
The essential part to generate a usable dll file is the modification of the header file from the original one for exe file.
For example, the hello.h should be

// hello.h___________________

#ifdef __cpluscplus
#define EXPORT extern "C" __declspec(dllexport)
#else
#define EXPORT __declspec(dllexport)
#endif

EXPORT void hello();
// end hello.h___________________________________________

The red bold part is the required addition to the original header file.

In the main file, hello.c, we can simply keep the original file as the original one, such as
// hello.c___________________

#include"hello.h"
#include

EXPORT void hello(){
printf("The First Hello World !");
}

  • To build a hello.dll, in visual C++ 6, we can create a dll project with the inclusion of the files above.
  • To use the hello.dll, we should include the hello.lib generated from the compiler into the main executable code. Then the dll, lib and other codes are put in the same folder to make a new executable file which can use the functions in dll files.

No comments: