Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Dos command

Dos command

Scheduled Pinned Locked Moved C / C++ / MFC
c++questionhelp
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MsmVc
    wrote on last edited by
    #1

    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

    R A C C 4 Replies Last reply
    0
    • M MsmVc

      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

      R Offline
      R Offline
      Rajesh R Subramanian
      wrote on last edited by
      #2

      Which DOS command are you looking to run from your c++ code?

      "Real men drive manual transmission" - Rajesh.

      1 Reply Last reply
      0
      • M MsmVc

        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

        A Offline
        A Offline
        Andrew Brock
        wrote on last edited by
        #3

        The system()[^] command can be used to execute an operating system function. Also read about it here[^]. You could also use CreateProcess[^] if you are wanting to execute a program

        M 1 Reply Last reply
        0
        • M MsmVc

          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 Offline
          C Offline
          Cool_Dev
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • M MsmVc

            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 Offline
            C Offline
            csrss
            wrote on last edited by
            #5

            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

            M 1 Reply Last reply
            0
            • A Andrew Brock

              The system()[^] command can be used to execute an operating system function. Also read about it here[^]. You could also use CreateProcess[^] if you are wanting to execute a program

              M Offline
              M Offline
              MsmVc
              wrote on last edited by
              #6

              Thanks problem solved

              1 Reply Last reply
              0
              • C csrss

                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

                M Offline
                M Offline
                MsmVc
                wrote on last edited by
                #7

                Thanks for solution Problem solved

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups