How to execute WinAPI calls with a different user account

Tdl
Tdl used Ask the Experts™
on
Hi there.

My API stuff works now but when I'm logged in and run my program the API call NetFileEnum gets a access denied which is logical.
I can logoff and login as Admin and then execute it, this works. But I dont wanna logoff/login everytime I need to execute this prgm.

Is there a way that I can promt for a (Domain)Username and Password in my program and then execute those API calls as this user?

Thanks for any hints

Marc
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Try something like this...

'Add the code to a bas module
Option Explicit
Private Declare Function NetUserLogon Lib "Advapi32" Alias "LogonUserA" (ByVal lpszUsername As String, ByVal lpszDomain As Any, ByVal lpszPassword As String, ByVal dwLogonType As Long, ByVal dwLogonProvider As Long, phToken As Long) As Long
' ---------------------------------------------
' Constants used by LogonUser
' ---------------------------------------------'
Private Const LOGON32_PROVIDER_DEFAULT As Long = 0&
Private Const LOGON32_LOGON_NETWORK As Long = 3&
'Private Const LOGON32_PROVIDER_WINNT35 As Long = 1&
'Private Const LOGON32_LOGON_INTERACTIVE As Long = 2&
'Private Const LOGON32_LOGON_BATCH As Long = 4&
'Private Const LOGON32_LOGON_SERVICE As Long = 5&

' Works on Win 95 & NT
Public Function Login2(ByVal UserName As String, ByVal Password As String) As Boolean

  On Error Resume Next ' Don't accept errors here

  Dim p_lngToken As Long
  Dim p_lngRtn As Long

  p_lngRtn = NetUserLogon(UserName, 0&, Password, LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, p_lngToken)

  If p_lngRtn = 0 Then
    Login2 = False
  Else
    Login2 = True
  End If

  On Error GoTo 0

End Function

God luck!
dannic
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in Community Support that this question is:
- points to dannic
Please leave any comments here within the
next seven days.

Commented:
Finalized as proposed

modulo

Community Support Moderator
Experts Exchange

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial