I'm trying to call GetUserNameEx
from secur32.dll
like this:
dll, err := syscall.LoadDLL("secur32.dll")
if err != nil {
log.Fatal(err)
}
defer dll.Release()
GetUserNameEx, err := dll.FindProc("GetUserNameExW")
if err != nil {
log.Fatal(err)
}
arr := make([]uint8, 256)
var size uint
GetUserNameEx.Call(3, uintptr(unsafe.Pointer(&arr[0])), uintptr(unsafe.Pointer(&size)))
fmt.Println(arr)
fmt.Println(size)
This code compiles fine, but GetUserNameEx.Call()
will fail. I don't know why I cannot get UserName
. Could anyone help me?
size
is an in-out parameter. When you make the call, you have to set it to the size of the buffer (arr
). Also its type isPULONG
, so in Go useuint32
. WindowsPULONG
type is a pointer to aULONG
(which has range0..4294967295
). See source.Also
Call()
returns 3 values:Store the returned
lastErr
and print it. Would you have done so, you would find the error earlier:Prints:
This means more data is available than what fits into the buffer you pass - or rather - by the size you indicated with the in-out parameter
size
(you passed0
as thesize
).Working code (note the division by 2 due to unicode and minus 1 for the terminating
'\0'
byte / character for the size calculation):In this case
lastErr
will be:To properly handle error:
Example: