Author Topic: Please, give win32 window application source  (Read 8975 times)

nobody

  • Guest
Please, give win32 window application source
« on: March 05, 2009, 11:48:42 AM »
Can anybody give the working win32 window application source code?
 Or please tell me where is the error.

%include 'win32n.inc';

extern MessageBoxA;
import MessageBoxA user32.dll;
extern LoadIconA;
import LoadIconA user32.dll;
extern RegisterClassA;
import RegisterClassA user32.dll;
extern RegisterClassExA;
import RegisterClassExA user32.dll;
extern CreateWindowExA;
import CreateWindowExA user32.dll;
extern ShowWindow;
import ShowWindow user32.dll;
extern UpdateWindow;
import UpdateWindow user32.dll;
extern GetMessageA;
import GetMessageA user32.dll;
extern TranslateMessage;
import TranslateMessage user32.dll;
extern DispatchMessageA;
import DispatchMessageA user32.dll;
extern DefWindowProcA;
import DefWindowProcA user32.dll;
extern GetModuleHandleA;
import GetModuleHandleA kernel32.dll;
extern ExitProcess;
import ExitProcess kernel32.dll;
extern GetLastError;
import GetLastError kernel32.dll;
extern FormatMessageA;
import FormatMessageA kernel32.dll;
extern LocalFree;
import LocalFree kernel32.dll;



;====Data Segment=====================================================================
section data use32 class=data;
  Caption db 'Hello', 0;
  Message db 'Hello, world!', 10, 13, 0;

ErrorCaption db 'Error  ', 0;
  msgErrorInRegisteringClass db 'Error in registering class', 0;

ClassName db 'Assembler', 0;

WindowClass : istruc WNDCLASS
                  at WNDCLASS.Style, dd CS_HREDRAW | CS_VREDRAW;
                  at WNDCLASS.lpfnWndProc, dd WinMain;
                  at WNDCLASS.cbClsExtra, dd 0;
                  at WNDCLASS.cbWndExtra, dd 0;
                  at WNDCLASS.hInstance, dd 0;
                  at WNDCLASS.hIcon, dd 0;
                  at WNDCLASS.hCursor, dd 0;
                  at WNDCLASS.hbrBackground, dd 0;
                  at WNDCLASS.lpszMenuName, dd 0;
                  at WNDCLASS.lpszClassName, dd ClassName;
                iend

WindowClassEx : istruc WNDCLASSEX
                    at WNDCLASSEX.cbSize, dd 48;
                    at WNDCLASSEX.Style, dd CS_HREDRAW | CS_VREDRAW;
                    at WNDCLASSEX.lpfnWndProc, dd WinMain;
                    at WNDCLASSEX.cbClsExtra, dd 0;
                    at WNDCLASSEX.cbWndExtra, dd 0;
                    at WNDCLASSEX.hInstance, dd 0;
                    at WNDCLASSEX.hIcon, dd 0;
                    at WNDCLASSEX.hCursor, dd 0;
                    at WNDCLASSEX.hbrBackground, dd COLOR_WINDOW;
                    at WNDCLASSEX.lpszMenuName, dd 0;
                    at WNDCLASSEX.lpszClassName, dd ClassName;
                    at WNDCLASSEX.hIconSm, dd 0;
                  iend

WindowMessage : istruc MSG
                    at MSG.hWnd, dd 0;
                    at MSG.Message, dd 0;
                    at MSG.wParam, dd 0;
                    at MSG.lParam, dd 0;
                    at MSG.Time, dd 0;
                    at MSG.pt, dd 0;
                  iend



;===Uninitialized data================================================================
section bss use32 class=data;
  WindowHandle resd 1;
  Error resd 1;
  ErrorString resd 1;

ErrorType resd 1;



;===Code Segment======================================================================
section code use32 class=code;
  ..start :

;---Registering windows class---------------------------------------------------------
  push DWORD 0;
  call [ GetModuleHandleA ];
  mov DWORD [ WindowClassEx+WNDCLASSEX.hInstance ], eax;

push DWORD WindowClassEx;
  call [ RegisterClassExA ];

mov BYTE [ ErrorCaption + 6 ], '1';

test eax, eax;
  jz NEAR ERROR;

;---Creating window-------------------------------------------------------------------
  push DWORD 0;
  call [ GetModuleHandleA ];

push LPCSTR 0;
  push HINSTANCE eax;
  push HMENU 0;
  push HWND 0;
  push INTEGER CW_USEDEFAULT;
  push INTEGER CW_USEDEFAULT;
  push INTEGER CW_USEDEFAULT;
  push INTEGER CW_USEDEFAULT;
  push DWORD WS_OVERLAPPEDWINDOW;
  push LPCSTR ClassName;
  push LPCSTR ClassName;
  push DWORD 0;
  call [ CreateWindowExA ];

mov BYTE [ ErrorCaption + 6 ], '2';

test eax, eax;
  jz NEAR ERROR;

mov [ WindowHandle ], eax;

