查看: 346|回复: 0

[C/C++] 【求助】WINAPI GetComputerName报错

[复制链接]

0

技术

9

魅力

1

原创

版主

禁止访问

Rank: 7Rank: 7Rank: 7

积分
7674
人气
176
分享
52

最佳新人活跃会员

发表于 2023-12-30 21:09:48 | 显示全部楼层 |阅读模式
如题,目前已经确认了动态内存分配没有问题,SetText没有问题(上面还有一个格式一摸一样的MyGetUserName)

GetComputerName时,会返回FALSE
[C] 纯文本查看 复制代码
char *MyGetComputerName() {
    char *computerName = (char *)malloc(256 *sizeof(char));
    DWORD strSize = sizeof(computerName);
    if (computerName == NULL) {
        // 内存分配失败
        SetText(RED);
        printf(
            "[Fatal] in %s, line %d, the function %s, the variable %s's memory not allocated correctly\n",
            __FILE__,__LINE__,"MyGetComputerName(char*)","computerName(char*)"
        );
        SetText(WHITE);
        return NULL;
    }

    if (GetComputerName(computerName, &strSize)) {
        return computerName;
    } else {
        SetText(RED);
        printf(
            "[Fatal] in %s, line %d, the function %s, the variable %s's value not assignment correctly\n",
            __FILE__,__LINE__,"MyGetComputerName(char*)","computerName(char*)"
        );
        SetText(WHITE);
        return NULL;
    }
}

小菜鸡一枚~
Gitee主页:https://gitee.com/juruoqwq
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表