473,387 Members | 1,673 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,387 software developers and data experts.

Simple LookupAccountName (working)

Working code below (paying back to the groups :)

/Tomas
Class SidFunctions

Enum SID_NAME_USE
SidTypeUser = 1
SidTypeGroup
SidTypeDomain
SidTypeAlias
SidTypeWellKnownGroup
SidTypeDeletedAccount
SidTypeInvalid
SidTypeUnknown
SidTypeComputer
End Enum 'SID_NAME_USE

Declare Function LookupAccountName Lib "advapi32.dll" Alias
"LookupAccountNameA" (ByVal lpSystemName As String, ByVal
lpAccountName As String,
<System.Runtime.InteropServices.MarshalAs(System.R untime.InteropServices.UnmanagedType.LPArray)>
ByVal Sid() As Byte, ByRef cbSid As System.UInt32, ByVal
ReferencedDomainName As System.Text.StringBuilder, ByRef
cchReferencedDomainName As System.UInt32, ByRef peUse As SID_NAME_USE)
As Boolean

Public Function ConvertUsernameToSidString(ByVal i_AccountName As
String, Optional ByVal i_Domain As String = "") As String

'************************************************* ********************
'* Source of example:
http://pinvoke.net/default.aspx/adva...kupAccountName
'************************************************* ********************

Dim resultAsByte As Byte() = Nothing
Dim result As String
Dim m_resultAsByteCapacity As System.UInt32 =
UInt32.Parse("0")
Dim m_StringBuilder As New System.Text.StringBuilder
Dim m_StringBuilderCapacity As System.UInt32 =
UInt32.Parse(m_StringBuilder.Capacity)
Dim m_SID_NAME_USE As SID_NAME_USE

'************************************************* ********************
'* Lookup
'************************************************* ********************

Call LookupAccountName(i_Domain, i_AccountName, resultAsByte,
m_resultAsByteCapacity, m_StringBuilder, m_StringBuilderCapacity,
m_SID_NAME_USE)

'************************************************* ********************
'* We should get ERROR_INSUFFICIENT_BUFFER
'************************************************* ********************

If System.Runtime.InteropServices.Marshal.GetLastWin3 2Error =
122 Then 'ERROR_INSUFFICIENT_BUFFER

'************************************************* ********************
'* Allocate the exact amount of needed space
'************************************************* ********************

resultAsByte = New
Byte(CLng(m_resultAsByteCapacity.ToString)) {}
m_StringBuilder.EnsureCapacity(CLng(m_StringBuilde rCapacity.ToString))

'************************************************* ********************
'* And fetch the information
'************************************************* ********************

If LookupAccountName(i_Domain, i_AccountName,
resultAsByte, m_resultAsByteCapacity, m_StringBuilder,
m_StringBuilderCapacity, m_SID_NAME_USE) = True Then

'************************************************* ********************
'* Store result (all OK)
'************************************************* ********************

result = BitConverter.ToString(resultAsByte)
'Call ConvertSidFromByteToString(resultAsByte, result)
Debug.WriteLine("Type=" & m_SID_NAME_USE.ToString &
vbTab & "SID=" & result)

End If

End If

Return result

End Function

End Class
Nov 21 '05 #1
6 4281
You're a VB 6 coder I see & not a VB.NET one.

You can look up account name in a few lines of code using WMI, which means
there is no need to use the API in this instance.

Also, I see you are using the 'Call' Keyword. Call was only used in VB 6
when you wanted to enclose everthing in brackets '()'. Without that keyword,
you just miss out the opening & end brackets.

You could also shorten your code by using 2 import statements that I have
noticed:

Imports System.Runtime.InteropServices ' For MarshalAs
Imports System.Text ' For StringBuilder

Lastly, why are you posting a solution here when no one has asked for it?
Rather pointless to me I think

Nice try though - 3/10
Nov 21 '05 #2
Crouchie,

I disagree and agree with you.

In past I have given messages as you do now, because then it was often more
that you were looking to Win32 coding than VBNet coding in this newsgroup.
(And really very much more than Thomas does now).

However the used code is basicly VBNet where it seems (I did not any check
about that however you tell) if not is tried to use Net code however the
Win32 API's, while there are such nice pages on MSDN for that.

http://msdn.microsoft.com/library/de...l/win32map.asp

(I changed such a Win32 thing yesterday completly what was about the
shutdowntime, it is in this newsgroup somewhere)

However it is not VB6 code, using Win32 API's can in almost every windows
programming language.

Cor
Nov 21 '05 #3
> You're a VB 6 coder I see & not a VB.NET one.

