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

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

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

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

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

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
winapi = "0.2.7"
user32-sys = "0.2.0"
  1. src\main.rs를 열고 다음과 같이 수정합니다.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
extern crate user32;
extern crate winapi;

use std::ffi::CString;
use user32::MessageBoxA;
use winapi::winuser::{MB_OK, MB_ICONINFORMATION};

fn main() {
    let lp_text = CString::new("Hello, world!").unwrap();
    let lp_caption = CString::new("MessageBox Example").unwrap();

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

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

참고

Hello World MesssageBox example in Rust

comments powered by Disqus