Featured image of post Rust와 windows-sys를 사용하여 Windows의 메시지 박스(MessageBox)를 표시하는 방법

Rust와 windows-sys를 사용하여 Windows의 메시지 박스(MessageBox)를 표시하는 방법

Rust에서 `windows-sys`와 `windows-sys` 크레이트를 이용해 Windows API를 호출하여 간단한 메시지 박스(MessageBox)를 표시하는 방법을, 프로젝트 생성부터 코드 구현까지 단계별로 설명합니다.

다음 절차에 따라 Rust에서 MessageBox를 표시할 수 있습니다.

  1. Rust를 설치합니다. Rust 시작하기 참조
  2. 명령 프롬프트에서 cargo new --bin message_box를 실행합니다.
  3. message_box 디렉터리로 이동합니다.
  4. Cargo.toml을 열고 다음과 같이 수정합니다.
1
2
3
4
5
6
7
[package]
name = "message_box"
version = "0.1.0"
edition = "2021"

[dependencies]
windows-sys = { version = "0.61", features = ["Win32_UI_WindowsAndMessaging"] }

이 예제는 Windows용입니다. Microsoft의 windows-sys는 Windows API의 원시 FFI 선언을 제공합니다. MessageBoxW는 UTF-16 문자열을 받으며 FFI 호출은 unsafe 블록 안에서 해야 합니다.

  1. src\main.rs를 열고 다음과 같이 수정합니다.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
use windows_sys::Win32::UI::WindowsAndMessaging::{
    MessageBoxW, MB_ICONINFORMATION, MB_OK,
};

fn wide_null(value: &str) -> Vec<u16> {
    value.encode_utf16().chain(std::iter::once(0)).collect()
}

fn main() {
    let text = wide_null("Hello, world!");
    let caption = wide_null("MessageBox Example");

    unsafe {
        MessageBoxW(
            std::ptr::null_mut(),
            text.as_ptr(),
            caption.as_ptr(),
            MB_OK | MB_ICONINFORMATION,
        );
    }
}
  1. 명령 프롬프트에서 cargo run을 실행합니다. img.png

  2. 릴리스 빌드를 하려면 cargo build --release를 실행합니다.

참고 자료

Licensed under CC BY-NC-SA 4.0
마지막 수정: 2026/11/24 00:00 +0900
comments powered by Disqus