Please, don't try to confuse people with bad replys. The code is
indeed .NET, using some techniques from VB6.
You can look up account name in a few lines of code using WMI, which means
there is no need to use the API in this instance.
No, WMI doesn't work in all cases. For example, I belive fetching a
local account on a remote machine doesn't work. Also, there is quite
some examples using WMI, and few using the API.
Also, I see you are using the 'Call' Keyword. Call was only used in VB 6
when you wanted to enclose everthing in brackets '()'. Without that keyword,
you just miss out the opening & end brackets.
Different programmer styles. I like to use call, so let me. I belive
it doesn't change the generated MSIL.
You could also shorten your code by using 2 import statements that I have
noticed:
Imports System.Runtime.InteropServices ' For MarshalAs
Imports System.Text ' For StringBuilder
This is basically the main reason why most examples on the Internet
fails to compile. Coding without imports clearly shows where the call
is originating (which leads to better developers), and it ensures
working code even if you forget to post the imports (which is very
important).
Lastly, why are you posting a solution here when no one has asked for it?
As I wrote, "Paying back to the groups". Researching for the solution
took me a while, so I thought I should save some time for someone
else.
Rather pointless to me I think


Maybe, but I belive *your* reply was indeed pointless. It didn't give
anything to the group, except information of how bad developer you
think I am (which the world probably cares less about), and the
information about the code being VB6 (which was faulty information).
Now, in the future, please stop wasting my and other peoples time by
replying with nonsense information.

/Tomas - Who hesitated a lot to reply at all.. (Don't feed the trolls)
Nov 21 '05 #4
GL
Well, I thank you Tomas.
Nice to see people taking the time to help others off their own back.

Adrian

"Tomas Nilsson" <no******@hotmail.com> wrote in message
news:17**************************@posting.google.c om...
You're a VB 6 coder I see & not a VB.NET one.
Please, don't try to confuse people with bad replys. The code is
indeed .NET, using some techniques from VB6.
You can look up account name in a few lines of code using WMI, which means there is no need to use the API in this instance.


No, WMI doesn't work in all cases. For example, I belive fetching a
local account on a remote machine doesn't work. Also, there is quite
some examples using WMI, and few using the API.
Also, I see you are using the 'Call' Keyword. Call was only used in VB 6
when you wanted to enclose everthing in brackets '()'. Without that keyword, you just miss out the opening & end brackets.


Different programmer styles. I like to use call, so let me. I belive
it doesn't change the generated MSIL.
You could also shorten your code by using 2 import statements that I have noticed:
Imports System.Runtime.InteropServices ' For MarshalAs
Imports System.Text ' For StringBuilder


This is basically the main reason why most examples on the Internet
fails to compile. Coding without imports clearly shows where the call
is originating (which leads to better developers), and it ensures
working code even if you forget to post the imports (which is very
important).
Lastly, why are you posting a solution here when no one has asked for

it?
As I wrote, "Paying back to the groups". Researching for the solution
took me a while, so I thought I should save some time for someone
else.
Rather pointless to me I think


Maybe, but I belive *your* reply was indeed pointless. It didn't give
anything to the group, except information of how bad developer you
think I am (which the world probably cares less about), and the
information about the code being VB6 (which was faulty information).
Now, in the future, please stop wasting my and other peoples time by
replying with nonsense information.

/Tomas - Who hesitated a lot to reply at all.. (Don't feed the trolls)

Nov 21 '05 #5
Thomas,

Please, don't try to confuse people with bad replys. The code is
indeed .NET, using some techniques from VB6.

In my opinion are as well confusing people.

Your code is not .Net it is VB.Net where partially is used Net, however not
everywhere.

With what I don't say it is wrong when you have no .Net alternative.

Just my thought,

Cor
Nov 21 '05 #6
Thomas,

However nobody thanked you for sharing this with us, so with this message.
Thanks,

Cor
Nov 21 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Bruce W...1 | last post by:
In my effort to learn PHP I'm playing with some simple email scripts. They worked a few days ago but they stopped working. The only thing I've done to this Windows 2000 PC in this time was a...
3
by: Akseli Mäki | last post by:
Hi, I'm working over my site to include simple CSS. I've now noticed, that the pages look totally different on Opera than compared to Mozilla and IE....
17
by: savesdeday | last post by:
In my beginnning computer science class we were asked to translate a simple interest problem. We are expected to write an algorithm that gets values for the starting account balance B, annual...
1
by: Tim T. | last post by:
I've been scouring the internet for a simple example of the LookupAccountName function to retrieve a user's SID. All of the ones that I have found are complex and confusing. All I need is the...
4
by: Tushar | last post by:
Hi, I am trying to get the user account information using the function LookupAccountName supplying it the user name. This API return the user SID if the user exists. Now, each SID has a user...
2
by: dondraper | last post by:
I have an application that uses a popular but simple set of JavaScript routines that implement an AJAX call used to populate a drop-down. It works for thousands of user but I have one customer...
24
by: firstcustomer | last post by:
Hi, Firstly, I know NOTHING about Javascript I'm afraid, so I'm hoping that someone will be able to point me to a ready-made solution to my problem! A friend of mine (honest!) is wanting to...
10
true911m
by: true911m | last post by:
This is a simple walkthrough to get PyInstaller up and running. I decided to give PI a try, because it claims to be more selective about what it bundles into its executable files by default, and...
2
by: kigoobe | last post by:
I am having a very weird issue here, a simple form submit not working. Trying for several hours now ... must be something stupid ... help appreciated. First, the html - <form id="formFinale"...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.