Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Creating a Process with an Impersonated User
Message
From
17/10/2005 07:48:51
 
 
To
All
General information
Forum:
Visual FoxPro
Category:
Windows API functions
Title:
Creating a Process with an Impersonated User
Environment versions
Visual FoxPro:
VFP 9
OS:
Windows XP SP2
Miscellaneous
Thread ID:
01059576
Message ID:
01059576
Views:
100
Hi,

I'm trying to make a program that creates a process (by using the CreateProcessAsUser api call), after impersonating to a new user (by using the LogonUser and ImpersonateLoggedOnUser api calls).

The impersonation seems to be working fine, but the CreateProcessAsUser returns an error code.

Below is the prg I'm using to make my tests.

Any help will be appreciated.

Regards,

Fernando

PS. I made up this test program using information contained in Message #88880 and MSDN.
* ===========================================================================
*                              Test Parameters
* ===========================================================================

* To be used by LogonUser

 cLoginId      = "myuserlogin"
 cPassword     = "mypassword" 
 cDomain       = "mydomain"   

* To be used by CreateProcessAsUser

 cCommandLine = "C:\Temp\Test.Bat"

* ===========================================================================
*                             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 @lnUserHandle

 Declare Short ImpersonateLoggedOnUser In AdvApi32 Integer lnUserHandle

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

 nUserHandle = 0

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

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

 nSuccess = ImpersonateLoggedOnUser (nUserHandle)

 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

 Declare Integer CreateProcessAsUser In advapi32 Integer hToken             , ;
                                                 String  lpApplicationName  , ;
                                                 String  lpCommandLine      , ;
                                                 Integer lpProcessAttributes, ;
                                                 Integer lpThreadAttributes , ;
                                                 Integer bInheritHandles    , ;
                                                 Integer dwCreationFlags    , ;
                                                 Integer lpEnvironment      , ;
                                                 Integer lpCurrentDirectory , ;
                                                 String @lpStartupInfo      , ;
                                                 String @lpProcessInformation

 cStart           = Long2Str  (68) + Replicate (Chr (0), 64)
 cProcess_Info    = Replicate (Chr (0), 16)
 cApplicationName = Chr (0)
 cCommandLine     = cCommandLine + Chr (0)

 nRetCode         = CreateProcessAsUser (nUserHandle          , ;
                                         cApplicationName     , ;
                                         cCommandLine         , ;
                                         0                    , ;
                                         0                    , ;
                                         1                    , ;
                                         NORMAL_PRIORITY_CLASS, ;
                                         0                    , ;
                                         0                    , ;
                                         @cStart              , ;
                                         @cProcess_Info)

 If nRetCode = 0 
    Messagebox ("Error in CreateProcessAsUser")
    Return
 endif

 Messagebox ("Process successfully created!")

 Return

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

 Function Long2Str
*-------- --------
 LParameters pLongVal

 Private I, cRetStr, nLongVal

 cRetStr  = ""
 nLongVal = pLongVal

 For I = 24 To 0 Step -8

     cRetStr  = Chr (Int (nLongVal / (2^I))) + cRetStr
     nLongVal = Mod (     nLongVal,  (2^I))

 EndFor

 Return cRetStr

 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

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

Click here to load this message in the networking platform