winapiautoitcomputer-name

When calling GetComputerNameA, the program crash without errors


I am trying to call GetComputerNameA from AutoIt, this is my code:

Local $mystruct = DllStructCreate(toStr("struct;dword;char[1024];endstruct"))
DllStructSetData($mystruct, 1, 1024)
Local $result = DllCall("kernel32.dll", "int", "GetComputerNameA", "ptr", DllStructGetPtr($mystruct, 2), "ptr", DllStructGetPtr($mystruct, 1))
MsgBox(0, "AutoIt", "Success")  

But after I run it, it doesn't print anything, like the script crashed without errors.
Any idea why it failed to call it?


Solution

  • It has two parameters, specifically:

    BOOL GetComputerNameA(
      LPSTR   lpBuffer,
      LPDWORD nSize
    );
    

    The first is a LPSTR (pointer to a char array), the second is a LPDWORD (pointer to an unsigned int).

    Here is how you'd call that:

    Func _get_computer_name()
        Local $dll_struct = DllStructCreate("char[17]")
        
        $sz = DllStructCreate('int')
        Local $x = DllCall("kernel32.dll","int","GetComputerNameA","ptr",DllStructGetPtr($dll_struct),"int_ptr",DllStructGetPtr($sz))
    
        Return DllStructGetData($dll_Struct,1)
    EndFunc