Re: logonuser api returns 127

From: Tom Shelton (tom_at_mtogden.com)
Date: 03/12/04


Date: Thu, 11 Mar 2004 22:05:00 -0800

On 2004-03-11, Brent Burkart <Brent.Burkart@wvmb.com> wrote:
> I have an impersonation procedure which was working before, but not any
> more. It is returning a 127. Any help is appreciated.
>
> Here is the code.
>
> 'First the call to the routine
> If Impersonate.impersonateValidUser("xxx", "xxx", "xxx") Then
>
> undoImpersonation()
>
> Else
>
> End If
>
> 'The routine
>
> Declare Auto Function LogonUser Lib "advapi32.dll" (ByVal lpszUsername As
> String, ByVal lpszDomain As String, ByVal lpszPassword As String, ByVal
> dwLogonType As Integer, ByVal dwLogonProvider As Integer, ByRef phToken As
> IntPtr) As Integer
>
> Declare Auto Function ImpersonateLoggedOnUser Lib "advapi32.dll" (ByVal
> hToken As IntPtr) As Integer
>
> Declare Auto Function RevertToSelf Lib "advapi32.dll" () As Integer
>
> Declare Auto Function GetLastError Lib "Kernel32.dll" () As Integer
>
> Function impersonateValidUser(ByVal username As String, ByVal Domain As
> String, ByVal password As String) As Boolean
>
> Dim LogonType As Integer
>
> Dim LogonProvider As Integer
>
> Dim Tk As IntPtr
>
> LogonType = 2 'Interactive
>
> LogonProvider = 0 'Default Provider
>
> If LogonUser(username, Domain, password, LogonType, LogonProvider, Tk) <> 0
> Then
>
> If ImpersonateLoggedOnUser(Tk) <> 0 Then
>
> impersonateValidUser = True
>
> Else
>
> impersonateValidUser = False
>
> End If
>
> Else
>
> Dim ret As Integer = GetLastError()
>
> MsgBox(ret)
>
> End If
>
> End Function
>
>

Brent,

First of all, are you absolutely sure that GetLastError is returning
127? Use of GetLastError is not good practice - and that applies to VB6
as well. The problem is that the runtime may make API calls, before you
call GetLastError - so the return value is useless. You should use
Marshal.GetLastWin32Error() instead...

Anyway, I haven't bothered to look up the error code... But, you can
get the system message associated with it by doing something like:

Dim errorCode As Integer = Marshal.GetLastWin32Error()
Dim ex As New Win32Exception(errorCode);
MessageBox.Show( _
        String.Formate("Error: {0} - {1}", errorCode, ex.Message)

My guess is - especially looking down the thread - that you're running
into some kind of security related problem.

-- 
Tom Shelton [MVP]
Powered By Gentoo Linux 1.4
I have often regretted my speech, never my silence.
		-- Publilius Syrus


Relevant Pages

  • logonuser api returns 127
    ... Declare Auto Function LogonUser Lib "advapi32.dll" (ByVal lpszUsername As ... String, ByVal lpszDomain As String, ByVal lpszPassword As String, ByVal ... Dim LogonProvider As Integer ... impersonateValidUser = False ...
    (microsoft.public.dotnet.languages.vb)
  • Impersonation half way working
    ... Dim LOGON32_LOGON_INTERACTIVE As Integer = 2 ... ByVal lpszDomain As String, _ ... ExistingTokenHandle As IntPtr, ByVal ImpersonationLevel As Integer, ByRef ... impersonateValidUser = False ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Impersonation half way working
    ... > Dim impersonationContext As WindowsImpersonationContext ... > ByVal lpszDomain As String, ... > ExistingTokenHandle As IntPtr, ByVal ImpersonationLevel As Integer, ByRef ... > impersonateValidUser = False ...
    (microsoft.public.dotnet.languages.vb)
  • Search pattern
    ... Dim strfile As String ... Dim bAddressFound As Boolean ... Dim strCurrentChar As String ...
    (comp.databases.ms-access)
  • Re: multiplatform (pocketPC & desktopPC) (Daniel !!)
    ... Friend Versione As String ... Public Sub GetMyConnectionPalmare() ... Dim errorMessages As String ... Private Function GetDS_Desktop(ByVal SQL As String) As DataSet ...
    (microsoft.public.dotnet.framework.compactframework)