如何通过内部命令使用 CreateProcessWithLogonW

How to use CreateProcessWithLogonW with internal commands

我们有一个 VB6 应用程序,有时需要 运行 具有提升权限的命令,因此我们调用 CreateProcessWithLogonW 函数,这样我们就可以在不需要用户输入凭据的情况下执行此操作。目前,我们使用 xcopy 执行此操作并且它可以正常工作。现在我正在尝试实现删除功能,但我在 return 代码中得到 0,LastDLLError = 2(我相信这意味着找不到文件)。所以,我认为这种行为的原因是由于 xcopy 是一个外部命令,而 del 是一个内部命令。有没有办法将 del 与 CreateProcessWithLogonW 一起使用?如果没有,除了 "built-in"(即不使用批处理脚本、我必须存储在某处的自定义程序等)之外,是否还有使用 del 命令的替代方法?

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

提前致谢!

cmd /c del <filename>

cmd.exe 是对 运行 的申请。

/c 切换 运行 命令并退出。