ConvertStringSidToSid help

M

Microsoft News

I have a list of String SIDs that I need to convert so that I can use
System.DirectoryServices to do a search for the objectSID property. I am
assuming that the "ConvertStringSidToSid" C++ function is what I need to
use. The problem is that I do not know how to use this code in C#, is there
anyone who could possibly help me out with this please. :)

Thank You,

Will Bailey
 
M

Mattias Sjögren

Will,

Start by picking up the declaration from http://www.pinvoke.net

Then call it something like this

IntPtr pSid;
if ( ConvertStringSidToSid(stringSid, out pSid) ) {
// do stuff with SID
Marshal.FreeHGlobal(pSid);
}



Mattias
 
W

Will Bailey

Ok, I've done the code you have below, and I've wen't to www.pinvoke.net I
assume that pSid is the return value and that is what I would use in my
search criteria, is this correct? Also I tried to show the return value in
a messagebox by using Convert.ToString(pSid) and it just comes back with a
value of 0, is there something that I am missing here?

I appreciate your help!

Will
 
W

Will Bailey

Ok I've figured out how to do the P/Invoke but I'm having a problem doing
the search. Below is the code, please let me know if there is something
that I am doing wrong. I'm banging my head against the wall just to do a
search on a stinking sid.


Code Below:

private void mdConvertSids()

{

foreach(string strTempStringSid in alSids)

{

string temp = strTempStringSid.Remove(0,1);

IntPtr ptrSID;

ConvertStringSidToSid(temp, out ptrSID);

byte[] btArray = new byte[28];

Marshal.Copy(ptrSID, btArray, 0, 28);

string strTempSID = "";

for (int i = 0; i < btArray.Length; i++)

{

strTempSID = strTempSID + btArray;

}

string strSID = "\\" + strTempSID;

System.String strBackSlash = "\\";

for(int intTemp = 0; intTemp < strSID.Length; intTemp++)

{

if(strSID.LastIndexOf(strBackSlash) + 3 > strSID.Length)

{

}

else

{

strSID = strSID.Insert(strSID.LastIndexOf(strBackSlash) + 3,"\\");

}

}

MessageBox.Show(strSID);

mdResolveSids(strSID);

Marshal.FreeHGlobal(ptrSID);

}

}

private void mdResolveSids(string strTempSid)

{

DirectoryEntry entry = new DirectoryEntry("LDAP://Test-DC");

DirectorySearcher mySearcher = new DirectorySearcher(entry);

mySearcher.Filter = ("(objectSID= " + strTempSid + ")");

foreach(System.DirectoryServices.SearchResult result in

mySearcher.FindAll())

{

MessageBox.Show( result.GetDirectoryEntry().Path );

}

}

[DllImport("advapi32.dll", SetLastError=true)]

static extern bool ConvertStringSidToSid(string StringSid,out IntPtr
ptrSid);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top