C 试图通过对象文件接收域SID

C++ trying to receive Domain Sid with object file

本文关键字:SID 文件 对象      更新时间:2023-10-16

我对以下代码有问题:

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;
}