logonuser api returns 127

B

Brent Burkart

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
 
J

Jeremy

I have an impersonation procedure which was working before [snip]

What changed when it stopped working?
 
B

Brent Burkart

The code thats working is running as a service. The code that is not
working is a windows app. It is the exact same code line for line.

Thanks
Jeremy said:
I have an impersonation procedure which was working before [snip]

What changed when it stopped working?
 
T

Tom Shelton

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.
 
B

Brent Burkart

I used GetLastWin32Error and recieved a "Error: 1400 - Invalid window
handle"

Does that mean anything to anyone? I am googling without any luck.

Thanks,
Brent
 
B

Brent Burkart

Thanks Lisa,

I tried that but no success. I am getting a new error though using
Marshal.GetLastWin32Error. "Invalid Window Handle" I am googling to see
what I come up with.

Thanks,
Brent
Lisa said:
Brent,

I got that errors before. Try to use the full path. For example:
C:\WINNT\system32\ADVAPI32.DLL instead of advapi32.dll.
However, I keeps getting 1314 errors on the Windows 2000 machine. I tried
to grant "Act of part of OS", but did not work.
 
G

Guest

Brent
What's the error that you got from GetLastError? (It should be same with Marshal.GetLastWin32Error().
It looks like that you still can not reach the Longuser function yet... Is the path right for both advapi32.dll and Kernel32.dll

Lis

----- Brent Burkart wrote: ----

I used GetLastWin32Error and recieved a "Error: 1400 - Invalid windo
handle

Does that mean anything to anyone? I am googling without any luck

Thanks
Bren
Tom Shelton said:
On 2004-03-11 said:
I have an impersonation procedure which was working before, but not an
more. It is returning a 127. Any help is appreciated
If Impersonate.impersonateValidUser("xxx", "xxx", "xxx") The A
String, ByVal lpszDomain As String, ByVal lpszPassword As String, ByVa
dwLogonType As Integer, ByVal dwLogonProvider As Integer, ByRef phToke A
IntPtr) As Intege
String, ByVal password As String) As Boolea
First of all, are you absolutely sure that GetLastError is returnin
127? Use of GetLastError is not good practice - and that applies to VB
as well. The problem is that the runtime may make API calls, before yo
call GetLastError - so the return value is useless. You should us
Marshal.GetLastWin32Error() instead..
Anyway, I haven't bothered to look up the error code... But, you ca
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 runnin
into some kind of security related problem
Tom Shelton [MVP
Powered By Gentoo Linux 1.
I have often regretted my speech, never my silence
-- Publilius Syru
 
B

Brent Burkart

hmmm... I am getting a 127 still for GetLastError and "1400 - Invalid
Window Handle" for Marshal.GetLastWin32Error().

I have included the full path of the API's and I have verified those API's
exist.

Any thoughts,
Thanks so much for your help,
Brent
Lisa said:
Brent,
What's the error that you got from GetLastError? (It should be same with Marshal.GetLastWin32Error().)
It looks like that you still can not reach the Longuser function yet... Is
the path right for both advapi32.dll and Kernel32.dll?
 
T

Tom Shelton

Brent,
What's the error that you got from GetLastError? (It should be same with Marshal.GetLastWin32Error().)

Not necessarily. Using GetLastError in a .NET or VB6 for that matter is
a bad idea... You should use the LastDllError property or the
Marshal.GetLastWin32Error function to get the error value. This is due
to the fact that the runtime may make api calls before your call to
GetLastError...
 
T

Tom Shelton

I used GetLastWin32Error and recieved a "Error: 1400 - Invalid window
handle"

Does that mean anything to anyone? I am googling without any luck.

Thanks,
Brent

Brent - that doens't sound correct... I've looked up 127 and that means
that the procedure was not found. I'm not sure why your getting 1400.
Anyway - is this app always running on an NT based os?

I might have to start playing with code soon :)
 
B

Brent Burkart

Thanks Tom

Yeah, I have it running on 2000. The interesting thing is that I have the
exact same code working as a Windows Service. When using the code in a
windows app, I am running into this error. I have stolen other examples of
using "logonuser" and it all returns the same problem.

I am stuck.

Thanks for your thoughts and help.

Brent
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top