Go Premium for a chance to win a PS4. Enter to Win

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

Programmatically shutting down Windows Notepad

My application creates a report file for the user to view in
Windows Notepad, by simply clicking on a speedbutton. However, sometimes some users forget to close down this file and run my program again - which will attempt to re-create this report file. When this happens, they of course get an I/O error window because this report file is already open in Windows Notepad.
  is there a way I can automatically close this file (..if it happens to be open) whenever the user re-runs my program? It would save them from crashing the program.

Thanks
  Shawn Halfpenny
  drumme59@sprint.ca
0
aztec
Asked:
aztec
  • 2
  • 2
  • 2
1 Solution
 
viktornetCommented:
interface
uses Windows;
ResourceString
  AppCantStart = 'App is already running. Can not start another copy.';
  AppTitle = 'Just an App';
implementation
const
  TEN_SECS = 10000;
var
  MutexHandle : Integer;

{...}//Your code goes here...

initialization
  MutezHandle := CreateMutex(nil, False, 'Some Mutex.');
  if WaitForSingleObject(MutexHandle, TEN_SECS) <> WAIT_OBJECT_0 then
  begin
    MutexHandle := -1;
    MessageBox(0, PChar(RSCantStart), PChar(RSTitle), MB_OK);
    Halt;
  end;
finalization
  if MutexHandle <> -1 then
    ReleaseMutex(MutexHandle);
end.

Regards,
Viktor Ivanov
0
 
MadshiCommented:
aztec,

if you want to close the notepad use something like

(1) PostMessage(FindWindow('Notepad',nil),WM_CLOSE,0,0);

Regards, Madshi.
0
 
aztecAuthor Commented:
Yes, but I don't want to just close down Notepad - as the user could have other files open which he is working on. Ideally I would like to check if the specific .rpt file is open, THEN close it down. See what I mean? Is there a way to check if a certain file is open, and if it is, THEN close it?

Regards
   Shawn
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
viktornetCommented:
var
  F : File;
begin
  try  
    AssignFile(F, 'C:\windows\desktop\test.txt');
    try
      Reset(f);
    except
      SendMessage(TheFileHandle, WM_CLOSE, 0, 0);
  finally
    CloseFile(F);
  end;
end;

Not sure if it works and if this is the way to do but you might want to try it out....

//Viktor
0
 
MadshiCommented:
aztec,

you could look if your filename is in the notepad's title bar (GetWindowText).
If you want to check if the file is open, use an exception statement (see Viktor's last comment).

Regards, Madshi.
0
 
aztecAuthor Commented:
Thanks Viktor.

Regards,
   Shawn
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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