Código
Public Function CMD(Comando As String) As String On Error GoTo Error_cmd Dim hReadPipe As Long Dim hWritePipe As Long Dim SecAtt As SECURITY_ATTRIBUTES Dim ProcInfo As PROCESS_INFORMATION Dim StUpInfo As STARTUPINFO Dim Ret As Long Dim Buffer As String * 256 Dim BytesRead As Long SecAtt.nLength = Len(SecAtt) SecAtt.bInheritHandle = 1& SecAtt.lpSecurityDescriptor = 0& If CreatePipe(hReadPipe, hWritePipe, SecAtt, 0) = 0 Then CMD = "Error: " & Err.LastDllError Exit Function End If StUpInfo.cb = Len(StUpInfo) StUpInfo.dwFlags = STARTF_USESTDHANDLES Or STARTF_USESHOWWINDOW StUpInfo.hStdOutput = hWritePipe StUpInfo.hStdError = hWritePipe Comando = Environ("COMSPEC") & " /c " & Comando If CreateProcessA(vbNullString, Comando, SecAtt, SecAtt, 1&, NORMAL_PRIORITY_CLASS, 0&, 0&, StUpInfo, ProcInfo) = 0 Then CMD = "Error: " & Err.LastDllError Exit Function End If CloseHandle (hWritePipe) CMD = "" Do Ret = ReadFile(hReadPipe, Buffer, 256, BytesRead, 0&) CMD = CMD & Left$(Buffer, BytesRead) Loop While Ret <> 0 CloseHandle (ProcInfo.hThread) CloseHandle (ProcInfo.hProcess) CloseHandle (hWritePipe) Exit Function Error_cmd: CMD = "Error: " & Err.Description End Function