Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Creating a Process with an Impersonated User
Message
General information
Forum:
Visual FoxPro
Category:
Windows API functions
Environment versions
Visual FoxPro:
VFP 9
OS:
Windows XP SP2
Miscellaneous
Thread ID:
01059576
Message ID:
01059596
Views:
19
Thanks once more, Anatoly!

Made all changes and (below) got the updated program, that still doesn't work. It returns an error code #2 (System could not find specified file" - this was translated from portuguese).
* ===========================================================================
*                              Test Parameters
* ===========================================================================

* To be used by LogonUser

 cLoginId      = "myloginid"
 cPassword     = "mypassword" 
 cDomain       = "mydomain"

* To be used by CreateProcessAsUser

 cCommandLine = "c:\windows\notepad.exe"

* ===========================================================================
*                             Get Test Parameters
* ===========================================================================

 bAbortTest = .F.

 oGetLoginInfo = CreateObj ("frmGetLoginInfo")

 oGetLoginInfo.Show ()

 Clear Events

 If bAbortTest
    Return
 endif

* ===========================================================================
*                            Impersonation Stuff
* ===========================================================================

* dwLogonProvider:

 #Define LOGON32_PROVIDER_DEFAULT  0
 #Define LOGON32_PROVIDER_WINNT50  3
 #Define LOGON32_PROVIDER_WINNT40  2
 #Define LOGON32_PROVIDER_WINNT35  1

* dwLogonType:

 #Define LOGON32_LOGON_INTERACTIVE 2
 #Define LOGON32_LOGON_NETWORK     3
 #Define LOGON32_LOGON_BATCH       4
 #Define LOGON32_LOGON_SERVICE     5

 Declare Short LogonUser               In AdvApi32 String  lcNewUserName  , ;
                                                   String  lcDomainName   , ;
                                                   String  lcPassWord     , ;
                                                   Integer lnLogonType    , ;
                                                   Integer lnLogonProvider, ;
                                                   Integer @phToken

 Declare Short ImpersonateLoggedOnUser In AdvApi32 Integer hToken


* ===========================================================================
*                         Logs User on and Impersonates
* ===========================================================================

 nToken   = 0

 nSuccess = LogonUser (cLoginId                 , ;
                       cDomain                  , ;
                       cPassword                , ;
                       LOGON32_LOGON_INTERACTIVE, ;
                       LOGON32_PROVIDER_DEFAULT , ;
                       @nToken                  )

 If nSuccess = 0
    Messagebox ("Error in LogonUser")
    Return
 endif

 nSuccess = ImpersonateLoggedOnUser (nToken)

 If nSuccess = 0
    Messagebox ("Error in ImpersonateLoggedOnUser")
    Return
 endif

* ===========================================================================
*                          Checks User Impersonation
* ===========================================================================

 nRetVal        = 0
 lpUserIDBuffer = Space (25)
 nBufferSize    = 25        

 Declare Integer GetUserName In Win32API String  @lpUserIDBuffer, ;
                                         Integer @nBufferSize

 nRetVal = GetUserName (@lpUserIDBuffer, @nBufferSize)

 cLogUserId = Left (lpUserIDBuffer, nbuffersize-1)

 MessageBox ("Impersonated user is: " + AllTrim (cLogUserId))

