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

[Win32]纯资源DLL

本文由 Ocrosoft 于 2018-06-23 17:28:40 发表

不导出任何函数,包含资源文件,这个DLL就作为纯资源库使用。(并不表示DLL做资源库就不能有导出函数)

例子中DLL包含了6张bitmap,showbit不引用bitlib,而是在运行的时候使用LoadLibrary加载DLL。
如果添加引用,因为bitlib没有包含导出函数,不会生成lib文件,会导致生成失败。

BITLIB/bitlib.c

#include <Windows.h>

int WINAPI DllMain(HINSTANCE hInstace, DWORD fdwReason, PVOID pvReserved)
{
	return TRUE;
}

BITLIB/BITLIB.rc

/////////////////////////////////////////////////////////////////////////////
//
// Bitmap
//

1             BITMAP                  "bitmap1.bmp"
2             BITMAP                  "bitmap2.bmp"
3             BITMAP                  "bitmap3.bmp"
4             BITMAP                  "bitmap4.bmp"
5             BITMAP                  "bitmap5.bmp"
6             BITMAP                  "bitmap6.bmp"

SHOTBIT/showbit.c

#include <Windows.h>

TCHAR szAppName[] = TEXT("ShowBit");

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

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrecInstance, PSTR szCmdLine, int iCmdShow)
{
	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 = szAppName;
	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;
}

void DrawBitmap(HDC hdc, int xStart, int yStart, HBITMAP hBitmap)
{
	BITMAP bm;
	HDC hMemDC;
	POINT pt;
	hMemDC = CreateCompatibleDC(hdc);
	SelectObject(hMemDC, hBitmap);
	GetObject(hBitmap, sizeof(BITMAP), &bm);
	pt.x = bm.bmWidth;
	pt.y = bm.bmHeight;

	BitBlt(hdc, xStart, yStart, pt.x, pt.y, hMemDC, 0, 0, SRCCOPY);

	DeleteDC(hMemDC);
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	static HINSTANCE hLibrary;
	static int iCurrent = 1;

	HBITMAP hBitmap;
	HDC hdc;
	PAINTSTRUCT ps;

	switch (message)
	{
	case WM_CREATE:
		// 加载DLL
		if ((hLibrary = LoadLibrary(TEXT("BITLIB.dll"))) == NULL)
		{
			MessageBox(hwnd, TEXT("Can't load BITLIB.DLL"), szAppName, 0);

			return -1;
		}
		return 0;

	case WM_CHAR:
		// 任意字符消息显示下一张
		if (hLibrary)
		{
			iCurrent++;
			InvalidateRect(hwnd, NULL, TRUE);
		}
		return 0;

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

		if (hLibrary)
		{
			// 加载DLL中的资源BITMAP
			hBitmap = LoadBitmap(hLibrary, MAKEINTRESOURCE(iCurrent));

			// 获取失败退回第一张
			if (!hBitmap)
			{
				iCurrent = 1;
				hBitmap = LoadBitmap(hLibrary, MAKEINTRESOURCE(iCurrent));
			}
			if (hBitmap)
			{
				DrawBitmap(hdc, 0, 0, hBitmap);
				DeleteObject(hBitmap);
			}
		}

		EndPaint(hwnd, &ps);
		return 0;

	case WM_DESTROY:
		if (hLibrary)
			// 卸载DLL
			FreeLibrary(hLibrary);

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

欢迎转载,请保留出处与链接。Ocrosoft » [Win32]纯资源DLL

点赞 (1)or拍砖 (0)

评论 抢沙发

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