Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Programmatically shutting down Windows Notepad

Posted on 1998-10-04
6
Medium Priority
?
200 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 10

Accepted Solution

by:
viktornet earned 150 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
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!

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

660 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