flat assembler
Message board for the users of flat assembler.

Index > Windows > GetComputerName function

Author
Thread Post new topic Reply to topic
Master0fAsm



Joined: 01 Sep 2007
Posts: 11
Location: Darlington, South Carolina [USA]
Master0fAsm 17 Sep 2007, 10:13
I want to make a program that retrieves the computer's name and displays it in a message box. I've tried, but I just can't seem to get it to work:

Code:
format pe gui 4.0
entry start

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'

section '.data' data readable writeable
title0 db "Computer's name",0
lpB dw ?
nSiz dw 17

section '.text' code readable executable
start:  push [nSiz]
        push [lpB]
        stdcall [GetComputerName]

st000:  push MB_OK
        push title0
        push lpB
        push 0
        stdcall [MessageBox]

exit:   push 0
        stdcall [ExitProcess]
    


Here is the GetComputerName function:

Quote:

The GetComputerName function retrieves the computer name of the current system. This name is established at system startup, when it is initialized from the registry.

BOOL GetComputerName(

LPTSTR lpBuffer, // address of name buffer
LPDWORD nSize // address of size of name buffer
);
Parameters

lpBuffer

Points to a buffer to receive the null-terminated character string containing the computer name.

nSize

Points to a variable that specifies the maximum size, in characters, of the buffer. This value should be large enough to contain MAX_COMPUTERNAME_LENGTH + 1 characters.

Return Value

If the function succeeds, the return value is TRUE and the variable at nSize contains the number of characters copied to the destination buffer, not including the terminating null character.
If the function fails, the return value is FALSE.
Post 17 Sep 2007, 10:13
View user's profile Send private message Yahoo Messenger Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
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.
Post 17 Sep 2007, 10:38
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Master0fAsm



Joined: 01 Sep 2007
Posts: 11
Location: Darlington, South Carolina [USA]
Master0fAsm 17 Sep 2007, 20:17
Quote:

4. You didn't check return value from GetComputerName. Doing so will reveal where problem is.


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:

filey.EXE has encountered a problem and needs to close. We are sorry for the inconvenience.
Post 17 Sep 2007, 20:17
View user's profile Send private message Yahoo Messenger Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
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.
According to this, you should declare NAME_SIZE = MAX_COMPUTERNAME_LENGTH + 1 (provided that the MAX_COMPUTERNAME_LENGTH is defined in headers).

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.
Post 17 Sep 2007, 20:38
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Master0fAsm



Joined: 01 Sep 2007
Posts: 11
Location: Darlington, South Carolina [USA]
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
    
Post 19 Sep 2007, 03:43
View user's profile Send private message Yahoo Messenger Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< 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.