flat assembler
Message board for the users of flat assembler.
Index
> Windows > GetComputerName function |
Author |
|
vid 17 Sep 2007, 10:38
try this master:
Code: NAME_SIZE = 100 name rb NAME_SIZE ... push NAME_SIZE push name stdcall [GetComputerName] push MB_OK push title0 push name push 0 stdcall [MessageBox] ... your problem was 1. you declared buffer as "dw ?". 2. you declared size of buffer as variable - not needed. 3. when calling GetComputerName, instead of size of buffer, you Pushed address of variable which holds size of buffer. 4. You didn't check return value from GetComputerName. Doing so will reveal where problem is. |
|||
17 Sep 2007, 10:38 |
|
Master0fAsm 17 Sep 2007, 20:17
Quote:
You mean making my program "robust" by checking to see if something goes wrong and taking the proper actions either by fixing the problem or, in this case, jumping to the exit: label. I intend to make the program robust after I've fully comprehended how to implement this function correctly. Code: format pe gui 4.0 entry start include 'win32a.inc' NAME_SIZE = 100 section '.idata'import data readable writeable library kernel, 'kernel32.dll', \ user32, 'user32.dll' import kernel, \ ExitProcess, 'ExitProcess', \ GetComputerName, 'GetComputerNameA' import user32, \ MessageBox, 'MessageBoxA' section '.data' data readable writeable title0 db "Computer's name",0 name rb NAME_SIZE section '.text' code readable executable start: push NAME_SIZE push name stdcall [GetComputerName] st000: push MB_OK push title0 push name push 0 stdcall [MessageBox] exit: push 0 stdcall [ExitProcess] It compiles well, yet when I attempt to run the program, it says this: Quote:
|
|||
17 Sep 2007, 20:17 |
|
vid 17 Sep 2007, 20:38
Quote: You mean making my program "robust" by checking to see if something goes wrong and taking the proper actions either by fixing the problem or, in this case, jumping to the exit: label. I intend to make the program robust after I've fully comprehended how to implement this function correctly. By making program more robust, you will discover bugs like this immediately as you produce them, so writing a more robust code is often actually faster than writing less robust code. At least with larger project. About the bug: sorry, my mistake, i was lazy to read about function properly. The second argument SHOULD be pointer to variable, that holds size of input buffer, not the size directly. Quote: This value should be large enough to contain MAX_COMPUTERNAME_LENGTH + 1 characters. As for what-to-do-on-unexpected-error problem, you can use code like this to display cause of error: Code: _error db "Error!",0 errmsg rb 500 ;500 byte buffer for error message ;======================================================== ; error handling win_error: ;get error code of last error call [GetLastError] push 0 ;ignored push 500 ;size of buffer push errmsg ;buffer for message push 0 ;language = use local language push eax ;message id = error code push 0 ;source = ignored with FORMAT_MESSAGE_FROM_SYSTEM push FORMAT_MESSAGE_FROM_SYSTEM ;we want to get error string from error code call [FormatMessageA],\ ;display error push MB_ICONERROR push _error push errmsg push 0 call [MessageBoxA] ;exit process with error code 1 push 1 call [ExitProcess] Warning: Only use this code on errors returned from WinAPI, when you are sure that "last error" is set. Otherwise, GetLastError can return some random value. |
|||
17 Sep 2007, 20:38 |
|
Master0fAsm 19 Sep 2007, 03:43
Took me a while to get it, but I've finally got it now (it's robust, to).
Note: The concatting procedure I used wasn't needed since I only used it one time. But, I copied it from somewhere else and I didn't know how to implement it myself. I could've saved some space and optimized my program, but it didn't need optimization. It wasn't a time-critical routine (speed didn't matter here), let alone, a meaningful program. I just wanted to write a program that displayed the computer's name: Code: format pe gui 4.0 entry s00 include 'win32a.inc' section '.idata'import data readable writeable library kernel, 'kernel32.dll', \ user32, 'user32.dll' import kernel, \ ExitProcess, 'ExitProcess', \ GetComputerName, 'GetComputerNameA' import user32, \ MessageBox, 'MessageBoxA' TRUE equ 1 section '.vars000' data readable writeable title0 db "Computer's name",0 msg00 db "The Computer's name is: ",0 err0 db "There was an error in retriving",13d db "the computer's name.",0 name00 rb 26d ;he needs a name nameSize dd 26d ;a limit to the # of ;characters in his name theBuffer db (32*28) dup (?) ;a new variable which will be msg00 + name000 section '.nameHim' code readable executable s00: invoke GetComputerName,\ ;get our thing's name name00,\ ;our thing's new name (in name00) nameSize ;how many characters in name00's name cmp eax,TRUE jne e01 j000: stdcall Concat,msg00,name00,theBuffer j001: push MB_OK push title0 push theBuffer ;msg00 + name00 push 0 stdcall [MessageBox] cmp eax,0 je e01 ;if not enough memory jmp e00 e01: invoke MessageBox,\ ;if something happens while retriving name 0,\ err0,\ title0,\ MB_ICONERROR e00: push 0 stdcall [ExitProcess] proc Concat uses esi edi, @AdrSrc1, @AdrSrc2, @AdrDest mov esi,[@AdrSrc1] mov edi,[@AdrDest] .concat_src1: movsb cmp byte[esi],0 jne .concat_src1 mov esi,[@AdrSrc2] .concat_src2: movsb cmp byte[esi],0 jne .concat_src2 movsb ret endp |
|||
19 Sep 2007, 03:43 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.