Window Creation

Tight_Coder_Ex 0 Tallied Votes 199 Views Share

I've often wondered why M$ didn't make creating a window a simple as registering a class in that only one 32 bit value need be passed to calling routine. Although this is not ground breaking code design it does facilitate calling code only set EBX to point to all values required by CreateWindowEx.

; 				    *** CREATE_WND ***

;	ENTRY:	EBX = Pointer to structure CreateWindowEx structure

;	LEAVE:	EAX = Handle to window, or null if failed
;		 CY = 0 Successful, 1 otherwise.

; ============================================================================================

  Create_Wnd	mov	ecx, 48
		sub	esp, ecx		; Create stack frame for CreateWindowEx
		mov	edx, esp		; Need this pointer in EDI
		push	esi
		push	edi			; Save registers
		mov	edi, edx
		mov	esi, ebx		; Setup for movsd
		shr	ecx, 2			; Number of dwords to move
		cld
		rep	movsd			; Copy parameters into stack area
		pop	edi
		pop	esi			; Restore index registers and create
		call	_CreateWindowExA@48	; window
		and	eax, eax
		jnz	.Done + 1		; If non zero, then was successful

	; Error handling code will eventually go here

  .Done		stc				; Set error flag
		ret

Dani AI

Generated

The assembly trick shown—pack the 12 CreateWindowEx parameters into a contiguous block, copy them to the call-area on the stack, then call the stdcall entry —is a neat, compact way to avoid a long sequence of push instructions. The @48 decoration on _CreateWindowExA@48 simply documents the stdcall name-mangling and the fact that 48 bytes (12 dwords) are the arguments. Nice idea, ; it makes wrapper code tidy and easy to reuse.

One critical issue to fix: the routine allocates stack space (sub esp,48) to hold the parameters but never restores the stack before returning. That will corrupt the caller's stack. Restore the allocation before returning — for example, after popping saved registers and after any error handling, add add esp, 48 (or use a standard prologue/epilogue: push ebp / mov ebp,esp ... leave / ret). Also, after a failed CreateWindowEx, call GetLastError() to get a diagnostic code rather than relying solely on flags; this is far more useful for debugging.

A few further practical notes: prefer the CreateWindowExW (Unicode) API in new code unless you specifically need ANSI. Make sure string pointers and class-name values you pass are valid for the duration of the call (class-name can be a string or an atom via MAKEINTRESOURCE). Be mindful of mixing this 32-bit stack-copy technique with 64-bit code: x64 Windows uses register-based parameter passing, so the same trick does not apply. When mixing with compiler-generated code that uses SSE, pay attention to the ABI and stack alignment requirements and test under a debugger to confirm esp before/after the wrapper.

Good direction overall — tidy wrappers like this are useful for an assembler GUI "Hello World" as suggested, provided the stack balance, charset choice, and error reporting are handled correctly.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Nice idea!
We have a little thread going on DaniWeb under: http://www.daniweb.com/techtalkforums/thread18331.html
Would be nice to get the asm Hello World! from you.

Tight_Coder_Ex 17 Posting Whiz in Training

I've never done anything in assembly for console, although the concept is very simple. This series I'm doing now will only have another couple of posts and it will be complete as far as displaying a window on the monitor. What I can do and what I think might be fairly informative is displaying "Hello World" in the default GUI font only while the left mouse button is being pressed at the cursor position at that time. This will be a little more consitent with my objective of this series of snippets. At that time I will post the entire source in thread18331. The complete source encorporates all the snippets I'm posting here.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

The thread is not limited to console only. Please go ahead and give us a good one! Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.