0

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!

user1422348
  • 345
  • 1
  • 7
  • 23
  • We cannot see the code you are using to invoke `del`. Presumably, you're going to have to launch a command processor and pass appropriate arguments to it. Are you? Please show a [mcve]. – IInspectable May 26 '20 at 06:31
  • 2
    If for some reason you don't want to run cmd.exe, and are trying not to run other applications that you will need to bundle in, perhaps launch a second, elevated copy of your own exe, passing in command line parameters such that your `Sub Main` will know what action to take, do that silently, and then exit. (In the absence of valid command line, your exe should of course run normally). – tcarvin May 26 '20 at 11:32

1 Answers1

3
cmd /c del <filename>

cmd.exe is an application to run.

/c switch runs a command and exits.

Alex Guteniev
  • 12,039
  • 2
  • 34
  • 79