使用 GetComputerObjectNameW win API 时出现编译错误
Posted
技术标签:
【中文标题】使用 GetComputerObjectNameW win API 时出现编译错误【英文标题】:Compilation error when using GetComputerObjectNameW win API 【发布时间】:2021-02-19 08:57:31 【问题描述】:我在 Visual Studio 上收到以下代码的编译错误。请帮助我理解问题。 如果您没有 Visual Studio,您可以尝试通过以下链接编译代码 https://rextester.com/l/cpp_online_compiler_visual
#include <iostream>
#include <atlbase.h>
#include <secext.h>
int main()
EXTENDED_NAME_FORMAT enf = NameFullyQualifiedDN;
LPWSTR pwszComputerName;
DWORD dwLen;
dwLen = 0;
GetComputerObjectNameW(enf, NULL, &dwLen);
pwszComputerName = new WCHAR[dwLen + 1];
if(NULL == pwszComputerName)
return 0;
if(!GetComputerObjectNameW(NameSamCompatible, pwszComputerName, &dwLen))
delete pwszComputerName;
return 0;
sbstrTrustee = pwszComputerName;
wprintf(L"GetComputerObjectName: %s\n", pwszComputerName);
delete pwszComputerName;
return 0
错误如下。
C:\Program Files (x86)\Windows Kits\8.1\include\\shared\secext.h(117): error C2146: syntax error: missing ';' before identifier 'GetUserNameExA'
C:\Program Files (x86)\Windows Kits\8.1\include\\shared\secext.h(121): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
C:\Program Files (x86)\Windows Kits\8.1\include\\shared\secext.h(126): error C2086: 'BOOLEAN SEC_ENTRY': redefinition
C:\Program Files (x86)\Windows Kits\8.1\include\\shared\secext.h(117): note: see declaration of 'SEC_ENTRY'
C:\Program Files (x86)\Windows Kits\8.1\include\\shared\secext.h(126): error C2146: syntax error: missing ';' before identifier 'GetUserNameExW'
C:\Program Files (x86)\Windows Kits\8.1\include\\shared\secext.h(130): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
C:\Program Files (x86)\Windows Kits\8.1\include\\shared\secext.h(140): error C2086: 'BOOLEAN SEC_ENTRY': redefinition
C:\Program Files (x86)\Windows Kits\8.1\include\\shared\secext.h(117): note: see declaration of 'SEC_ENTRY'
C:\Program Files (x86)\Windows Kits\8.1\include\\shared\secext.h(140): error C2146: syntax error: missing ';' before identifier 'GetComputerObjectNameA'
C:\Program Files (x86)\Windows Kits\8.1\include\\shared\secext.h(144): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
C:\Program Files (x86)\Windows Kits\8.1\include\\shared\secext.h(147): error C2086: 'BOOLEAN SEC_ENTRY': redefinition
C:\Program Files (x86)\Windows Kits\8.1\include\\shared\secext.h(117): note: see declaration of 'SEC_ENTRY'
C:\Program Files (x86)\Windows Kits\8.1\include\\shared\secext.h(147): error C2146: syntax error: missing ';' before identifier 'GetComputerObjectNameW'
C:\Program Files (x86)\Windows Kits\8.1\include\\shared\secext.h(151): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
C:\Program Files (x86)\Windows Kits\8.1\include\\shared\secext.h(161): error C2086: 'BOOLEAN SEC_ENTRY': redefinition
C:\Program Files (x86)\Windows Kits\8.1\include\\shared\secext.h(117): note: see declaration of 'SEC_ENTRY'
C:\Program Files (x86)\Windows Kits\8.1\include\\shared\secext.h(161): error C2146: syntax error: missing ';' before identifier 'TranslateNameA'
C:\Program Files (x86)\Windows Kits\8.1\include\\shared\secext.h(167): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
C:\Program Files (x86)\Windows Kits\8.1\include\\shared\secext.h(170): error C2086: 'BOOLEAN SEC_ENTRY': redefinition
C:\Program Files (x86)\Windows Kits\8.1\include\\shared\secext.h(117): note: see declaration of 'SEC_ENTRY'
C:\Program Files (x86)\Windows Kits\8.1\include\\shared\secext.h(170): error C2146: syntax error: missing ';' before identifier 'TranslateNameW'
C:\Program Files (x86)\Windows Kits\8.1\include\\shared\secext.h(176): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
source_file.cpp(28): error C2065: 'sbstrTrustee': undeclared identifier
Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23506 for x64
【问题讨论】:
【参考方案1】:这段代码有几个错误:
-
您应该包含
security.h
而不是secext.h
(请参阅including secExt.h causes errors)。
您必须定义SECURITY_WIN32
、SECURITY_KERNEL
或SECURITY_MAC
之一(请参阅SSPI header file - fatal error)。
sbstrTrustee
变量未声明。
return 0
语句后没有;
。
假设您的应用程序是用户模式窗口,代码可能如下:
#define SECURITY_WIN32
#include <iostream>
#include <atlbase.h>
#include <security.h>
int main()
EXTENDED_NAME_FORMAT enf = NameFullyQualifiedDN;
LPWSTR pwszComputerName;
DWORD dwLen;
dwLen = 0;
GetComputerObjectNameW(enf, NULL, &dwLen);
pwszComputerName = new WCHAR[dwLen + 1];
if (NULL == pwszComputerName)
return 0;
if (!GetComputerObjectNameW(NameSamCompatible, pwszComputerName, &dwLen))
delete pwszComputerName;
return 0;
//sbstrTrustee = pwszComputerName;
wprintf(L"GetComputerObjectName: %s\n", pwszComputerName);
delete pwszComputerName;
return 0;
您还必须链接Secur32.lib
。
【讨论】:
嗨内维拉德,感谢您的快速响应,但我没有得到预期的结果。这个程序在 wprintf 之前返回。 您的问题是关于编译错误的。你没有提到你期望的结果。 GetComputerObjectNameW 返回什么?在 GetComputerObjectNameW 调用失败后调用 GetLastError 以获取失败的原因。以上是关于使用 GetComputerObjectNameW win API 时出现编译错误的主要内容,如果未能解决你的问题,请参考以下文章
在使用加载数据流步骤的猪中,使用(使用 PigStorage)和不使用它有啥区别?
Qt静态编译时使用OpenSSL有三种方式(不使用,动态使用,静态使用,默认是动态使用)