News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Sources to study Windows API coding

Started by Vortex, November 19, 2023, 10:52:25 PM

Previous topic - Next topic

jj2007

Quote from: Vortex on November 19, 2023, 10:52:25 PMhttps://zetcode.com/gui/winapi/strings/

Quoteansic_functions.c
#include <windows.h>
#include <wchar.h>

#define STR_EQUAL 0

int wmain(void) {

    wchar_t str1[] = L"There are 15 pines";

    wprintf(L"The length of the string is %lld characters\n", wcslen(str1));

    wchar_t buf[20];
    wcscpy(buf, L"Wuthering");
    wcscat(buf, L" heights\n");
    wprintf(buf);

Good stuff :thumbsup:

One day, when all my projects are running without glitches, I'll write a C->MASM converter :rolleyes:

A Simple Window:
        case WM_CLOSE:
            DestroyWindow(hwnd);

Does anybody use DestroyWindow?

IMHO Assembly is simpler:

include \masm32\include\masm32rt.inc        ; **** load standard Masm32 includes ****
.data    ; initialised data section
wcx    WNDCLASSEX <WNDCLASSEX, 0, WndProc, 0, 0, 1, 2, 3, COLOR_BTNFACE+1, 0, txClass, 4>
txClass    db "Masm32GUI", 0            ; class name, will be registered below
.code
WinMain proc
LOCAL msg:MSG
  wc equ [ebx.WNDCLASSEX]            ; we use an equate for better readability
  mov ebx, offset wcx
  mov wc.hInstance, rv(GetModuleHandle, 0)            ; rv ("return value") is a Masm32 macro
  mov wc.hIcon, rv(LoadIcon, eax, IDI_APPLICATION)
  mov wc.hIconSm, eax            ; the rv macro returns results in eax
  mov wc.hCursor, rv(LoadCursor, NULL, IDC_ARROW)    ; get a cursor
  invoke RegisterClassEx, addr wc            ; the window class needs to be registered
  invoke CreateWindowEx, WS_EX_ACCEPTFILES, wc.lpszClassName, chr$("Hello World"),     ; set window title here
     WS_OVERLAPPEDWINDOW or WS_VISIBLE,
     600, 150, 600, 400,        ; window position: x, y, width, height
     NULL, rv(LoadMenu, wc.hInstance, 100), wc.hInstance, NULL
  .While 1
    invoke GetMessage, addr msg, 0, 0, 0
    inc eax
    shr eax, 1
    .Break .if Zero?    ; 0 (OK) or -1 (error)
    invoke TranslateMessage, addr msg
    invoke DispatchMessage, addr msg
  .Endw
  invoke ExitProcess, msg.wParam
WinMain endp
WndProc proc uses esi edi ebx hWnd, uMsg, wParam:WPARAM, lParam:LPARAM
  .if uMsg==WM_DESTROY
    invoke PostQuitMessage, NULL
  .endif
  invoke DefWindowProc, hWnd, uMsg, wParam, lParam    ; default processing
  ret
WndProc endp
end WinMain

Vortex

#2
Hi Jochen,

Thanks. Here is an interesting article :

https://swedishembedded.com/gpt-translate-assembly/

jj2007

Let's try:
QuoteCan you please translate the following code to MASM syntax?
QuoteCertainly! Below is the MASM syntax version of the provided code:

assembly
Copy code
include windows.inc
include user32.inc
include kernel32.inc

.data
g_szClassName db "myWindowClass",0

.data?
hInstance HINSTANCE ?
hPrevInstance HINSTANCE ?
lpCmdLine LPSTR ?
nCmdShow DWORD ?
hwnd HWND ?
Msg MSG <>

.code
WndProc proc hwnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
    cmp uMsg, WM_CLOSE
    je  @WM_CLOSE
    cmp uMsg, WM_DESTROY
    je  @WM_DESTROY
    jmp @Default

@WM_CLOSE:
    invoke DestroyWindow, hwnd
    jmp @EndSwitch

@WM_DESTROY:
    invoke PostQuitMessage, 0
    jmp @EndSwitch

@Default:
    invoke DefWindowProc, hwnd, uMsg, wParam, lParam
    ret

@EndSwitch:
    xor eax, eax
    ret
WndProc endp

WinMain proc
    ; Step 1: Registering the Window Class
    invoke GetModuleHandle, NULL
    mov hInstance, eax

    mov wc.cbSize, sizeof WNDCLASSEX
    mov wc.style, 0
    mov wc.lpfnWndProc, offset WndProc
    mov wc.cbClsExtra, 0
    mov wc.cbWndExtra, 0
    mov wc.hInstance, hInstance
    mov wc.hIcon, LoadIcon, NULL, IDI_APPLICATION
    mov wc.hCursor, LoadCursor, NULL, IDC_ARROW
    mov wc.hbrBackground, COLOR_WINDOW + 1
    mov wc.lpszMenuName, 0
    mov wc.lpszClassName, offset g_szClassName
    mov wc.hIconSm, LoadIcon, NULL, IDI_APPLICATION

    invoke RegisterClassEx, addr wc
    test eax, eax
    jz @ClassRegistrationFailed

    ; Step 2: Creating the Window
    invoke CreateWindowEx, WS_EX_CLIENTEDGE,
        offset g_szClassName,
        offset "The title of my window",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
        NULL, NULL, hInstance, NULL

    mov hwnd, eax
    test hwnd, hwnd
    jz @WindowCreationFailed

    invoke ShowWindow, hwnd, nCmdShow
    invoke UpdateWindow, hwnd

    ; Step 3: The Message Loop
    mov eax, 0
    messageLoop:
        invoke GetMessage, addr Msg, NULL, 0, 0
        test eax, eax
        jle @EndMessageLoop

        invoke TranslateMessage, addr Msg
        invoke DispatchMessage, addr Msg
        jmp messageLoop

    @EndMessageLoop:

    ; Return wParam of the last message to WinMain's caller
    mov eax, Msg.wParam
    ret

@ClassRegistrationFailed:
    invoke MessageBox, NULL, offset "Window Registration Failed!", offset "Error!", MB_ICONEXCLAMATION or MB_OK
    xor eax, eax
    ret

@WindowCreationFailed:
    invoke MessageBox, NULL, offset "Window Creation Failed!", offset "Error!", MB_ICONEXCLAMATION or MB_OK
    xor eax, eax
    ret

WinMain endp
end WinMain
Please note that this translation is a basic conversion to MASM syntax, and there might be variations depending on the specific assembler you are using. Additionally, some WinAPI functions might require additional setup or error handling depending on the context of your application.

Pretty close but far from perfect...