浙江财经大学
信工学院ACM集训队

[Win32]一个简单的动态链接库

本文由 Ocrosoft 于 2018-06-21 17:10:36 发表

这个动态链接库和主程序是放在同一个解决方案下的。

需要新建一个解决方案,再添加一个动态链接库项目(EDRLIB),再在文件-添加-新建项目中添加一个桌面应用项目(EDRTEST)。

编译之前需要在解决方案管理器中,EDRTEST项目的引用单击右键,将EDRLIB项目添加到引用。还要右键EDRTEST项目,将其设置为启动项目。

如果不添加引用,可以在EDRTEST的属性-链接器-常规-附加库目录中添加../Debug,EDRTEST的属性-链接器-输入-附加依赖项中添加EDRLIB.LIB。

EDRLIB/EDRLIB.h

#ifdef __cplusplus
// 防止编译程序破坏C++函数名,使C/C++都能够使用DLL
#define EXPORT extern "C" __declspec(dllexport)
#else
#define EXPORT __declspec(dllexport)
#endif

// 所有函数都需要声明EXPORT,保证函数名被添加到.lib中
EXPORT BOOL CALLBACK EdrCenterTextA(HDC, PRECT, PCSTR);
EXPORT BOOL CALLBACK EdrCenterTextW(HDC, PRECT, PCWSTR);

#ifdef UNICODE
#define EdrCenterText EdrCenterTextW
#else
#define EdrCenterText EdrCenterTextA
#endif

EDRLIB/EDRLIB.c

#include <windows.h>
#include "EDRLIB.h"

// 类似WinMain,用于执行初始化
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);
}

EDRTEST/EDRTEST.c

#include <windows.h>
#include "../EDRLIB/edrlib.h"

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
	static TCHAR szAppName[] = TEXT("StrProg");
	HWND hwnd;
	MSG msg;
	WNDCLASS wndclass;

	wndclass.style = CS_HREDRAW | CS_VREDRAW;
	wndclass.lpfnWndProc = WndProc;
	wndclass.cbClsExtra = 0;
	wndclass.cbWndExtra = 0;
	wndclass.hInstance = hInstance;
	wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
	wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wndclass.lpszMenuName = NULL;
	wndclass.lpszClassName = szAppName;

	if (!RegisterClass(&wndclass))
	{
		MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
		return 0;
	}

	hwnd = CreateWindow(szAppName, TEXT("DLL Demonstration Program"), WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
		NULL, NULL, hInstance, NULL);

	ShowWindow(hwnd, iCmdShow);
	UpdateWindow(hwnd);

	while (GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return msg.wParam;
}

LRESULT APIENTRY WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	HDC hdc;
	PAINTSTRUCT ps;
	RECT rect;

	switch (message)
	{
	case WM_PAINT:
		hdc = BeginPaint(hwnd, &ps);

		GetClientRect(hwnd, &rect);

		// 调用EDRLIB的函数
		EdrCenterText(hdc, &rect, TEXT("This, string was displayed by a DLL"));

		EndPaint(hwnd, &ps);
		return 0;

	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
	}
	return DefWindowProc(hwnd, message, wParam, lParam);
}

欢迎转载,请保留出处与链接。Ocrosoft » [Win32]一个简单的动态链接库

点赞 (0)or拍砖 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址