Thanks for the advice. Everything seems to be working well. My code
is below. My only problem is how to open Notepad with a pre-existing
(blank) file that I've created. More simulating keypresses? Or is
there a way of doing it when CreateProcess is used?
STARTUPINFO si;
PROCESS_INFORMATION pi;
DWORD dwCode = 0;
HWND hwnd;
ZeroMemory(&si, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_SHOWNORMAL;
int i;
if(CreateProcess(NULL, // No module name (use command line)
"notepad.exe", // The command line to be executed
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // The handles are not inherited
NORMAL_PRIORITY_CLASS, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi)) // Pointer to PROCESS_INFORMATION
structure
{
cout<<"The ID of notepad is "<<pi.dwProcessId <<endl;
WaitForSingleObject(pi.hProcess, 5000); // Wait in
milliseconds
hwnd = GetForegroundWindow(); // Returns a handle to the
foreground window
DWORD processId;
DWORD threadId = GetWindowThreadProcessId
(hwnd,&processId); // returns identifier of the thread that created
the window
// Simulating a Ctrl+S keystroke
keybd_event(VK_LCONTROL, 0x9d, 0, 0); // Left Ctrl Press
keybd_event('S', 0, 0, 0); // S Press
keybd_event('S', 0, KEYEVENTF_KEYUP, 0); // S Release
keybd_event(VK_LCONTROL, 0x9d, KEYEVENTF_KEYUP, 0); // Left
Ctrl Release
// Simulating an Alt+F4 keystroke
keybd_event(VK_MENU, 0xb8, 0, 0); // Alt Press
keybd_event(VK_F4, 0xbe, 0, 0); // F4 Press
keybd_event(VK_F4, 0xbe, KEYEVENTF_KEYUP, 0); // F4 Release
keybd_event(VK_MENU, 0xb8, KEYEVENTF_KEYUP, 0); // Alt Release
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
}
else
{
cout << "CreateProcess failed: "<< GetLastError() << endl;
}
Thanks again for all the help. I never would have worked it out on my
own.
--- In [email protected], Thomas Hruska <[EMAIL PROTECTED]> wrote:
>
> remarknibor wrote:
> > I'm using Visual C++ 2005 in Windows, and I'm writing a console
> > application to run from the command prompt.
> > I was just wondering if it was possible to have my program open
up
> > Notepad at a certain point so that users could write an essay in
it,
> > and then have my program save the file to a specified location
after a
> > certain amount of time has passed and close Notepad again.
Basically,
> > I'd like to use Notepad for the essay writing capabilities rather
than
> > the command prompt, but would like complete control over when it
> > opens/closes and where it saves the current writing to.
> > If this is possible, could you explain how it can be done? (It
doesn't
> > have to be Notepad, rather it could be any basic text editor.
> > So far, all I've found is that I can open Notepad up from my
code
> > using:
> > system("notepad.exe");
> >
> > Thanks in advance.
>
> What you want to do is non-ANSI Standard (but so is anything else
> involving timed user input). You are going to have to deviate from
> Standard C++ and delve into the Windows API to do what you want.
Search
> MSDN Library for the following APIs:
>
> CreateProcess()
> WaitForSingleObject()
> GetForegroundWindow()
> GetWindowThreadProcessId()
> keybd_event()
>
> You'll use them in that specific order. I recommend sending Ctrl+S
(or
> Alt+F and then S) and then Alt+F4 (or Alt+F and then X) to force
the
> app. to exit. You should start Notepad with a file to load (create
an
> empty file). WaitForSingleObject() will keep the CPU at 0% while
> waiting for the time to run out OR the person exits Notepad
(whichever
> comes first).
>
> --
> Thomas Hruska
> CubicleSoft President
> Ph: 517-803-4197
>
> *NEW* MyTaskFocus 1.1
> Get on task. Stay on task.
>
> http://www.CubicleSoft.com/MyTaskFocus/
>