LookupAccountName 有问题,wrote a question并发现我必须使用 LookupAccountName 两次才能工作。现在我尝试将 NetLocalGroupAddMembers 与我从 LookupAccountName 获得的 SID 一起使用,它返回成功,但新创建的用户不会添加到用户组。
我从 CreateWellKnownSid 获取用户组,使用 LookupAccountSid 将其转换为字符串,并通过转换带有 LookupAccountName.
编辑:
这是一组可测试的代码:
#include <Windows.h>
#include <tchar.h>
#include <wchar.h>
#include <LM.h>
#include <sddl.h>
#pragma comment(lib, "Netapi32.lib")
#define MAX_NAME 256
VOID ShowError(DWORD errorCode)
{
//FormatMessageW
DWORD flags = FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS;
LPWSTR errorMessage;
DWORD size = 0;
if (!FormatMessageW(flags, NULL, errorCode, 0, (LPWSTR)&errorMessage, size, NULL))
{
fwprintf(stderr, L"Could not get the format message, error code: %u\n", GetLastError());
exit(1);
}
wprintf(L"\n%s\n", errorMessage);
LocalFree(errorMessage);
}
int wmain(int argc, WCHAR **argv)
{
//NetUserAdd function
NET_API_STATUS addUser;
DWORD infoLevel = 1; //USER_INFO_1
USER_INFO_1 userData;
DWORD paramError = 0;
//LocalAlloc
UINT memAttributes = LMEM_FIXED;
DWORD sidSize = SECURITY_MAX_SID_SIZE;
//CreateWellKnownSid
WELL_KNOWN_SID_TYPE sidType = WinBuiltinUsersSid;
PSID groupSID;
//LookupAccountSid
WCHAR name[MAX_NAME];
DWORD nameSize = MAX_NAME;
WCHAR domainName[MAX_NAME];
DWORD domainNameSize = MAX_NAME;
SID_NAME_USE accountType;
//LookupAccountName
LPCWSTR machine = NULL;
BYTE accountSIDBuf[SECURITY_MAX_SID_SIZE];
PSID accountSID = (PSID)accountSIDBuf;
DWORD cbSid = 0;
SID_NAME_USE typeOfAccount;
/*WCHAR refDomain[MAX_NAME];*/
DWORD cchRefDomain = 0;
//NetLocalGroupAddMembers
NET_API_STATUS localGroupAdd;
DWORD levelOfData = 0; //LOCALGROUP_MEMBERS_INFO_0
LOCALGROUP_MEMBERS_INFO_0 localMembers;
DWORD totalEntries = 0;
if (argc != 2)
{
fwprintf(stderr, L"\nUsage: %s [UserName]\n", *argv);
return 1;
}
//Set up USER_INFO_1 structure
userData.usri1_name = argv[1];
userData.usri1_password = NULL;
userData.usri1_priv = USER_PRIV_USER;
userData.usri1_home_dir = NULL;
userData.usri1_comment = NULL;
userData.usri1_flags = UF_SCRIPT;
userData.usri1_script_path = NULL;
addUser = NetUserAdd(NULL, infoLevel, (LPBYTE)&userData, ¶mError);
if (addUser != NERR_Success)
{
fwprintf(stderr, L"\nA system error has ocurred: %d\n", addUser);
return 1;
}
else
{
//Let's allocate memory for the SID
if (!(groupSID = LocalAlloc(memAttributes, sidSize))) //if fails
{
ShowError(GetLastError());
exit(1);
}
//Let's create a SID for Users group
if (!CreateWellKnownSid(sidType, NULL, groupSID, &sidSize))
{
ShowError(GetLastError());
exit(1);
}
else
{
if (!LookupAccountSidW(NULL, groupSID, name, &nameSize,
domainName, &domainNameSize, &accountType))
{
ShowError(GetLastError());
return 1;
}
if (!LookupAccountNameW(NULL, argv[1], NULL, &cbSid, NULL, &cchRefDomain, &typeOfAccount))
{
ShowError(GetLastError());
/*exit(1);*/
}
PSID theSID;
LPWSTR refDomainName = (LPWSTR)malloc(cchRefDomain * sizeof(WCHAR));
if (!(theSID = LocalAlloc(memAttributes, cbSid)))
{
ShowError(GetLastError());
exit(1);
}
if (refDomainName == NULL)
{
fwprintf(stderr, L"Error allocating memory for RefDomainName \n");
exit(1);
}
//Here we go again!
if (!LookupAccountNameW(NULL, argv[1], theSID, &cbSid,
refDomainName, &cchRefDomain, &typeOfAccount))
{
ShowError(GetLastError());
exit(1);
}
//Here I should be able to use NetLocalGroupAddMembers
//to add the user passed as argument to the Users group.
localMembers.lgrmi0_sid = theSID;
localGroupAdd = NetLocalGroupAddMembers(NULL, name, levelOfData, (LPBYTE)&localMembers, totalEntries);
if (localGroupAdd != NERR_Success)
{
ShowError(localGroupAdd);
return 1;
}
else
{
ShowError(localGroupAdd);
}
LocalFree(theSID);
free(refDomainName);
}
LocalFree(groupSID);
}
return 0;
}
这是我得到的结果,符合预期:
The data area passed to a system call is too small.
The operation completed successfully.
但是当我用 Net User 查询新创建的用户时,它说:
Local Group Memberships
Global Group Membership *None
它应该说本地组成员中的用户。
有什么帮助吗?
谢谢!
最佳答案
您正在使用 totalentries=0 调用 NetLocalGroupAddMembers,但您应该使用 1 调用它。
旁注:
有什么好的理由将所有变量放在顶部而不在需要的地方声明它们吗?位置可能已帮助您找到错误。
usri1_password 未记录为允许 NULL。
您确定要将 NULL 作为机器/系统名称传递到任何地方吗?有时这与“.”不同,参见示例 LookupAccountSidW。
关于c - NetLocalGroupAddMembers 返回成功,但未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48219577/
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返
我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion在首页我有:汽车:VolvoSaabMercedesAudistatic_pages_spec.rb中的测试代码:it"shouldhavetherightselect"dovisithome_pathit{shouldhave_select('cars',:options=>['volvo','saab','mercedes','audi'])}end响应是rspec./spec/request
在Rails4.0.2中,我使用s3_direct_upload和aws-sdkgems直接为s3存储桶上传文件。在开发环境中它工作正常,但在生产环境中它会抛出如下错误,ActionView::Template::Error(noimplicitconversionofnilintoString)在View中,create_cv_url,:id=>"s3_uploader",:key=>"cv_uploads/{unique_id}/${filename}",:key_starts_with=>"cv_uploads/",:callback_param=>"cv[direct_uplo
我有一个包含多个键的散列和一个字符串,该字符串不包含散列中的任何键或包含一个键。h={"k1"=>"v1","k2"=>"v2","k3"=>"v3"}s="thisisanexamplestringthatmightoccurwithakeysomewhereinthestringk1(withspecialcharacterslike(^&*$#@!^&&*))"检查s是否包含h中的任何键的最佳方法是什么,如果包含,则返回它包含的键的值?例如,对于上面的h和s的例子,输出应该是v1。编辑:只有字符串是用户定义的。哈希将始终相同。 最佳答案
这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下
所以我开始关注ruby,很多东西看起来不错,但我对隐式return语句很反感。我理解默认情况下让所有内容返回self或nil但不是语句的最后一个值。对我来说,它看起来非常脆弱(尤其是)如果你正在使用一个不打算返回某些东西的方法(尤其是一个改变状态/破坏性方法的函数!),其他人可能最终依赖于一个返回对方法的目的并不重要,并且有很大的改变机会。隐式返回有什么意义?有没有办法让事情变得更简单?总是有返回以防止隐含返回被认为是好的做法吗?我是不是太担心这个了?附言当人们想要从方法中返回特定的东西时,他们是否经常使用隐式返回,这不是让你组中的其他人更容易破坏彼此的代码吗?当然,记录一切并给出
使用Ruby1.9.2运行IDE提示说需要gemruby-debug-base19x并提供安装它。但是,在尝试安装它时会显示消息Failedtoinstallgems.Followinggemswerenotinstalled:C:/ProgramFiles(x86)/JetBrains/RubyMine3.2.4/rb/gems/ruby-debug-base19x-0.11.30.pre2.gem:Errorinstallingruby-debug-base19x-0.11.30.pre2.gem:The'linecache19'nativegemrequiresinstall