* ===========================================================================
*                               Task Creation
* ===========================================================================

 #Define NORMAL_PRIORITY_CLASS       32
 #Define IDLE_PRIORITY_CLASS         64
 #Define HIGH_PRIORITY_CLASS        128
 #Define REALTIME_PRIORITY_CLASS   1600

 #Define STARTF_USESHOWWINDOW      1
 #Define SW_SHOWNORMAL             1 
 #Define CREATE_DEFAULT_ERROR_MODE 0x04000000
 #Define CREATE_NEW_CONSOLE        0x00000010
 #Define CREATE_NEW_PROCESS_GROUP  0x00000200
 
 DECLARE INTEGER CreateProcessAsUser IN advapi32 INTEGER hToken             , ;
                                                 STRING  lpApplicationName  , ;
                                                 STRING  lpCommandLine      , ;
                                                 INTEGER lpProcessAttributes, ;
                                                 INTEGER lpThreadAttributes , ;
                                                 INTEGER bInheritHandles    , ;
                                                 INTEGER dwCreationFlags    , ;
                                                 INTEGER lpEnvironment      , ;
                                                 STRING  lpCurrentDirectory , ;
                                                 STRING @lpStartupInfo      , ;
                                                 STRING @lpProcessInformation

 cProcInfo  = Replicate (Chr (0), 16)

 cStartInfo = num2dword (68)                   + ;
              num2dword (0)                    + ;
              num2dword (0)                    + ;
              num2dword (0)                    + ;
              num2dword (0)                    + ;
              num2dword (0)                    + ;
              num2dword (0)                    + ;
              num2dword (0)                    + ;
              num2dword (0)                    + ;
              num2dword (0)                    + ;
              num2dword (0)                    + ;
              num2dword (STARTF_USESHOWWINDOW) + ;
              num2dword (SW_SHOWNORMAL       ) + ;
              num2dword (0)                    + ;
              num2dword (0)                    + ;
              num2dword (0)                    + ;
              num2dword (0)

 nCreationFlags = NORMAL_PRIORITY_CLASS        + ;
                  CREATE_DEFAULT_ERROR_MODE    + ;
                  CREATE_NEW_CONSOLE           + ;
                  CREATE_NEW_PROCESS_GROUP

 cCommandLine = cCommandLine + Chr (0)
 cCmd         = Chr (0)
 cDir         = Chr (0)

 If CreateProcessAsUser (nToken              , ;
                         cCommandLine        , ;
                         " " + AllTrim (cCmd), ;
                         0                   , ;
                         0                   , ;
                         0                   , ;
                         nCreationFlags      , ;
                         0                   , ;
                         cDir                , ;
                         @cStartInfo         , ;
                         @cProcInfo          ) = 0
	ShowLastError ()

	Return
 
 endif

 Messagebox ("Process successfully created!")

 Return

* ===========================================================================

 Procedure ShowLastError
*-------- --------------
              
 #Define FORMAT_MESSAGE_FROM_SYSTEM 4096

 Declare Integer GetLastError  In Kernel32

 Declare Integer FormatMessage In Kernel32 Integer dwFlags        , ;
                                           String  lpSource       , ;
                                           Integer dwMessageId    , ;
                                           Integer dwLanguageId   , ;
                                           String  @lpBuffer      , ;
                                           Integer nSize          , ;
                                           Integer Arguments
 
 nLastError   = GetLastError  ()
 cMessageText = Space         (256)
 nRetCode     = FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM, 0, nLastError, 0, @cMessageText, 256, 0)

 Messagebox ("Error #" + lTrim (Str (nLastError)) + ":" + cMessageText)

 Return

* ===========================================================================

 Function num2dword (lnValue) 
*-------- ---------

 #DEFINE m0  0x0000100
 #DEFINE m1  0x0010000
 #DEFINE m2  0x1000000

 IF lnValue < 0
    nValue = 0x100000000 + lnValue
 ENDIF

 LOCAL b0, b1, b2, b3

 b3 = Int(lnValue/m2)
 b2 = Int((lnValue - b3*m2)/m1)
 b1 = Int((lnValue - b3*m2 - b2*m1)/m0)
 b0 = Mod(lnValue, m0)

 RETURN Chr(b0)+Chr(b1)+Chr(b2)+Chr(b3) 
 
 EndFunc 

* ===========================================================================

 Define Class frmGetLoginInfo as Form
