It probably didn't work because the 'ptr' parameter is an in/out parameter - you need to set the value pointed-to to the length of the buffer (1024 in this case):
make memblock 1, 1024 : `1023 characters + null char is probably overkill though
ptr = make memory(4) : `Will hold the string size
*ptr = 1024 : ` <--- set the buffer size
load dll "ADVAPI32.DLL", 1
call dll 1, "GetUserNameA", get memblock ptr(1), ptr
for i = 0 to (*ptr)
username$ = username$ + chr$(memblock byte(1, i))
next i
`Print result
print username$
`Clear resources
delete memory ptr
delete memblock 1
wait key
end
There's also an easier way to get strings from some windows API's - you can pre-size a string and simply pass that string as a parameter:
load dll "advapi32", 1
UserName as string
UserName = space$( 256 ) ` <--- presize a string (256 + terminator)
ptr = alloc zeroed(4)
*ptr = 257 ` <--- the size of the buffer including the terminator
call dll 1, "GetUserNameA", UserName, ptr
free ptr
print UserName
print ""
print "Done"
wait key
end
If you don't have ALLOC ZEROED/FREE then 1) Why don't you have my plug-ins

? 2) Substitute MAKE MEMORY/DELETE MEMORY as per the original snippet.
Finally, there's a
WINDOWS USERNAME$ function in my plug-ins that does this for you