|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Simple LookupAccountName (working)/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.Runtime.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/advapi32.LookupAccountName '********************************************************************* 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.GetLastWin32Error = 122 Then 'ERROR_INSUFFICIENT_BUFFER '********************************************************************* '* Allocate the exact amount of needed space '********************************************************************* resultAsByte = New Byte(CLng(m_resultAsByteCapacity.ToString)) {} m_StringBuilder.EnsureCapacity(CLng(m_StringBuilderCapacity.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 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 > 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 isindeed .NET, using some techniques from VB6. > You can look up account name in a few lines of code using WMI, which means No, WMI doesn't work in all cases. For example, I belive fetching a> there is no need to use the API in this instance. 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 Different programmer styles. I like to use call, so let me. I belive> when you wanted to enclose everthing in brackets '()'. Without that keyword, > you just miss out the opening & end brackets. it doesn't change the generated MSIL. > You could also shorten your code by using 2 import statements that I have This is basically the main reason why most examples on the Internet> noticed: > Imports System.Runtime.InteropServices ' For MarshalAs > Imports System.Text ' For StringBuilder 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 solutiontook 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 giveanything 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) Well, I thank you Tomas.
Nice to see people taking the time to help others off their own back. Adrian Show quoteHide quote "Tomas Nilsson" <nono9***@hotmail.com> wrote in message news:17421296.0504010130.7def211c@posting.google.com... > > 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) Thomas,
> In my opinion are as well confusing people.> Please, don't try to confuse people with bad replys. The code is > indeed .NET, using some techniques from VB6. > 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 Thomas,
However nobody thanked you for sharing this with us, so with this message. Thanks, Cor
Hashtable question
guidelines for developing an application Form question (Re-usuable code) copying data from MS-SQL to MS-Access using VB.Net How do you protect from pirating? Icon for Components Object variable or With block variable not set. Anyone know what could cause the following error messages? SqlCommand w/ Params Select Issue Using the IN Clause Print information when ANY control is used |
|||||||||||||||||||||||