Windows 套接字和身份验证构建在包含 Poco-Library 时失败

Windows Socket and Authentication build failed when including Poco-Library

本文关键字:包含 Poco-Library 失败 构建 套接字 身份验证 Windows      更新时间:2023-10-16

我编写了一个C++程序来验证Windows用户,该程序在未包含Poco库(事件)时无缝工作。我有一个无限的while循环(while(true)),当没有来自serer应用程序的请求时需要停止。套接字读取在单独的线程中独立运行。

编译器 : 明W 7.2

C++标准 : C++14

包管理器 : Msys2

架构 : x64

我收到一个错误:

g++    -c -g -D__DEBUG -I/C/msys64/mingw64/include/boost -I/C/msys64/mingw64/include `pkg-config --cflags libconfig++` `pkg-config --cflags gnutls` -std=c++14  -MMD -MP -MF "build/Debug/MinGW-Windows/Authenticate.o.d" -o build/Debug/MinGW-Windows/Authenticate.o Authenticate.cpp
In file included from C:/msys64/mingw64/include/Poco/Foundation.h:102:0,
from C:/msys64/mingw64/include/Poco/Event.h:23,
from Common.hpp:41,
from Authenticate.hpp:19,
from Authenticate.cpp:14:
C:/msys64/mingw64/include/Poco/Platform_WIN32.h:179:92: note: #pragma message: Compiling POCO on Windows without #define POCO_WIN32_UTF8 is deprecated.
#pragma message("Compiling POCO on Windows without #define POCO_WIN32_UTF8 is deprecated.")
                      ^
Authenticate.cpp: In member function 'bool Authenticate::authenticateUserCommandLine(std::__cxx11::string, std::__cxx11::string, std::__cxx11::string, std::__cxx11::string&)':
Authenticate.cpp:30:26: error: 'LogonUser' was not declared in this scope
logonReturnVal = LogonUser(userName.c_str(), domain.c_str(), NULL, LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, &token);
^~~~~~~~~
Authenticate.cpp:30:26: note: suggested alternative: 'LogonUserW'
logonReturnVal = LogonUser(userName.c_str(), domain.c_str(), NULL, LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, &token);
^~~~~~~~~
LogonUserW
Authenticate.cpp:32:26: error: 'LogonUser' was not declared in this scope
logonReturnVal = LogonUser(userName.c_str(), domain.c_str(), password.c_str(), LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, &token);
^~~~~~~~~
Authenticate.cpp:32:26: note: suggested alternative: 'LogonUserW'
logonReturnVal = LogonUser(userName.c_str(), domain.c_str(), password.c_str(), LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, &token);
^~~~~~~~~
LogonUserW
Authenticate.cpp: In member function 'bool Authenticate::authenticateUserCommandLine(std::__cxx11::string, std::__cxx11::string, std::__cxx11::string&)':
Authenticate.cpp:54:26: error: 'LogonUser' was not declared in this scope
logonReturnVal = LogonUser(userName.c_str(), domain.c_str(), NULL, LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, &token);
^~~~~~~~~
Authenticate.cpp:54:26: note: suggested alternative: 'LogonUserW'
logonReturnVal = LogonUser(userName.c_str(), domain.c_str(), NULL, LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, &token);
^~~~~~~~~
LogonUserW
Authenticate.cpp:56:26: error: 'LogonUser' was not declared in this scope
logonReturnVal = LogonUser(userName.c_str(), domain.c_str(), password.c_str(), LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, &token);
^~~~~~~~~
Authenticate.cpp:56:26: note: suggested alternative: 'LogonUserW'
logonReturnVal = LogonUser(userName.c_str(), domain.c_str(), password.c_str(), LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, &token);
^~~~~~~~~
LogonUserW

如果我删除#include <Poco/Event.h>程序可以正常工作,没有错误。

如果我添加#define POCO_WIN32_UTF8,我必须用LogonUserW替换LogonUser。我在添加#define POCO_WIN32_UTF8时遇到的最大问题是我在::GetLastError()说找不到函数时收到错误。

登录用户使用情况 :

if(password.length() == 0)
logonReturnVal = LogonUser(userName.c_str(), domain.c_str(), NULL, LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, &token);
else
logonReturnVal = LogonUser(userName.c_str(), domain.c_str(), password.c_str(), LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, &token);

::GetLastError() 用法 :

string Error::GetLastErrorAsString(void)
{
//Get the error message, if any.
DWORD errorMessageID = ::GetLastError();
if(errorMessageID == 0)
return string(); //No error message has been recorded
LPSTR messageBuffer = nullptr;
size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0, NULL);
string message(messageBuffer, size);
//Free the buffer.
LocalFree(messageBuffer);
return message;
}

问题是我在windows.h之前包含了Poco/Event.hPoco/Event.h需要先定义#define POCO_WIN32_UTF8,然后再包含导致问题的标头。

通过在定义之前包含windows.h解决了这个问题#define POCO_WIN32_UTF8而而Poco/Event.h又在包含之前定义。