0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

VC++でDLLをディレイロードしてみた

Posted at

環境

  • Windows 10 Home (64bit)
  • Visual Studio Community 2019

手順

プロジェクトの作成

プロジェクト テンプレート:空のプロジェクト [C++]
プロジェクト名:dlltest

プロジェクトのプロパティ
リンカー/システム
サブシステム:Windows (/SUBSYSTEM:WINDOWS)

アーリーバインド

dlltest.cpp
# include <Windows.h>

int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
	MessageBoxA(nullptr, "アーリーバインド", "dlltest #1", MB_OK);
}

ディレイロード

dlltest.cpp
# include <Windows.h>

typedef int(__stdcall* PFMSGBOX)(HWND, LPCSTR, LPCSTR, UINT);

int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
	HMODULE hModule = LoadLibraryA("user32.dll");

	PFMSGBOX pfMsgBox = (PFMSGBOX)GetProcAddress(hModule, "MessageBoxA");
	pfMsgBox(nullptr, "ディレイロード", "dlltest #2", MB_OK);

	FreeLibrary(hModule);
}

インラインアセンブラ

dlltest.cpp
# include <Windows.h>

int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
	HMODULE hModule = LoadLibraryA("user32.dll");

	FARPROC pFunc = GetProcAddress(hModule, "MessageBoxA");
	LPCSTR lpText = "インラインアセンブラ";
	LPCSTR lpCaption = "dlltest #3";

	__asm {
		push	MB_OK
		push	lpCaption
		push	lpText
		push	NULL
		call	pFunc
		; stdcallにつきスタックは呼出先で戻す
	}

	FreeLibrary(hModule);
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?