user1422348
user1422348

Reputation: 355

How to use CreateProcessWithLogonW with internal commands

We have a VB6 application that sometimes needs to run a command with elevated permissions, so we invoke CreateProcessWithLogonW function so we can do so without requiring the user to enter the credentials. Currently, we do this with xcopy and it works without a problem. Now I'm trying to implement a delete functionality but I'm getting 0 in the return code, LastDLLError = 2 (I believe this means file not found). So, I think that the reason for this behavior is due to xcopy being an external command, while del is an internal one. Is there any way to use del with CreateProcessWithLogonW? If not, are there any alternatives to using the del command that are just "built-in" (i.e. no batch script use, custom program that I have to store somewhere, etc.)?

Public Declare Function CreateProcessWithLogon Lib "Advapi32" Alias "CreateProcessWithLogonW" _
(ByVal lpUserName As Long, ByVal lpDomain As Long, ByVal lpPassword As Long, ByVal dwLogonFlags _
As Long, ByVal lpApplicationName As Long, ByVal lpCommandLine As Long, ByVal dwCreationFlags As _
Long, ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, lpStartupInfo As STARTUPINFO, _
lpProcessInfo As PROCESS_INFORMATION) As Long
................
    Dim lngReturnValue As Long
    Dim lngProcessID As Long, lngProcessReturnValue As Long
    Dim StartInfo As STARTUPINFO, ProcessInfo As PROCESS_INFORMATION
    Dim strFullCallCommand As String
    Dim i As Long

    On Error GoTo ErrorHandler

    StartInfo.cb = LenB(StartInfo)
    StartInfo.dwFlags = &H1     'new console window
    StartInfo.wShowWindow = &H0 'hide the new console window

    'Run command
    lngReturnValue = CreateProcessWithLogon(StrPtr(strUserName), StrPtr(strDomain), StrPtr(strPassword), &H2&, StrPtr(vbNullString), StrPtr(strCommand), _
                                            &H4000000 Or &H10& Or &H200&, ByVal 0&, StrPtr(vbNullString), StartInfo, ProcessInfo)

    If lngReturnValue = 0 Then
        RaiseErr errExternalProgram_ProgramErredOut, "CreateProcessWithLogonW function failed." & vbCrLf & _
                                                     "LastDLLError Code: " & Err.LastDllError & vbCrLf & _
                                                     "User: " & strUserName & vbCrLf & _
                                                     "Domain: " & strDomain & vbCrLf & _
                                                     "Password: " & String(10, "*") & vbCrLf & _
                                                     "Command: " & strCommand & vbCrLf & _
                                                     "Actual Call: CreateProcessWithLogon(" & CStr(StrPtr(strUserName)) & ", " & CStr(StrPtr(strDomain)) & ", " & _
                                                                   CStr(StrPtr(strPassword)) & ", " & CStr(&H1&) & ", " & CStr(StrPtr(vbNullString)) & ", " & _
                                                                   CStr(StrPtr(strCommand)) & ", " & CStr(&H4000000) & " Or " & CStr(&H10&) & " Or " & _
                                                                   CStr(&H200&) & ", ByVal " & CStr(0&) & ", " & CStr(StrPtr(vbNullString)) & ", [STARTUPINFO Type], [PROCESS_INFORMATION Type])"
    End If

Thanks is advance!

Upvotes: 1

Views: 413

Answers (1)

Alex Guteniev
Alex Guteniev

Reputation: 13689

cmd /c del <filename>

cmd.exe is an application to run.

/c switch runs a command and exits.

Upvotes: 3

Related Questions