Given the below code snippet, can anyone tell me why line 17 only prints "Name:"?
#include <Windows.h>
#include <stdio.h>
#pragma comment(lib, "Ws2_32.lib")
// Caller supplies function with a buffer to copy the name into as
// mentioned by nobugz here: https://social.msdn.microsoft.com/Forums/en-US/6547d2bf-2884-4ad2-b600-adaab0fa1031/warning-c4172-returning-address-of-local-variable-or-temporary?forum=Vsexpressvc
BOOL getName(int num, LPWSTR buffer) {
DWORD size = sizeof(buffer);
return GetComputerNameEx((COMPUTER_NAME_FORMAT)num, buffer, &size);
}
void main(int argc, char ** argv) {
printf("%s\n", "RAT POC RUNNING");
TCHAR buffer[256];
getName((COMPUTER_NAME_FORMAT)1, buffer);
printf("Name: %ws\n", buffer);
TCHAR hostName[MAX_COMPUTERNAME_LENGTH + 1];
DWORD hNsize = sizeof(hostName) / sizeof(hostName[0]);
GetComputerNameEx((COMPUTER_NAME_FORMAT)1, hostName, &hNsize);
printf("Hostname: %ws\n", hostName);
TCHAR domainName[MAX_COMPUTERNAME_LENGTH + 1];
DWORD dNsize = sizeof(domainName) / sizeof(domainName[0]);
GetComputerNameEx((COMPUTER_NAME_FORMAT)2, domainName, &dNsize);
printf("Domain name: %ws\n", domainName);
}
By setting a breakpoint at line 17, I find that the buffer
variable contains 256 entries of the value 52428, which I suspect is because I'm doing this in unicode. But I'm not sure why the GetComputerNameEx isn't filling said buffer with the actual name? Line 22, which calls the same GetComputerNameEx returns the correct name.