Solved

Let the app only run once

Posted on 1998-12-19
8
210 Views
Last Modified: 2010-04-04
how can I check, whether my app is already run or not?
I mean: the user shall not be allowed to run my app more than one time. I have a dizzy idea of how this works, but I'm being lazy, so go ahead, get the points...  :-)

Greetings, Oli
0
Comment
Question by:Oli2
  • 5
  • 3
8 Comments
 

Accepted Solution

by:
Metty earned 100 total points
ID: 1352012
//the usual stuff above
var hwnd: Word;
 
begin
  if hPrevInst = 0 then
  begin
    Application.CreateForm(TForm1, Form1);
    Application.Run;
  end
  else
  begin
    hwnd := FindWindow('TForm1', nil);
    if (not IsWindowVisible(hwnd)) then
    begin
      ShowWindow(hwnd, sw_ShowNormal);
      PostMessage(hwnd, wm_User, 0, 0);
    end;
    else
      SetWindowPos(hwnd, HWND_TOP, 0,0,0,0,
        SWP_NOSIZE or SWP_NOMOVE);
  end;
end.
//====================================================

In the form's PAS file add a message response function for the wm_User
message.

//====================================================
{in the form declaration}
public
  procedure WMUser(var msg: TMessage); message wm_User;

{in the implementation section}
procedure TForm1.WMUser(var msg: TMessage);
begin
  Application.Restore;
end;

0
 
LVL 1

Author Comment

by:Oli2
ID: 1352013
Hi, Metty!
Thanx for the answer. I'm checking it. If it works out, you'll get the points. Give me an hour or two.

Greetings, Oli
0
 
LVL 1

Author Comment

by:Oli2
ID: 1352014
Hi, Metty!

Sorry, to say, but it don't work yet.

First of all, I get the error while compiling, in this line:
-->  PostMessage(hwnd, wm_User, 0, 0);  <--
Error: wm_User is undefined.

Maybe I should have told you before, that the app is an ShellNotifyIcon - application.

Here's some code:

begin
  Application.Initialize;
  Application.ShowMainForm := False;
  Application.CreateForm(TfrmMain, frmMain);
  ShowWindow ( Application.Handle, SW_HIDE );
  Application.Run;
end.

and from the .pas file:

public
  IconData : TNotifyIconData;

procedure TfrmMain.FormCreate(Sender: TObject);
begin
     ShowWindow ( Application.Handle, SW_HIDE );
     IconData.cbSize := SizeOf ( IconData );
     IconData.szTip := 'Click here';
     IconData.Wnd := Handle;
     IconData.uID := 234;
     IconData.hIcon := Application.Icon.Handle;
     IconData.uCallbackMessage := WM_USER + 1;
     IconData.uFlags := NIF_ICON + NIF_MESSAGE + NIF_TIP;
     Shell_NotifyIcon( NIM_ADD, @IconData );
end;

procedure TfrmMain.WndProc ( var Msg: TMessage );
var       pos: TPoint;
begin
     case Msg.Msg of
     WM_User + 1 :
        case Msg.LParam of
             WM_RBUTTONDOWN:
                begin
                     SetForegroundWindow(Application.Handle);
                     Application.ProcessMessages;
                     GetCursorPos ( pos );
                     mnuMain.Popup ( pos.x, pos.y );
                end;
             WM_LBUTTONDOWN:
                begin
                    //Still to do !
                end;
        end;
     end;
     inherited;
end;

The Form is not shown at the start. Later on, it might become visible, if the user clicks on the popupmenu - "Show Form", but if he doesn't, the Form will alway be hidden.

Greetings, Oli

0
 

Expert Comment

by:Metty
ID: 1352015
Sorry! My first code was totally crap (oops)!!! I've found this
somewhere and I planned to use this code too, but I've been totally busy
with the rest of my project. Well today I've implemented the code into my
file and realized that it's totally buggy! Well below is the corrected
and tested version :-)

//Place this in your *.dpr file:
Uses
  WinProcs,
  WinTypes;

Const AppName : 'My Application'; //Place your name here
var PrevInstance : hWnd;
     
begin
  PrevInstance := FindWindow('TApplication', AppName);
  if PrevInstance <> 0 then
    if IsIconic(PrevInstance) then
       ShowWindow(PrevInstance, sw_Restore)
    else
       BringWindowToTop(PrevInstance)
  else
  begin
    Application.Title := AppName;
    Application.CreateForm(TForm1, Form1);
    Application.Run;
  end;
end.

I think this should work with your ShellnotifyIcon-App too
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:Oli2
ID: 1352016
Hi, Metty !
Well, at least this code doesn't bring up any compiling errors. Still the problem is: I can start my app as many times as I want :-(
Is it possible that my overriding of the WndProc is the problem ?
I'll try it.

Greetings, Oli
0
 

Expert Comment

by:Metty
ID: 1352017
Sorry again, but in my project all works fine now.
Perhaps you forgot the line:
"Application.Title := AppName;"

My app doesn't even use "Application.Initialize"!

By the way, what app do you want to program?

Ciao, Metty
0
 
LVL 1

Author Comment

by:Oli2
ID: 1352018
Sorry, it was my fault !!!
It works just fine now !
Since you had quiet some work, I'll adjust the points from 50 to 100, is that okay for you?

Thanks a lot !

Greetings, Oli
0
 
LVL 1

Author Comment

by:Oli2
ID: 1352019
You were right, I forgot Application.Title ... stupid me !

This app is kind of a trigger - I start other parts of the app from it. ( Some are parts of the same dpr, some are own dpr's ) This is why it's pretty important to have it run only once.

Greetings, Oli

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
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…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

867 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

20 Experts available now in Live!

Get 1:1 Help Now