DLL系列之一:如何编写DLL(动态链接库)
作者:互联网
以下内容引用自《Windows程序设计(第5版)》,作者:(美)Charles Petzold
---------------------------------------------------------------------------------------------------------------
首先我们建立一个头文件 newDLL.h
#ifdef _cplusplus #define EXPORT extern "C" _declspec (dllexport) #else #define EXPORT _declspec (dllexport) #endif EXPORT BOOL CALLBACK EdrCenterTextA(HDC, PRECT, PCSTR); EXPORT BOOL CALLBACK EdrCenterTextW(HDC, PRECT, PCWSTR); #ifdef UNICODE #define EdrCenterText EdrCenterTextW #else #define EdrCenterText EdrCenterTextA #endif
然后 我们建立个 C/C++文件 newDLL.cpp 实现头文件中的函数接口
#include <Windows.h> #include "newDLL.h" int WINAPI DllMain(HINSTANCE hInstance, DWORD fdwReason, PVOID pvReserved) { return TRUE; } EXPORT BOOL CALLBACK EdrCenterTextA(HDC hdc, PRECT prc, PCSTR pString) { int iLength; SIZE size; iLength = lstrlenA(pString); GetTextExtentPoint32A(hdc, pString, iLength, &size); return TextOutA(hdc, (prc->right - prc->left - size.cx) / 2, (prc->bottom - prc->top - size.cy) / 2, pString, iLength); } EXPORT BOOL CALLBACK EdrCenterTextW(HDC hdc, PRECT prc, PCWSTR pString) { int iLength; SIZE size; iLength = lstrlenW(pString); GetTextExtentPoint32W(hdc, pString, iLength, &size); return TextOutW(hdc, (prc->right - prc->left - size.cx) / 2, (prc->bottom - prc->top - size.cy) / 2, pString, iLength); }
最后 生成解决方案 便会在项目文件夹中生成相应的 lib 和 dll 文件
标签:prc,hdc,EXPORT,DLL,pString,iLength,动态链接库,编写,size 来源: https://www.cnblogs.com/liseawave/p/16318745.html