Link to home
Start Free TrialLog in
Avatar of qocarlos
qocarlosFlag for Saint Kitts and Nevis

asked on

How to launch an application

I need to execute from my application another program (not Notepad!) and this program must open a txt file.
I tried the following:

void Launch()
{
SHELLEXECUTEINFO aShellExecStruct;
      aShellExecStruct.cbSize = sizeof(SHELLEXECUTEINFO);
      aShellExecStruct.fMask = SEE_MASK_NOCLOSEPROCESS;
      aShellExecStruct.hwnd = ::GetDesktopWindow();
      aShellExecStruct.lpVerb = "open";
      aShellExecStruct.lpFile ="application.exe";
      aShellExecStruct.lpParameters = NULL;
      aShellExecStruct.lpDirectory = "c:\\temp";
      aShellExecStruct.nShow = SW_SHOWNORMAL;
      
      
      BOOL aResult = ::ShellExecuteEx(&aShellExecStruct);      
      if (aResult)
      {
            HANDLE aProcHandle = aShellExecStruct.hProcess;
            if (aProcHandle)
            {
                  ::WaitForSingleObject(aProcHandle, INFINITE);
                  AfxMessageBox("application is done.");
            }
      }
      else
      {
            AfxMessageBox("Could not start the application");
      }
}

This code launchs the application correcly but I don't know how to open a document in this application.

Any idea?

Thanks
Avatar of vachooho
vachooho
Flag of United States of America image

aShellExecStruct.lpParameters = "document_File_name";

most applications has the ability to open document  by specifying it in their command line

try this

Avatar of qocarlos

ASKER

Thanks,
I've also written the application I must execute. The problem is that in this application there is the line
cmdInfo.m_nShellCommand=CCommandLineInfo::FileNothing

in order to keep the application from creating a blank document at startup.

For this reason, when I specify the file in the command line, nothing is opened.
How can I solve this problem?
Avatar of gelbert
gelbert

There are two options:

1. Change your application that before you reset command line(cmdInfo.m_nShellCommand) you save command line in some other variable. This way you would have name of a docunent to open and avoid creating blank document

2. Make COM Outproc server out of your application. Then after creation call interface method to specify name of document to open
try using the

WinExec(LPCSTR filename,NULL)

function. It is easier to use than the shell commands


Good Luck!
ASKER CERTIFIED SOLUTION
Avatar of vachooho
vachooho
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks!