Notice
Recent Posts
관리 메뉴

Hacking Arts

LoadLibrary() 와 GetProcAddress() 본문

Programing/API Programing

LoadLibrary() 와 GetProcAddress()

Rakehell 2014. 9. 26. 03:47

-LoadLibrary()

[그림 1 함수의 구조]

-프로세스에서 사용되는 api나 함수들은 라이브러리라는 dll 실행파일에 구현되어있고 프로세스는 구현되어있는 dll의 함수를 가져와서 사용하게 됩니다. 그때 프로세스에서 dll을 로드 하는데 사용되는 함수가 LoadLibrary입니다.

-인자값으로는 간단하게 dll파일의 위치만 알려주면 됩니다.


-GetProcAddress()


[그림 2 함수의 구조]

-GetProcAddress함수는 HMODULE은 모듈핸들로 DLL의 핸들값을 받아온다. 그다음 lpProcName은 모듈안에 찾고자하는 함수의 이름을 넣으면 주소값을 받아온다.


-FARPROC 


-dll을 로드시키는 프로그램 소스코드

#include
#include
#include

typedef int (*add_func)(int, int);
typedef int (*sub_func)(int, int);

int main()
{
	HINSTANCE hInst;

	add_func f_add_func;
	sub_func f_sub_func;

	hInst = LoadLibrary(_T("Gatsby.dll"));

	if (hInst == NULL)
	{
		printf("hInst NULL");
		return 1;
	}

	f_add_func = (add_func)GetProcAddress(hInst, "add_func");
	f_sub_func = (sub_func)GetProcAddress(hInst, "sub_func");

	int a = 150;
	int b = 80;
	int nRet = 0;

	nRet = f_add_func(a, b);
	printf("%d + %d = %d\n", a, b, nRet);

	nRet = f_sub_func(a, b);

	printf("%d - %d = %d\n", a, b, nRet);

	FreeLibrary(hInst);

	return 0;
}

-dll 프로그램 소스코드

#include
#include

#define EXPORT extern "C" __declspec(dllexport)

EXPORT int add_func(int a, int b);
EXPORT int sub_func(int a, int b);

BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
	return TRUE;
}

EXPORT int add_func(int a, int b)
{
	return a + b;
}

EXPORT int sub_func(int a, int b)
{
	return a - b;
}

-참고

http://msdn.microsoft.com/ko-kr/library/zzk20sxw.aspx

http://www.hackerschool.org/HS_Boards/zboard.php?id=Free_Lectures&page=1&sn1=on&divpage=2&sn=on&ss=off&sc=off&keyword=HongMK900&select_arrange=headnum&desc=asc&no=7899

http://wwwi.tistory.com/72

'Programing > API Programing' 카테고리의 다른 글

출력 / GDI / DC  (0) 2014.10.10
APIENTRY와 CALLBACK, WndProc  (0) 2014.09.28
WndProc - 메시지 처리하기  (0) 2014.09.20
WinMain - 4.메시지 루프 돌리기  (0) 2014.09.20
WinMain - 3.윈도우 객체 화면에 띄우기  (0) 2014.09.19