如何通过内部命令使用CreateProcessWithLogonW
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何通过内部命令使用CreateProcessWithLogonW相关的知识,希望对你有一定的参考价值。
我们有一个VB6应用程序,有时需要以提升的权限运行命令,因此我们调用CreateProcessWithLogonW函数,因此我们可以这样做,而无需用户输入凭据。目前,我们使用xcopy进行此操作,并且可以正常工作。现在,我正在尝试实现删除功能,但返回代码LastDLLError = 2(我相信这意味着找不到文件)的返回值为0。因此,我认为此行为的原因是由于xcopy是外部命令,而del是内部命令。有什么方法可以将del与CreateProcessWithLogonW一起使用?如果不是,是否有任何其他替代方式可以使用“内置”的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>
以上是关于如何通过内部命令使用CreateProcessWithLogonW的主要内容,如果未能解决你的问题,请参考以下文章