flat assembler
Message board for the users of flat assembler.

Index > Windows > simple win64 program

Goto page 1, 2  Next
Author
Thread Post new topic Reply to topic
tixvin



Joined: 13 Apr 2013
Posts: 12
tixvin 13 Apr 2013, 20:08
I'm trying to create a window but this code fails. Who knows why?
Code:
;*********************************************************
  format PE64 GUI 5.0
  entry start

  include 'win64a.inc'

 section '.text' code readable executable
 start:
  sub rsp, 8

  mov  rax, fproz
  mov [wc.lpfnWndProc], rax
  xor rcx, rcx
 call [GetModuleHandleA]
  mov [wc.hInstance], rax
  mov [wc.lpszClassName], kls
  mov rcx, wc
  sub rsp, 20h
  call [RegisterClassA]
  add rsp, 20h

   mov rcx, 0   
   mov rdx, kls
   mov r8, titel
   mov r9, WS_OVERLAPPEDWINDOW
  push 0
  push [wc.hInstance]
  push 0
  push 0
  push 128
  push 128
  push 64
  push 64
   sub rsp, 20h
  call [CreateWindowExA]
   add rsp, 8*12
   mov [fnstr], rax

   mov rcx, [fnstr]
   xor rdx, rdx
   inc rdx
   sub rsp, 20h
   call [ShowWindow]
   add rsp, 20h


  fproz:
  xor rax,  rax
 ret

;*******************************
  section '.data' data readable writeable
   fnstr dq 0
   wc WNDCLASS
 titel db 'schach window',0
    kls db 'klass', 0

   section '.idata' import data readable writeable

  library kernel32,'KERNEL32.DLL',\
  user32,'USER32.DLL'

   include 'api\kernel32.inc'
  include 'api\user32.inc'    
edit by revolution: Added code tags


Last edited by tixvin on 14 Apr 2013, 04:42; edited 1 time in total
Post 13 Apr 2013, 20:08
View user's profile Send private message Send e-mail ICQ Number Reply with quote
ACP



Joined: 23 Sep 2006
Posts: 204
ACP 13 Apr 2013, 22:16
One thing is that according to MSDN you should end your program by invoking ExitProcess() even if other methods work. Secondlyg shouldn't you use call [ShowWindow]? Please use code tags for posting code so it is a bit more readable.

This is just a quick peek into your code so there maybe other issues as well.

Is there any exception when you run your code under debugger?
Post 13 Apr 2013, 22:16
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4043
Location: vpcmpistri
bitRAKE 13 Apr 2013, 22:46
ShowWindow isn't needed if you use WS_VISIBLE flag on window.

Please use [code] [/code] tags.

_________________
¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup
Post 13 Apr 2013, 22:46
View user's profile Send private message Visit poster's website Reply with quote
HaHaAnonymous



Joined: 02 Dec 2012
Posts: 1178
Location: Unknown
HaHaAnonymous 13 Apr 2013, 23:41
[ Post removed by author. ]


Last edited by HaHaAnonymous on 28 Feb 2015, 21:05; edited 1 time in total
Post 13 Apr 2013, 23:41
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4043
Location: vpcmpistri
bitRAKE 13 Apr 2013, 23:45
Process creation can create other threads. RET would only end the current thread - which is a valid way to end a thread, btw, Yet, other threads just hang in the process and do whatever. So, to reiterate: Windows and loaded DLLs can create threads in your programs process space (this is not under your control), and ExitProcess is needed to end those threads.

I have an internal belief that there is some memory clean-up involved as well, but I don't have anything concrete to say about that.

_________________
¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup
Post 13 Apr 2013, 23:45
View user's profile Send private message Visit poster's website Reply with quote
HaHaAnonymous



Joined: 02 Dec 2012
Posts: 1178
Location: Unknown
HaHaAnonymous 14 Apr 2013, 01:13
[ Post removed by author. ]


Last edited by HaHaAnonymous on 28 Feb 2015, 21:05; edited 1 time in total
Post 14 Apr 2013, 01:13
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4043
Location: vpcmpistri
bitRAKE 14 Apr 2013, 03:17
Program crashing is different than a run-away program. Program crash signals a need to respond to an error. This doesn't happen in the case of a run-away program. The computer can't solve the halting problem - It needs to be told something is wrong.

Here, try it out:
Code:
format PE64 GUI 5.0 at $543210000
include '%INCLUDE%\win64wxp.inc'

.code

At_The_Start:
    invoke CreateThread,0,1,BusyThread,0,0,0
    invoke Sleep,1000

    xor eax,eax
    div eax ; comment this to leave a thread hanging...

    pop rax ; FASM macro put a QWORD on stack
    retn

    align 64
BusyThread:

@@: call @F
    jmp @B

@@: enter 0,0
    leave
    retn

.end At_The_Start    

_________________
¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup
Post 14 Apr 2013, 03:17
View user's profile Send private message Visit poster's website Reply with quote
HaHaAnonymous



Joined: 02 Dec 2012
Posts: 1178
Location: Unknown
HaHaAnonymous 14 Apr 2013, 03:42
[ Post removed by author. ]


Last edited by HaHaAnonymous on 28 Feb 2015, 21:05; edited 1 time in total
Post 14 Apr 2013, 03:42
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4043
Location: vpcmpistri
bitRAKE 14 Apr 2013, 04:07
I thought you'd say that. Rolling Eyes

