sh711 님의 블로그

windows API 프로그래밍 본문

Study/Windows

windows API 프로그래밍

sh711 2025. 3. 13. 01:46

1. 개념

windows API 프로그래밍은 윈도우 환경에서 작동되는 프로그래밍 개발 방식으로 윈도우의 자체적인 API를 사용하는 프로그래밍이다

 

2. 실습

간단한 창을 띄우는 프로그램을 만들어보겠다.

프로그램에 사용될 Window API 함수들에 대해 살펴보자

 

2-2. CreateWindowExA

해당 함수는 창을 만들어주는 함수로 크기, 스타일 등을 매개변수로 받아 호출된다.

// 함수 원형

HWND CreateWindowExA(
  [in]           DWORD     dwExStyle,	// 만들 창의 확장 창 스타일
  [in, optional] LPCSTR    lpClassName,	//  창 클래스 이름
  [in, optional] LPCSTR    lpWindowName,	// 창 이름
  [in]           DWORD     dwStyle,	// 생성되는 창의 스타일
  [in]           int       X,	// 창의 초기 가로 위치
  [in]           int       Y,	// 창의 초기 세로 위치
  [in]           int       nWidth,	// 창의 너비
  [in]           int       nHeight,	// 창의 높이
  [in, optional] HWND      hWndParent,	// 부모 또는 소유자 창에 대한 핸들
  [in, optional] HMENU     hMenu,	// 팝업 창의 경우 메뉴 식별
  [in, optional] HINSTANCE hInstance,	// 연결할 모듈 인스턴스의 핸들
  [in, optional] LPVOID    lpParam	// CREATESTRUCT를 통해 창에 전달할 값의 포인터
);

 

2-2. WindowPorc

WindowProc 콜백 함수는 Windows 운영 체제에서 발생한 이벤트를 처리하기 위해 실행되는 함수이다

이 함수는 윈도우 메시지를 처리하는 함수로, 메시지 루프가 실행될 때 특정 메시지를 받아서 해당 메시지를 처리하는 역할을 한다.

// 함수 원형

WNDPROC Wndproc;

LRESULT Wndproc(
  HWND unnamedParam1,	// 창 핸들
  UINT unnamedParam2,	// 메시지
  WPARAM unnamedParam3,	// 추가 메시지 정보
  LPARAM unnamedParam4	// 추가 메시지 정보
)
{...}

return : LRESULT => 전송 메시지 처리에 대한 결과

 

2-3. GetMessage

GetMessage 함수는 Windows API에서 메시지 큐에서 메시지를 가져오는 함수이다

이 함수는 메시지 루프의 핵심 요소로 사용되며 애플리케이션이 발생한 시스템 이벤트(마우스 클릭, 키보드 입력(alt + F4등), 윈도우 크기 변경 등)를 처리한다. 

// 함수 원형

BOOL GetMessage(
  [out]          LPMSG lpMsg,
  [in, optional] HWND  hWnd,
  [in]           UINT  wMsgFilterMin,
  [in]           UINT  wMsgFilterMax
);

 

2-4. ShowWindow

이 함수는 지정된 창의 표시 상태를 설정한다.

// 함수 원형

BOOL ShowWindow(
  [in] HWND hWnd,
  [in] int  nCmdShow	// SW_SHOW : 5, SW_HIDE : 0, SW_MINIMIZE : 7 ...
);

 

 

 

프로그램 코드

#include <windows.h>

// 윈도우 프로시저 함수
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    switch (uMsg) {
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

int main() {
    // 윈도우 클래스 등록
    WNDCLASS wc = { };
    wc.lpfnWndProc = WindowProc; // 윈도우 프로시저 함수 지정
    wc.hInstance = GetModuleHandle(NULL);
    wc.lpszClassName = "SampleWindowClass";
    RegisterClass(&wc);

    // 윈도우 생성
    HWND hwnd = CreateWindowExA(
        0,                            // 확장 스타일
        wc.lpszClassName,             // 클래스 이름
        "Sample Window",              // 창 제목 (ASCII 문자열)
        WS_OVERLAPPEDWINDOW,          // 창 스타일
        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 
        NULL, NULL, wc.hInstance, NULL);

    if (hwnd == NULL) {
        return 0;
    }

    ShowWindow(hwnd, SW_SHOW);

    // 메시지 루프
    MSG msg = { };
    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;
}

 

실행 결과

 

'Study > Windows' 카테고리의 다른 글

Windows Kernel Driver Load  (0) 2025.03.14
Windbg 윈도우 커널 분석 - 1  (0) 2025.03.10
Windbg 프로그램 분석  (0) 2025.03.08