🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #10078

Re: Implementation of run once

#10078
From: Ed Mulroy [TeamB]
Date: Thu, 27 May 2004 11:39
73 lines
3228 bytes
Process32First and Process32Next are Windows Toolhelp functions and
are no more related to VB than to BCB.  They are covered in the help.

There is an ambiguity in what you are asking.  Are you trying to make
sure that only one copy of the program can be active at the same time
or are you trying to make it so that the program can only be run once
on a machine and never again on the same machine?

If only one active at the same time then use a mutex.  It might go
something like this:

-------------------
const char mutex_name[] = ---whatever you want---

int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
  {
  // use a mutex to only run one copy of the program
  HANDLE mutex_hand =
      OpenMutex(MUTEX_ALL_ACCESS, FALSE, mutex_name);

  if (mutex_hand)
    {
    // try to switch focus to the other copy of the program
    HWND other_hwnd =
       FindWindow(MAKEINTRESOURCE(DIALOG), window_caption);

    if (other_hwnd)
      {  // switch to ti
      SetForegroundWindow(other_hwnd);
      }
    }
  else
    {
    mutex_hand = CreateMutex(NULL, TRUE, mutex_name);

    -----do the work of the program------

    if (mutex_hand)
      {
      CloseHandle(mutex_hand);
      }
    }

  return 0;
  }

-------------------

You can use FindWindow, EnumWIndows or
CreateToolhelp32Snapshot/Process32First/Process32Next to search for
the other instance of the program.  If you use the process enumeration
you normally would use EnumWindows to find one of its windows and
switch to it.

However if you want to ban use of the program on a PC because it has
already been run once in the life of the machine then you are on your
own.  There are no facilities to reliably do that in a fashion the
user cannot get around.

.  Ed

> Daniele Borini wrote in message
> news:40b5cc3f@newsgroups.borland.com...
>
> i need to check if my app has been already ran, to deny
> multiple executions on the same PC.
> i've thought to search in the active processes list is there's
> the .exe name of my app; if so restore those process's
> window and exit from the  second app's instance.
> i'd like to know which API lets me to gather infos on
> processes. I know in VB there's an interesting
> Process32Next/Process32First which
> return good infos, but i can't find it in BCB...
Technical Details
Message-ID: <40b5d385@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!controlnews3.google.com!news1.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <40b5cc3f@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10cc67,91ebf45a6fbd8e57
X-Google-Attributes: gid10cc67,public
From: "Ed Mulroy [TeamB]" <dont_email_me@bitbuc.ket>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <40b5cc3f@newsgroups.borland.com>
Subject: Re: Implementation of run once
Date: Thu, 27 May 2004 07:39:26 -0400
Lines: 75
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1409
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409
NNTP-Posting-Host: 12.161.27.84
Message-ID: <40b5d385@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1085657989 12.161.27.84 (27 May 2004 04:39:49 -0700)
X-Authenticated-User: edmulroy
Path: arc1-ca.pugleaf.net!controlnews3.google.com!news1.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail