Dunno about c# either, but in C++, this would have been
AllocateAndInitializeSid(S
Main Topics
Browse All TopicsHello Experts,
my XP service impersonates a user at startup. Because this user must have the SE_TCB_NAME privilege, I try to call LsaAddAccountRights at the first start after installation.
But the service throws a NullReferenceException exception at AllocateAndInitializeSid, GetLastError returns ERROR_MOD_NOT_FOUND.
This is my declaration:
[System.Runtime.InteropSer
private static extern bool AllocateAndInitializeSid(
SID_IDENTIFIER_AUTHORITY pIdentifierAuthority,
int nSubAuthorityCount,
long dwSubAuthority0, long dwSubAuthority1,
long dwSubAuthority2, long dwSubAuthority3,
long dwSubAuthority4, long dwSubAuthority5,
long dwSubAuthority6, long dwSubAuthority7,
IntPtr pSid);
[StructLayoutAttribute(Lay
class SID_IDENTIFIER_AUTHORITY {
[MarshalAs(UnmanagedType.B
public byte[] Value;
}
And there the Exception occurs:
SID_IDENTIFIER_AUTHORITY auth = new SID_IDENTIFIER_AUTHORITY()
auth.Value = new byte[6]{0,0,0,0,0,5};
try{
AllocateAndInitializeSid(n
}
catch{ eventLog.WriteEntry(GetLas
I also have tried ConvertStringSidToSid, but it fails with ERROR_INVALID_HANDLE:
String strSid = "S-1–5-11";
if( ! ConvertStringSidToSid(strS
Console.WriteLine(GetLastE
}
Does anyone know this problem? I can't find anything about it on the web.
Thanks,
coco
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Thanks _nn_, now I'm a few steps further, at the next problem :)
After initializing the SID variable, I look up the SID using LookupAccountName.
IsValidSid() returns True, so I open a policy with LsaOpenPolicy. LsaOpenPolicy return 0, so everything's fine so far.
But LsaAddAccountRights fails with ERROR_INVALID_HANDLE. Which handle does it mean?
LsaAddAccountRights:
[DllImport("advapi32.dll",
private static extern long LsaAddAccountRights(
ref long PolicyHandle,
ref long AccountSid,
ref LSA_UNICODE_STRING[] UserRights,
int CountOfRights);
Somewhere in the code:
LookupAccountName(null, "bg.vm\\VITA_MBA", ref sid, ref sidSize, domainName, ref nameSize, out accountType);
eventLog.WriteEntry("IsVal
int access = (int)(POLICY_LOOKUP_NAMES | POLICY_CREATE_PRIVILEGE | POLICY_CREATE_ACCOUNT);
LSA_UNICODE_STRING systemName = new LSA_UNICODE_STRING();
uint result = LsaOpenPolicy(ref systemName, ref ObjectAttributes, access, out policyHandle);
eventLog.WriteEntry("OpenP
LSA_UNICODE_STRING[] userRights = new LSA_UNICODE_STRING[1];
userRights[0] = new LSA_UNICODE_STRING();
userRights[0].Buffer = "SeTcbPrivilege";
userRights[0].Length = userRights[0].Buffer.Lengt
userRights[0].MaximumLengt
long res = LsaAddAccountRights(ref policyHandle, ref sid, ref userRights, 1);
long winErrorCode = LsaNtStatusToWinError(res)
eventLog.WriteEntry("Accou
thanks in advance,
coco
Business Accounts
Answer for Membership
by: _nn_Posted on 2003-07-14 at 06:51:18ID: 8917057
I don't know much about C#, but you could take a look at /csharp/Wi n32.asp
http://www.codeproject.com
for more details. It looks like imports and structures definitions differ a bit from yours. Main difference being that the SID_IDENTIFIER_AUTHORITY parameter is explicitly passed by reference.