Solved

Let the app only run once

Posted on 1998-12-19
8
211 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

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
 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

778 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