Solved

Spawning another application from MFC application

Posted on 1998-10-29
2
181 Views
Last Modified: 2013-11-20
I have a VC++ application using MFC. How do I start notepad from this application and load a particular txt file in the notepad? I want to use this to display a log file generated by my application.
Thanks
0
Comment
Question by:sm176811
  • 2
2 Comments
 
LVL 7

Accepted Solution

by:
psdavis earned 50 total points
ID: 1323980
This is what I use for running one of my programs...

      HMODULE hKernel = GetModuleHandle( "KERNEL32.DLL" );

      if( hKernel != NULL )
      {
      // Find the addresses of the requested functions

         CREATESNAPSHOT hCreateToolhelp32Snapshot = (CREATESNAPSHOT) GetProcAddress( hKernel, "CreateToolhelp32Snapshot" );
         PROCESSWALK    hProcess32First           = (PROCESSWALK)    GetProcAddress( hKernel, "Process32First"           );
         PROCESSWALK    hProcess32Next            = (PROCESSWALK)    GetProcAddress( hKernel, "Process32Next"            );

         if( hCreateToolhelp32Snapshot != NULL && hProcess32First != NULL && hProcess32Next != NULL )
         {
            PROCESSENTRY32 hProcEntry;
            hProcEntry.dwSize = sizeof( hProcEntry );

            HANDLE hSnapshot = hCreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );

            hProcess32First( hSnapshot, &hProcEntry );

            CString strEngine     = _T( "ENGINE.EXE" );

         // Now loop through all of the executing programs to see if the engine is already started

            bool bEngineFound = false;

            while( hProcess32Next( hSnapshot, &hProcEntry ) == TRUE )
            {
               CString strExecutable = hProcEntry.szExeFile;

               if( strExecutable.Find( strEngine ) != -1 )
               {
                  bEngineFound = true;
                  break;
               }
            }

         // OK, let's run it!

            if( bEngineFound == false )
            {
               CString strDrive, strDirectory, strFilename, strExtension;

               _splitpath(AfxGetApp()->m_pszHelpFilePath,
                          strDrive   .GetBuffer( _MAX_DRIVE ), strDirectory.GetBuffer( _MAX_DIR ),
                          strFilename.GetBuffer( _MAX_FNAME ), strExtension.GetBuffer( _MAX_EXT ) );

               ShellExecute( this->GetSafeHwnd( ), "open", strEngine, NULL, strDirectory, SW_SHOWMINIMIZED );
            }
         }
      }

Phillip
0
 
LVL 7

Expert Comment

by:psdavis
ID: 1323981
To have notepad open up the correct file, add a parameter to notepad.exe with the name of the file that you want.

Phillip
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now