Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Printing computer name using GetComputerNameW

I am new to Windows APIs. I am doing an exercise on printing the computer name to the console. Here is the code I have so far:

wchar_t compName[MAX_COMPUTERNAME_LENGTH+1];
DWORD maxSize = MAX_COMPUTERNAME_LENGTH+1 ;
BOOL getName = GetComputerNameW(compName, &maxSize);

wprintf(L"Computer name is: %s",compName);

The issue is on the output. I only see Computer name is D on the console, when I should see DESKTOP-XXXXXX.

What am I doing wrong? I believe I am calling the right functions, per the MSDN docs. I have seen posts on StackOverflow regarding this issue, but it is in C++ and not C.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I feel like it has something to do with me printing a Unicode string.

>Solution :

The format specifier %s is for printing a string pointed at by char*, even in wprintf.

You should use format specifier %ls to print a string of wide characters.

printf, _printf_l, wprintf, _wprintf_l | Microsoft Learn

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading