生成部分:
ToolDll.h
#pragma once
#ifndef WINVER // Specifies that the minimum required platform is Windows Vista.
#define WINVER 0x0600 // Change this to the appropriate value to target other versions of Windows.
#endif
#ifndef _WIN32_WINNT // Specifies that the minimum required platform is Windows Vista.
#define _WIN32_WINNT 0x0600 // Change this to the appropriate value to target other versions of Windows.
#endif
#ifndef _WIN32_WINDOWS // Specifies that the minimum required platform is Windows 98.
#define _WIN32_WINDOWS 0x0410 // Change this to the appropriate value to target Windows Me or later.
#endif
#ifndef _WIN32_IE // Specifies that the minimum required platform is Internet Explorer 7.0.
#define _WIN32_IE 0x0700 // Change this to the appropriate value to target other versions of IE.
#endif
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
// Windows Header Files:
#include <windows.h>
#ifdef SOGOUPYTOOLDLL_EXPORTS
#define OUTPUT_API __declspec(dllexport)
#else
#define OUTPUT_API __declspec(dllimport)
#endif
//extern "C" 没有它的话,导出函数的名字是解析成C++模式的,使用不当会出问题
extern "C" OUTPUT_API int nToolDll;
extern "C" OUTPUT_API int fnToolDll(void);
ToolDll.cpp
#include "ToolDll.h"
// This is an example of an exported variable
OUTPUT_API int nToolDll=0;
// This is an example of an exported function.
OUTPUT_API int fnToolDll(void)
{
return 42;
}
dllmain.cpp
// dllmain.cpp : Defines the entry point for the DLL application.
#include "ToolDll.h"
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
调用部分:
#include <iostream>
#include <Windows.h>
using namespace std;
typedef int (CallToolDll)(void);
int main()
{
HINSTANCE hDllInst;
hDllInst = LoadLibrary(L"ToolDll.dll");
if (hDllInst == NULL)
{
cout <<"加载dll失败"<<endl;
}
else
{
CallToolDll * callFunc = (CallToolDll *)GetProcAddress(hDllInst,"fnToolDll");
if (callFunc!=NULL)
{
cout << (*callFunc)() << endl;;
}
FreeLibrary(hDllInst);
}
return 0;
}