Solved

Programmatically shutting down Windows Notepad

Posted on 1998-10-04
6
186 Views
Last Modified: 2010-04-06
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
Comment
Question by:aztec
  • 2
  • 2
  • 2
6 Comments
 
LVL 10

Accepted Solution

by:
viktornet earned 50 total points
ID: 1341754
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
 
LVL 20

Expert Comment

by:Madshi
ID: 1341755
aztec,

if you want to close the notepad use something like

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

Regards, Madshi.
0
 

Author Comment

by:aztec
ID: 1341756
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 10

Expert Comment

by:viktornet
ID: 1341757
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
 
LVL 20

Expert Comment

by:Madshi
ID: 1341758
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
 

Author Comment

by:aztec
ID: 1341759
Thanks Viktor.

Regards,
   Shawn
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 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

10 Experts available now in Live!

Get 1:1 Help Now