[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 230
  • Last Modified:

Spawning another application from MFC application

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
sm176811
Asked:
sm176811
  • 2
1 Solution
 
psdavisCommented:
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
 
psdavisCommented:
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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now