Coding small programs, I still use RETN and cross my fingers that no other thread is created, or just don't end the program - force the user to kill the process. That way it seems like a "feature". Whereas the error looks like "bad" programming.
Cool

IMHO, there are accessibility tools which create thread attached to processes. So, using RETN causing problems for people using IME, or the handicap.

_________________
¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup
Post 14 Apr 2013, 04:07
View user's profile Send private message Visit poster's website Reply with quote
tixvin



Joined: 13 Apr 2013
Posts: 12
tixvin 14 Apr 2013, 04:47
the windows event viewer tells about the mistake 0xc0000005

its about memory access violation
Post 14 Apr 2013, 04:47
View user's profile Send private message Send e-mail ICQ Number Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20342
Location: In your JS exploiting you and your system
revolution 14 Apr 2013, 04:58
HaHaAnonymous wrote:
Good! Then I can exit my program by intentionally crashing it.
Of course. No need to use brakes to stop your car when you can use a brick wall instead.
Post 14 Apr 2013, 04:58
View user's profile Send private message Visit poster's website Reply with quote
tixvin



Joined: 13 Apr 2013
Posts: 12
tixvin 14 Apr 2013, 05:44
so does anybody really know whats wrong here with code
Post 14 Apr 2013, 05:44
View user's profile Send private message Send e-mail ICQ Number Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20342
Location: In your JS exploiting you and your system
revolution 14 Apr 2013, 06:08
tixvin: We don't know what you mean by fails.

But anyhow, there is at least one problem that I can see.

1) There appears to be no message loop to pass commands to the window procedure. You just quit directly after creating the window. Where is your window procedure? Have a look in the sub-folders of the fasm download for examples of creating and managing a window in 64-bit mode.
Post 14 Apr 2013, 06:08
View user's profile Send private message Visit poster's website Reply with quote
tixvin



Joined: 13 Apr 2013
Posts: 12
tixvin 14 Apr 2013, 06:12
this code just create a window nothing more
Ii looked through win64 examples and I do not want to use invoke I just want to use pure asm commands

as for fail the window does not appear at all and exit with error 0xc0000005
Post 14 Apr 2013, 06:12
View user's profile Send private message Send e-mail ICQ Number Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20342
Location: In your JS exploiting you and your system
revolution 14 Apr 2013, 06:16
Sure you create a window but then you immediately do a ShowWindow and then quit the program.

I'm not talking about invoke, I'm talking about having a valid windows procedure to process the command necessary to manage a window, and having a valid message loop to pass those command to the window. Without those two things you will never get a window to display anything.
Post 14 Apr 2013, 06:16
View user's profile Send private message Visit poster's website Reply with quote
tixvin



Joined: 13 Apr 2013
Posts: 12
tixvin 14 Apr 2013, 06:28
this is simply a copy of this c program in asm

Here is the complete code to create the window. Remember that WindowProc is still just a forward declaration of a function.

Копировать

Code:
    // Register the window class.
    const wchar_t CLASS_NAME[]  = L"Sample Window Class";
    
    WNDCLASS wc = { };

    wc.lpfnWndProc   = WindowProc;
    wc.hInstance     = hInstance;
    wc.lpszClassName = CLASS_NAME;

    RegisterClass(&wc);

    // Create the window.

    HWND hwnd = CreateWindowEx(
        0,                              // Optional window styles.
        CLASS_NAME,                     // Window class
        L"Learn to Program Windows",    // Window text
        WS_OVERLAPPEDWINDOW,            // Window style

        // Size and position
        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,

        NULL,       // Parent window    
        NULL,       // Menu
        hInstance,  // Instance handle
        NULL        // Additional application data
        );

    if (hwnd == NULL)
    {
        return 0;
    }

    ShowWindow(hwnd, nCmdShow);

    



Congratulations, you've created a window! Right now, the window does not contain any content or interact with the user. In a real GUI application, the window would respond to events from the user and the operating system. The next section describes how window messages provide this sort of interactivity.
Post 14 Apr 2013, 06:28
View user's profile Send private message Send e-mail ICQ Number Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 14 Apr 2013, 08:42
tixvin,
MSDN wrote:
…An application-defined window procedure should pass any messages that it does not process to the DefWindowProc function for default processing.
Anyway, without message pump your window won't last long. It would be destroyed during standard cleanup after your program ends.

Your example in C is incomplete, maybe you'd overlooked something (outside posted code) when translating to fasm?
Post 14 Apr 2013, 08:42
View user's profile Send private message Reply with quote
tixvin



Joined: 13 Apr 2013
Posts: 12
tixvin 14 Apr 2013, 08:49
Post 14 Apr 2013, 08:49
View user's profile Send private message Send e-mail ICQ Number Reply with quote
tixvin



Joined: 13 Apr 2013
Posts: 12
tixvin 14 Apr 2013, 10:07
function WindowsCreateEx returns zero- fail creating window
Post 14 Apr 2013, 10:07
View user's profile Send private message Send e-mail ICQ Number Reply with quote
ACP



Joined: 23 Sep 2006
Posts: 204
ACP 14 Apr 2013, 10:22
tixvin wrote:
the windows event viewer tells about the mistake 0xc0000005

its about memory access violation


You haven't read my post - have you? If you would run your code under debugger (WinDbg for example) you would find out that access valiotion happens due to wrong call address which is result of:

Code:
call ShowWindow    
Post 14 Apr 2013, 10:22
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2  Next

< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.