我遇到以下代码的问题:
std::string getDomainSid()
{
DWORD dw;
//vector for the domain SID
std::string myDomainSID;
std::vector<std::string> domainSID;
std::wstringstream convertingDomainNameDNS;
wchar_t wc;
std::wstring getDomainName;
std::string domainNameconverted = "";
//get the domain information
dw = DsRoleGetPrimaryDomainInformation(NULL,
DsRolePrimaryDomainInfoBasic,
(PBYTE *)&info);
if (dw != ERROR_SUCCESS)
{
//wprintf(L"DsRoleGetPrimaryDomainInformation: %u\n", dw);
}
if (info->DomainNameDns == NULL)
{
wprintf(L"DomainNameDns is NULL\n");
}
else
{
//printing the domainname
wprintf(L"DomainNameDns: %s\n", info->DomainNameDns);
//converting the DomainNameDNS to a LPWSTR to use it to get the Domain SID
convertingDomainNameDNS << info->DomainNameDns;
convertingDomainNameDNS >> getDomainName;
std::wcout << getDomainName << std::endl;
}
std::wstring_convert<std::codecvt_utf8<wchar_t>> myconv;
USES_CONVERSION_EX;
domainNameconverted = myconv.to_bytes(getDomainName);
std::cout << domainNameconverted << std::endl;
LPWSTR lp = A2W_EX(domainNameconverted.c_str(), text.length());
std::wcout << lp << std::endl;
//getting the Domain SID
//LPTSTR wszAccName = lp;
LPTSTR wszAccName = reinterpret_cast<LPTSTR>(lp);
std::wcout << wszAccName << std::endl;
LPTSTR wszDomainName = (LPTSTR)GlobalAlloc(GPTR, sizeof(TCHAR) * 1024);
printf("%s", wszDomainName);
std::cout << wszDomainName << std::endl;
DWORD cchDomainName = 1024;
SID_NAME_USE eSidType;
LPTSTR sidstring;
char sid_buffer[1024];
DWORD cbSid = 1024;
SID * sid = (SID *)sid_buffer;
if (!LookupAccountName(NULL, wszAccName, sid_buffer, &cbSid, wszDomainName, &cchDomainName, &eSidType))
{
printf("Error");
}
if (!ConvertSidToStringSid(sid, &sidstring))
{
printf("%s",sidstring);
printf("Error converting Sid");
}
printf("%s", sidstring);
//myDomainSID = myconv.to_bytes(sidstring);
wchar_t* test = reinterpret_cast<wchar_t*>(sidstring);
std::wstring ts(test);
std::string domain(ts.begin(), ts.end());
printf("%s", domain);
myDomainSID = domain;
//std::string testchar = "test";
//return testchar;
return myDomainSID;
}
如果我在Visual Studio中启动程序一切正常,但现在我想创建一个目标文件并打印域SID,然后我收到以下内容:
ErrorãD$ @错误转换SidãD$ @ $ 8CC6039CB7C4
为什么我在目标文件中会出现此错误,但在Visual Studio中却没有?
我像这样创建我的目标文件:
cl / EHsc / c / MT / I. testing.cpp cl / c / nologo / c / MT / I. testprogramm.c testprogramm.c LINK / NOLOGO / OPT:NOREF / NXCOMPAT / DYNAMICBASE /out:test.exe testprogramm.obj SHA1.obj test.obj LIBCPMT.LIB LIBCMT.LIB libvcruntime.lib oldnames.lib KERNEL32.LIB USER32.LIB netapi32.lib GDI32.LIB comdlg32.lib comctl32.lib wsock32.lib shell32.lib Rpcrt4.lib oleaut32.lib Ole32.lib Wbemuuid.lib wintrust.lib crypt32.lib Ws2_32.lib iphlpapi.lib Psapi.lib advapi32.lib Shlwapi.lib dhcpcsvc.lib userenv。 lib atls.lib msvcrtd.lib vcruntimed.lib netapi32.lib Advapi32.lib IPHLPAPI.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32的.lib
答案 0 :(得分:0)
我认为你在那里做了不必要的事情。使用ATL再次从宽到单然后再转换为单一,我再次失败了。
另外,请注意,应首先使用空缓冲区调用LookupAccountName()
之类的函数,以返回所需的缓冲区大小。然后你分配正确的尺寸并再次调用它。
以下是代码的简化,它返回域SID。请注意,它不能处理错误,您必须自己做。适合我。
std::string getDomainSid()
{
std::string domainsid;
DSROLE_PRIMARY_DOMAIN_INFO_BASIC* info = nullptr;
auto dw = DsRoleGetPrimaryDomainInformation(
nullptr,
DsRolePrimaryDomainInfoBasic,
(PBYTE *)&info);
if (dw != ERROR_SUCCESS || info->DomainNameDns == nullptr)
{
// TODO
}
std::wstring_convert<std::codecvt_utf8<wchar_t>> myconv;
auto domainName = myconv.to_bytes(info->DomainNameDns);
SID_NAME_USE eSidType;
DWORD cchDomainName = 0;
DWORD cbSid = 0;
if (!LookupAccountName(nullptr, domainName.data(), nullptr, &cbSid, nullptr, &cchDomainName, &eSidType))
{
std::vector<char> sid_buffer(cbSid);
std::string refDomainName(cchDomainName, 0);
if (LookupAccountName(nullptr, domainName.data(), &sid_buffer.front(), &cbSid, &refDomainName.front(), &cchDomainName, &eSidType))
{
LPTSTR sidstring;
if (ConvertSidToStringSid(reinterpret_cast<PSID>(sid_buffer.data()), &sidstring))
{
domainsid = sidstring;
LocalFree(sidstring);
}
else
{
// TODO
}
}
else
{
// TODO
}
}
DsRoleFreeMemory(info);
return domainsid;
}