1 Replies - 746 Views - Last Post: 03 September 2012 - 04:29 PM

#1 Hooker  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 29
  • Joined: 17-August 12

Error while assembling code which displays computer name

Posted 02 September 2012 - 11:30 PM

.data
appname db "my first program in masm",0
buffer db 100 dup(0)
.code
start:
invoke GetComputerName,addr buffer,100
invoke MessageBox,NULL,addr buffer,addr appname,MB_OK
invoke ExitProcess,0
end start


error while assembling in line 6, what may the error in code.
Is This A Good Question/Topic? 0
  • +

Replies To: Error while assembling code which displays computer name

#2 GunnerInc  Icon User is online

  • "Hurry up and wait"
  • member icon




Reputation: 856
  • View blog
  • Posts: 2,247
  • Joined: 28-March 11

Re: Error while assembling code which displays computer name

Posted 03 September 2012 - 04:29 PM

What error are you getting? I do not get an error when Assembling but it will not work because the second parameter to GetComputerName is wrong!

Quote

lpnSize
[in, out] On input, specifies the size of the buffer, in TCHARs. On output, the number of TCHARs copied to the destination buffer, not including the terminating null character.


When docs say in/out for a parameter, you need to pass an address of a buffer, so you fill this buffer with the size of the buffer for lpBuffer, in your case the size of buffer, and when GetComputerName returns, it will fill the buffer you pass for lpnSize with the number of characters it put in your buffer.

Here is a little tip, when you put an initialized buffer in your data section, it takes makes your final exe bigger by that size you make that variable. For buffers, put them in your bss section (.data?) which does not make your exe bigger.

So, your code should look like this:
.data
appname db "my first program in masm",0

.data?
lpnSize     dd  ?
buffer      db 100 dup(0)

.code
start:
    mov     lpnSize, 100
    invoke  GetComputerName, offset buffer, offset lpnSize
    invoke  MessageBox, NULL, offset buffer, offset appname, MB_OK
    invoke  ExitProcess, 0
end start


Your really only need to use ADDR for local variables (stack variables) use OFFSET for everything else, it makes your code a bit more self documenting. If I see ADDR, then I know it is a local variable to the proc, if I see OFFSET then I know it is a variable in the .data or .data? section.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1