471,325 Members | 1,233 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,325 software developers and data experts.

Using ConvertSidToStringSid() in advapi32.dll

I have a IntPtr to a Sid and I'd like to convert that to a string, but I
can't figure out how to get a "pointer to a pointer to a buffer that will
contain a string". Im also afraid of memory leaks. Here is my code, any
tips are appreciated!

[DllImport("advapi32.dll", EntryPoint = "ConvertSidToStringSid",
CallingConvention = CallingConvention.Winapi,
SetLastError = true)]
public static extern bool externConvertSidToStringSid(
IntPtr lpSid,
IntPtr lpStringSid
);
static string ConvertSidToStringSid(IntPtr Sid)
{
bool fResult;

string szSid = "";
IntPtr ptrFriendlySID;

ptrFriendlySID = Marshal.AllocHGlobal(64);
fResult = externConvertSidToStringSid( Sid, ptrFriendlySID);
if (fResult == true)
{
szSid = Marshal.PtrToStringAnsi(ptrFriendlySID);
}

Marshal.FreeHGlobal(ptrFriendlySID);

return szSid;

}
Feb 2 '06 #1
3 8155
I figured it out... I needed a "ref" before the intptr... I didn't know that
combination was allowed. I'm not sure whats happening under the hood but I'd
like to know... for example am I working off the heap? or the stack?

Thanks!

"Scottie_do" wrote:
I have a IntPtr to a Sid and I'd like to convert that to a string, but I
can't figure out how to get a "pointer to a pointer to a buffer that will
contain a string". Im also afraid of memory leaks. Here is my code, any
tips are appreciated!

[DllImport("advapi32.dll", EntryPoint = "ConvertSidToStringSid",
CallingConvention = CallingConvention.Winapi,
SetLastError = true)]
public static extern bool externConvertSidToStringSid(
IntPtr lpSid,
IntPtr lpStringSid
);
static string ConvertSidToStringSid(IntPtr Sid)
{
bool fResult;

string szSid = "";
IntPtr ptrFriendlySID;

ptrFriendlySID = Marshal.AllocHGlobal(64);
fResult = externConvertSidToStringSid( Sid, ptrFriendlySID);
if (fResult == true)
{
szSid = Marshal.PtrToStringAnsi(ptrFriendlySID);
}

Marshal.FreeHGlobal(ptrFriendlySID);

return szSid;

}

Feb 2 '06 #2
I figured it out... I needed a "ref" before the intptr... I didn't know that
combination was allowed. I'm not sure whats happening under the hood but I'd
like to know... for example am I working off the heap? or the stack?


The string is heap allocated if that's what you mean. The lpStringSid
must be a ref parameter (or even better: out) because a pointer is
returned to you. You do not have to allocate a buffer yourself, so you
can remove the AllocHGlobal call.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Feb 2 '06 #3

"Scottie_do" <Sc*******@discussions.microsoft.com> wrote in message
news:C0**********************************@microsof t.com...
|I have a IntPtr to a Sid and I'd like to convert that to a string, but I
| can't figure out how to get a "pointer to a pointer to a buffer that will
| contain a string". Im also afraid of memory leaks. Here is my code, any
| tips are appreciated!
|
| [DllImport("advapi32.dll", EntryPoint = "ConvertSidToStringSid",
| CallingConvention = CallingConvention.Winapi,
| SetLastError = true)]
| public static extern bool externConvertSidToStringSid(
| IntPtr lpSid,
| IntPtr lpStringSid
| );
|
|
| static string ConvertSidToStringSid(IntPtr Sid)
| {
| bool fResult;
|
| string szSid = "";
| IntPtr ptrFriendlySID;
|
| ptrFriendlySID = Marshal.AllocHGlobal(64);
|
|
| fResult = externConvertSidToStringSid( Sid, ptrFriendlySID);
| if (fResult == true)
| {
| szSid = Marshal.PtrToStringAnsi(ptrFriendlySID);
| }
|
| Marshal.FreeHGlobal(ptrFriendlySID);
|
| return szSid;
|
| }

Note that this is no longer needed when running v2 of the framework, the
SecurityIdentifier class in System.Security.Principal has everything you
need to translate binary SID's to string SID's and much more.

Willy.
Feb 2 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Mary | last post: by
4 posts views Thread by Mohammed Abdel-Razzak | last post: by
3 posts views Thread by Parveen | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.