How to create dll on windows

Preparation

Using visual studio to create dll binary as your project in order to reuse code conveniently.

1. Install visual studio which includes visual c++

2. Create a new project (File/Create/Project) and choose visual c++ template on left panel and choose win32 console application. Then input the project name.

3. Choose DLL in application type and keep other settings as default.

4. If you want to export the public interfaces as .lib file, set configuration properties/Linker/Advanced/Import Library as $(outdir)$(targetname).lib.

5. Define some public interfaces and then build it. Then you will see the lib, dll file is under the same folder.

DLL implement

for exporting global functions, use __declspec keyword. With using this dllexport way, the method signature will be listed in the .lilb file.

__declspec(dllexport) int Add (int lhs, int rhs)
{
return lhs + rhs;
}
 

Invoking DLL

include the header file with the method signature
#include "../ItemSync/ItemSyncDll.h"
#pragma comment (lib, "ItemSync.lib")
int main()
{
int ret = Add(1,2);
return 0;
}
Also you could put the lib under configuration properties/Linker/Input/Additional Dependencies. In thay way, the pragma comment could be ignored.
 

你可能感兴趣的:(windows)