Link to home
Create AccountLog in
C++

C++

--

Questions

--

Followers

Top Experts

Avatar of Tycotrix
Tycotrix

how do i create a process in c++?
how do i write a Win32 Console application that starts a new process.  That process should be an instance of Notepad.exe.  The program should output the Process ID of the created process.  Once the program has started the new process it should monitor it (displaying a dot on the screen every second while monitoring) until the Notepad process terminates.  It should then itself close down. This entire project shld be done either in c or c++.


Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of grg99grg99

Very easy, here's a start of this.:


PROCESS_INFORMATION procInfo;
STARTUPINFO startupInfo = {0};
startupInfo.cb = sizeof(STARTUPINFO);
char attr[] = "NotePad.exe -options";
char process[] = "NotePad.exe";

// start the ScriptFile
m_create  = CreateProcess(process, attr,NULL, NULL, FALSE, 0, NULL, NULL, &startupInfo, &procInfo);
if (m_create == 0) {
 m_error = GetLastError();
}
else
{
      // wait for process to finish...
     while( processrunning( m_create ) ) { ... };

}




Avatar of TycotrixTycotrix

ASKER

i can't seem to get it to work....

the following error messages i received after compiling the program....

c:\program files\microsoft visual studio\myprojects\ass12\ass12.cpp(11) : error C2065: 'm_create' : undeclared identifier
c:\program files\microsoft visual studio\myprojects\ass12\ass12.cpp(13) : error C2065: 'm_error' : undeclared identifier
c:\program files\microsoft visual studio\myprojects\ass12\ass12.cpp(18) : error C2065: 'processrunning' : undeclared identifier
c:\program files\microsoft visual studio\myprojects\ass12\ass12.cpp(18) : error C2143: syntax error : missing ';' before '...'
c:\program files\microsoft visual studio\myprojects\ass12\ass12.cpp(21) : fatal error C1004: unexpected end of file found
Error executing cl.exe.

I'm sorry but i'm kinda new to c++..could u help me out with the entire code?? thanks...

You can use CreateProcess to create the thread like grg99 said.  Then you can use WaitForSingleObject to wait for the process to finish, and to display a . every second you'll have to create a thread function to do that.  Something like:

#include <windows.h>
#include <iostream>

using namespace std;

bool finished;

// This is a thread function that just displays a " . " every second until
// the finished flag is set
DWORD WINAPI monitor_function(LPVOID parameter)
{
  while(! finished)
  {
    std::cout << " . ";
    Sleep(1000);
  }

  return 0;
}

int main()

{
  // Set the startup information
  STARTUPINFO startup_info = {0};
  startup_info.cb = sizeof startup_info;
  PROCESS_INFORMATION pi = {0};
  finished = false; // Set a flag for the thread

  // Create the process
  DWORD result = CreateProcess("c:\\winnt\\system32\\notepad.exe", "c:\\winnt\\system32\\notepad.exe c:\\test.txt", NULL, NULL, FALSE, 0, NULL, NULL, &startup_info, &pi);
  if(result == 0)
  {
    // Error
    return 0;
  }

  // Create the thread that will wait for it to return
  DWORD thread_id;
  CreateThread(NULL, 0, monitor_function, NULL, 0, &thread_id);
 
  // This function in this thread will wait for notepad to go away
  ::WaitForSingleObject(pi.hProcess, -1);
  finished = true; // Let the thread know

  return 0;
}

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


ok i'm sorry to sound so dumb but how do i combine the 2 codes as 1 to get the entire working program...???

how do i combine grg99's code and mnashadka 's code??

My code already contains the CreateProcess from grg99's code.

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


it doesn't work.....the notepad doesn't open....only the dos screen opens.....

ASKER CERTIFIED SOLUTION
Avatar of mnashadkamnashadka

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

thanks alot mnashadka ...it really works....thanks alot....

#include <windows.h>
#include <iostream>
using namespace std;


int main(int argc, char *argv[]) {
      // Local variables
      PROCESS_INFORMATION pi;
      STARTUPINFO si;

      // Argument count
      if (argc != 2)
            return EXIT_FAILURE;

      // Initialize
      memset(&si,0,sizeof(si));
      si.cb = sizeof(si);

      // Execute
      if(!CreateProcess(NULL, argv[1], NULL, NULL, false, 0, NULL,NULL,&si,&pi)) {
            // Failed
            cout << "Could not run the program." << endl;
            return EXIT_FAILURE;
}
      }




CAN ANYONE HELP ME ON THIS TO MAKE IT ERROR FREE???

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.

C++

C++

--

Questions

--

Followers

Top Experts

C++ is an intermediate-level general-purpose programming language, not to be confused with C or C#. It was developed as a set of extensions to the C programming language to improve type-safety and add support for automatic resource management, object-orientation, generic programming, and exception handling, among other features.