;---Showing window--------------------------------------------------------------------
  push INTEGER SW_SHOW;
  push HWND eax;
  call [ ShowWindow ];

push HWND [ WindowHandle ];
  call [ UpdateWindow ];

;---Message loop----------------------------------------------------------------------
  MESSAGE_LOOP :
    push UINT 0;
    push UINT 0;
    push HWND NULL;
    push DWORD WindowMessage;
    call [ GetMessageA ];
    test eax, eax;
    jz QUIT;

push DWORD WindowMessage;
    call [ TranslateMessage ];

push DWORD WindowMessage;
    call [ DispatchMessageA ];

jmp SHORT MESSAGE_LOOP;

QUIT :
  push UINT NULL;
  call [ ExitProcess ];

;---Error Handling--------------------------------------------------------------------
  ERROR :
  call [ GetLastError ];
  mov [ Error ], eax;

push 0;
  push 0;
  push ErrorString;
  push 0;
  push DWORD [ Error ];
  push 0;
  push FORMAT_MESSAGE_ALLOCATE_BUFFER + FORMAT_MESSAGE_FROM_SYSTEM;
  call [ FormatMessageA ];

push UINT MB_OK;
  push LPCSTR ErrorCaption;
  push LPCSTR [ ErrorString ];
  push HWND NULL;
  call [ MessageBoxA ];

push ErrorString;
  call [ LocalFree ];

push UINT Error;
  call [ ExitProcess ];

;---Main Procedure--------------------------------------------------------------------
  WinMain :
    %define ebp_hWnd ebp + 8;
    %define ebp_Message ebp + 12;
    %define ebp_wParam ebp + 16;
    %define ebp_lParam ebp + 20;

cmp UINT [ ebp_Message ], WM_CREATE;
    je CREATE;

cmp UINT [ ebp_Message ], WM_CLOSE;
    je CLOSE;

push LPARAM [ ebp_lParam ];
    push WPARAM [ ebp_wParam ];
    push UINT [ ebp_Message ];
    push HWND [ ebp_hWnd ];
    call [ DefWindowProcA ];

;---Creating window---------------------------------------------------------------
    CREATE :
    jmp SHORT EXIT;

;---Closing window----------------------------------------------------------------
    CLOSE :

EXIT:
  leave
  retn 0;

Program compiles, but after loading it writes "Wrong window descriptor" and mistake occures after calling [ CreateWindowExA ].

nobody

  • Guest
Re: Please, give win32 window application source
« Reply #1 on: March 05, 2009, 01:28:56 PM »
I'm *way* over my head here!!!

I *think* you want to call GetModuleHandle once, as you've done, and stuff it into your WNDCLASSEX structure, as you've done. I *don't* think it's correct to call GetModuleHandle again, and use the new/different one to call CreateWindow. Try using the same hinstance you got from the first GetmoduleHandle...

Just a wild guess. In the "files" section of the Yahoo win32-nasm-users group, you should find examples, including Nasm translations of the examples from Iczelion's tutorials. Maybe they'd help get you off on the right track...

Best,
Frank

nobody

  • Guest
Re: Please, give win32 window application source
« Reply #2 on: March 05, 2009, 02:36:35 PM »
I've changed the call of [ CreateWindowExA ] to

push LPCSTR 0;
  push HINSTANCE [ WindowClassEx+WNDCLASSEX.hInstance ];
  push HMENU 0;
  push HWND 0;
  push INTEGER CW_USEDEFAULT;
  push INTEGER CW_USEDEFAULT;
  push INTEGER CW_USEDEFAULT;
  push INTEGER CW_USEDEFAULT;
  push DWORD WS_OVERLAPPEDWINDOW;
  push LPCSTR ClassName;
  push LPCSTR ClassName;
  push DWORD 0;
  call [ CreateWindowExA ];

But there is still the same problem. Similar program in FreePascal works very well. I've send the registration request to yahoo win32-nasm-users, but i think that such things like working source code, libraries and include files must be on the official site of nasm.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Please, give win32 window application source
« Reply #3 on: March 05, 2009, 09:26:49 PM »
Well... I disagree. Example code, include files, etc. are between you and your OS. Nasm isn't a "Windows tool", y'know, it's an "all of 'em" tool. You expect us to provide include files for OS/2, AtheOS BeOs, all the BSDs, Linux and MacOSX, etc. too?

However, since you asked nicely, and since we *do* have a lot of Windows users, and since there *does* seem to be demand for example code (that "multiboot.asm" gets downloaded way out of proportion to its usefulness)... I have just downloaded *all* of the example code, include files - Alink, several versions, NaGoa, etc., etc., etc. and uploaded a massive zip to the "example code" package here at SF. It is an impractically large download, IMHO. Totally undocumented as to what's in there - a lot of duplication, unless I'm mistaken. Someone needs to go through it and sort out what's "worthwhile" and document what's in there. But you want example code and include files, you got it!!!

Best,
Frank