C 试图通过对象文件接收域SID
C++ trying to receive Domain Sid with object file
我对以下代码有问题:
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: %un", dw);
}
if (info->DomainNameDns == NULL)
{
wprintf(L"DomainNameDns is NULLn");
}
else
{
//printing the domainname
wprintf(L"DomainNameDns: %sn", 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,然后收到以下内容:
错误$@错误转换sidãd$@$ 8cc6039cb7c4
为什么我会在对象文件中获得此错误,而在Visual Studio中却没有?
我这样创建我的对象文件:
cl/ehsc/c/mt/i。testing.cpp cl/c/nologo/c/mt/i。testProgramm.c testprogramm.c .lib
我认为您在那里做不必要的事情。从宽到单宽,然后使用ATL的转换,单曲再次,我有点迷失了。
另外,请注意,诸如LookupAccountName()
之类的函数应该首先使用NULL缓冲区调用,以返回您所需的缓冲区大小。然后您分配正确的尺寸并再次致电。
这是对您的代码的简化,可以返回您的域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;
}
相关文章:
- .cpp和.h文件中的模板专用化声明
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 文本文件中的单词链表
- CMake-按正确顺序将项目与C运行时对象文件链接
- 使用新行和不使用新行读取文件
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 挂起和取消挂起一个文件DLL
- 如何确定我已使用非编码文件到达 EOF?
- 命名空间中具有.h和.cpp文件的类
- 如何使用ndk-build.cmd构建Android.so文件
- 从包含m行的文件中提取n行,必要时(惰性地)重复该文件
- 读取文件并输入到矢量中
- 在C++中查找文件
- c++库的公共头文件中应该包含什么
- 用c++从输入文件中读取另一行
- Cppcheck生成xml转储文件
- 读取文件的最后一行并输入到链接列表时出错
- 无法编译 rtmidi 测试 cmidiin.cpp 文件, 非法指令
- 如何将内容数组写入文本文件?
- C 试图通过对象文件接收域SID