Dos command
-
Hi All I try to run dos command through mfc vc++.I know run dos command through .bat file.But my question is Can i run dos command directly from code? Please help me
Which DOS command are you looking to run from your c++ code?
"Real men drive manual transmission" - Rajesh.
-
Hi All I try to run dos command through mfc vc++.I know run dos command through .bat file.But my question is Can i run dos command directly from code? Please help me
-
Hi All I try to run dos command through mfc vc++.I know run dos command through .bat file.But my question is Can i run dos command directly from code? Please help me
MsmVc wrote:
I try to run dos command through mfc vc++.
If you just want to run a command from you application, go fot the functions mentioned in earlier reply. If you are aiming to run a command and show its output in a window in your application, you have to go for Pipe communication.
-
Hi All I try to run dos command through mfc vc++.I know run dos command through .bat file.But my question is Can i run dos command directly from code? Please help me
C WinAPI solution (you can adopt it in mfc easily i think):
#include <windows.h>
#include <stdio.h>char * ExecuteCMD(char * command)
{
SECURITY_ATTRIBUTES sec;
PROCESS_INFORMATION pi;
STARTUPINFO si;
HANDLE hOutR, hOutW;
DWORD BTAvail;
char * Result = NULL;
char * cmdline = NULL;
char cmdpath[256];
OSVERSIONINFO OSVersionInfo;
DWORD Read = 0;ZeroMemory(&pi, sizeof(PROCESS\_INFORMATION)); ZeroMemory(&sec, sizeof(SECURITY\_ATTRIBUTES)); sec.nLength = sizeof(SECURITY\_ATTRIBUTES); sec.bInheritHandle = TRUE; sec.lpSecurityDescriptor = NULL; if (CreatePipe(&hOutR, &hOutW, &sec, 0)) { ZeroMemory(&si, sizeof(STARTUPINFO)); si.cb = sizeof(STARTUPINFO); si.hStdOutput = hOutW; si.dwFlags = STARTF\_USESTDHANDLES|STARTF\_USESHOWWINDOW; cmdline = (char \*) GlobalAlloc(GMEM\_FIXED, (7 + lstrlen(command))); lstrcat(lstrcpy(cmdline, " /a /c "), command); OSVersionInfo.dwOSVersionInfoSize = sizeof (OSVERSIONINFO); GetVersionEx (&OSVersionInfo); if (OSVersionInfo.dwPlatformId == VER\_PLATFORM\_WIN32\_NT) { GetEnvironmentVariable("ComSpec", cmdpath, 2048); } if (CreateProcess(cmdpath, cmdline, &sec, &sec, TRUE, NORMAL\_PRIORITY\_CLASS, NULL, NULL, &si, &pi)) { WaitForSingleObject(pi.hProcess, INFINITE); CloseHandle(pi.hThread); CloseHandle(pi.hProcess); PeekNamedPipe (hOutR, NULL, 0, NULL, &BTAvail, NULL); if (BTAvail > 0) { Result = (char \*) GlobalAlloc(GMEM\_FIXED, BTAvail + 1); ReadFile(hOutR, Result, BTAvail, &Read, NULL); Result\[BTAvail\] = '\\0'; OemToChar(Result, Result); return Result; } }
}
return NULL;
}int main(int argc, char **argv)
{
char *cmd = ExecuteCMD("dir");
puts(cmd);
}011011010110000101100011011010000110100101101110 0110010101110011
-
-
C WinAPI solution (you can adopt it in mfc easily i think):
#include <windows.h>
#include <stdio.h>char * ExecuteCMD(char * command)
{
SECURITY_ATTRIBUTES sec;
PROCESS_INFORMATION pi;
STARTUPINFO si;
HANDLE hOutR, hOutW;
DWORD BTAvail;
char * Result = NULL;
char * cmdline = NULL;
char cmdpath[256];
OSVERSIONINFO OSVersionInfo;
DWORD Read = 0;ZeroMemory(&pi, sizeof(PROCESS\_INFORMATION)); ZeroMemory(&sec, sizeof(SECURITY\_ATTRIBUTES)); sec.nLength = sizeof(SECURITY\_ATTRIBUTES); sec.bInheritHandle = TRUE; sec.lpSecurityDescriptor = NULL; if (CreatePipe(&hOutR, &hOutW, &sec, 0)) { ZeroMemory(&si, sizeof(STARTUPINFO)); si.cb = sizeof(STARTUPINFO); si.hStdOutput = hOutW; si.dwFlags = STARTF\_USESTDHANDLES|STARTF\_USESHOWWINDOW; cmdline = (char \*) GlobalAlloc(GMEM\_FIXED, (7 + lstrlen(command))); lstrcat(lstrcpy(cmdline, " /a /c "), command); OSVersionInfo.dwOSVersionInfoSize = sizeof (OSVERSIONINFO); GetVersionEx (&OSVersionInfo); if (OSVersionInfo.dwPlatformId == VER\_PLATFORM\_WIN32\_NT) { GetEnvironmentVariable("ComSpec", cmdpath, 2048); } if (CreateProcess(cmdpath, cmdline, &sec, &sec, TRUE, NORMAL\_PRIORITY\_CLASS, NULL, NULL, &si, &pi)) { WaitForSingleObject(pi.hProcess, INFINITE); CloseHandle(pi.hThread); CloseHandle(pi.hProcess); PeekNamedPipe (hOutR, NULL, 0, NULL, &BTAvail, NULL); if (BTAvail > 0) { Result = (char \*) GlobalAlloc(GMEM\_FIXED, BTAvail + 1); ReadFile(hOutR, Result, BTAvail, &Read, NULL); Result\[BTAvail\] = '\\0'; OemToChar(Result, Result); return Result; } }
}
return NULL;
}int main(int argc, char **argv)
{
char *cmd = ExecuteCMD("dir");
puts(cmd);
}011011010110000101100011011010000110100101101110 0110010101110011