473,385 Members | 1,922 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 8313
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on...
2
by: Mary | last post by:
Hello, I am having a problem with the cl compiler. I have written a C class (RegConnect.c) which uses Win32 API functions such as RegOpenKey, RegCloseKey etc. Initially when I was trying to...
7
by: Vincent Nguyen | last post by:
Hi, Does anyone know how call Win32 native API GetTokenInformation() by using C#? Any sample code would be helpful. Thanks! Vincent
4
by: Mohammed Abdel-Razzak | last post by:
Dear sirs I want to know how can I shutdown or restart my computer using C# Also I want to know how can I open any windows program using C# (EX: opening the windows calculator from my...
3
by: Parveen | last post by:
I'm trying to use the GetUserName API function in the advapi32.dll library. I declare my function as follows Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer...
2
by: John Regan | last post by:
Hello All I am trying to find the owner of a file or folder on our network (Windows 2000 Server) using VB.Net and/or API. so I can search for Folders that don't follow our company's specified...
10
by: Martin Robins | last post by:
I need to access the scheduler service on a network computer in order to manipulate it remotely from .NET; I have all of the necessary code to perform the manipulation and it works - great - but I am...
4
by: AndyL | last post by:
Hi, i want to enumerate the name of the user from a SID string. So I use the LConvertStringookupAccountSID function. It looks good up to the converting the name pointer to a string. I receive a...
2
by: Podi | last post by:
I have ctypes version 0.9.6 and Python 2.4.2 running on Windows XP Professional. When I tried to use some functions in the Advapi32.dll, some functions are available and some are not. Is this a...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.