*------ ----- --------------- -- ----

        Top         =   0
        Left        =   0
        Height      = 160
        Width       = 330
        AutoCenter  =  .T.
        ControlBox  =  .T.
        MaxButton   =  .F.
        MinButton   =  .F.
        ZoomBox     =  .F.
        SizeBox     =  .F.
        ShowTips    =  .T.
        ShowWindow  =   0
        WindowType  =   1
        Caption     = " Please Enter Impersonation Info"

        Add Object lblLoginId   as Label         with Top          =  15        , ;
                                                      Left         =  10        , ;
                                                      AutoSize     =  .T.       , ;
                                                      FontName     =  "Arial"   , ;
                                                      FontSize     =   8        , ;
                                                      Caption      = "Login Id:"
 
        Add Object txtLoginId as TextBox         with Top          =  10        , ;
                                                      Left         =  70        , ;
                                                      Height       =  20        , ;
                                                      Width        = 250        , ;
                                                      FontName     =  "Arial"   , ;
                                                      FontSize     =   8        , ;
                                                      Margin       =   1        , ;
                                                      MaxLength    =  60        , ;
                                                      Value        = Space (60)

        Add Object lblPassword  as Label         with Top          =  45        , ;
                                                      Left         =  10        , ;
                                                      AutoSize     =  .T.       , ;
                                                      FontName     =  "Arial"   , ;
                                                      FontSize     =   8        , ;
                                                      Caption      = "Password:"
 
        Add Object txtPassword as  TextBox       with Top          =  40        , ;
                                                      Left         =  70        , ;
                                                      Height       =  20        , ;
                                                      Width        = 250        , ;
                                                      FontName     =  "Arial"   , ;
                                                      FontSize     =  12        , ;
                                                      PasswordChar =  "*"       , ;
                                                      Margin       =   1        , ;
                                                      MaxLength    =  60        , ;
                                                      Value        = Space (60)

        Add Object lblDomain    as Label         with Top          =  75        , ;
                                                      Left         =  10        , ;
                                                      AutoSize     =  .T.       , ;
                                                      FontName     =  "Arial"   , ;
                                                      FontSize     =   8        , ;
                                                      Caption      = "Domain"
 
        Add Object txtDomain    as TextBox       with Top          =  70        , ;
                                                      Left         =  70        , ;
                                                      Height       =  20        , ;
                                                      Width        = 250        , ;
                                                      FontName     =  "Arial"   , ;
                                                      FontSize     =   8        , ;
                                                      Margin       =   1        , ;
                                                      MaxLength    =  60        , ;
                                                      Value        = Space (60)

        Add Object lblCommand   as Label         with Top          = 105        , ;
                                                      Left         =  10        , ;
                                                      AutoSize     =  .T.       , ;
                                                      FontName     =  "Arial"   , ;
                                                      FontSize     =   8        , ;
                                                      Caption      = "Command:"
 
        Add Object txtCommand  as TextBox        with Top          = 100        , ;
                                                      Left         =  70        , ;
                                                      Height       =  20        , ;
                                                      Width        = 250        , ;
                                                      FontName     =  "Arial"   , ;
                                                      FontSize     =   8        , ;
                                                      Margin       =   1        , ;
                                                      MaxLength    = 250        , ;
                                                      Value        = Space (250)

        Add Object cmdOK        as CommandButton with Top          = 130        , ;
                                                      Left         = 165        , ;
                                                      Height       =  25        , ;
                                                      Width        =  75        , ;
                                                      FontName     =  "Arial"   , ;
                                                      FontSize     =   8        , ;
                                                      Caption      = "\<OK"    

        Add Object cmdCancel    as CommandButton with Top          = 130        , ;
                                                      Left         = 245        , ;
                                                      Height       =  25        , ;
                                                      Width        =  75        , ;
                                                      FontName     =  "Arial"   , ;
                                                      FontSize     =   8        , ;
                                                      Cancel       = .T.        , ;
                                                      Caption      = "\<Cancel"

        Procedure cmdCancel.Click
*                 --------- -----
                  bAbortTest = .T.

                  ThisForm.Release
        EndProc

        Procedure cmdOK.Click
*                 ----- -----
                  cLoginId     = AllTrim (ThisForm.txtLoginId.Value )
                  cPassword    = AllTrim (ThisForm.txtPassword.Value)
                  cDomain      = AllTrim (ThisForm.txtDomain.Value  )
                  cCommandLine = AllTrim (ThisForm.txtCommand.Value )

                  bAbortTest = .F.

                  ThisForm.Release
        EndProc

        Procedure Activate
*                 --------
                  Set Cursor On
        EndProc

        Procedure Init
*                 ----
                  ThisForm.txtLoginId.Value  = cLoginId     
                  ThisForm.txtPassword.Value = cPassword    
                  ThisForm.txtDomain.Value   = cDomain      
                  ThisForm.txtCommand.Value  = cCommandLine 
        EndProc

        Procedure QueryUnload
*                 -----------
                  ThisForm.cmdCancel.Click

                  NoDefault

                  Return .T.
        EndProc

 EndDefine

* ===========================================================================
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform