简体   繁体   中英

Storing the output of GetComputerNameA in a structure in C++

How would I go about storing the output of GetComputerNameA in a structure with a character array in c++? I don't know, I think I have to use lpstrcpy?

I'm not too familiar with using objects in c++.

As per GetComputerNameA documentation, char array size should be large enough to contain MAX_COMPUTERNAME_LENGTH + 1 characters.

#include <windows.h>
#include <stdio.h>

struct Computer
{
    char name[MAX_COMPUTERNAME_LENGTH + 1 ];
};


void main()
{
    DWORD cchComputerName = MAX_COMPUTERNAME_LENGTH + 1;
    Computer computer = { 0 };

    if (!GetComputerNameA(&computer.name[0], &cchComputerName))
    {
        printf("Failed to get computer name, Error: %u\r\n", GetLastError());
    }
    else
    {
        printf("Computer Name: %s\r\n", computer.name);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM