Solved

Programmatically shutting down Windows Notepad

Posted on 1998-10-04
6
198 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 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
Independent Software Vendors: 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

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!

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month11 days, 16 hours left to enroll

623 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