----- Original Message -----
From: Ross Levis
To: Multiple recipients of list delphi
Sent: Saturday, July 26, 2003 8:53 AM
Subject: [DUG]: windows class name

>I want to tell from a D5 app if another D5 app is currently running.  Is
the class name the same as >the main forms Name or is this set somewhere
else?
>
>Thanks,
>Ross.

You can modify this example, which doesn't allow multiple copies of your
program to run:

// Making only one instance of the application
// and sending ParamStr parameters from second instance to the first
// before closing the second instance.


// This is declaration of main form of the project.
// The name of form class must be unique. "TForm1" or "TfrmMain" is
// not very apropriate because you will find the form by it's class name.

interface

type
  TfrmYourApplicationMainForm = class(TForm)
    MainMenu1: TMainMenu;
    File1: TMenuItem;
//    .........
//    .... etc...
    procedure FormShow(Sender: TObject);    // OnShow event.
  private
    { Private declarations }
//    .........
//    .... etc...

    // Insert this in private section.
    procedure WMCopyData(var msg:TWMCopyData); message WM_COPYDATA;
  public
    { Public declarations }
//    .........
//    .... etc...
  end;


implementation


procedure TfrmYourApplicationMainForm.WMCopyData(var msg: TWMCopyData);
var i,imax:integer;
    s:string;
begin
  // Here I am using 2 string manipulating functions from
  // unit StrUtils of the RxLib library:
  // WordCount and ExtractWord.

  // If you don't use this excelent library, you may use the source of this
  // functions from the end of this file.

  s := PChar(msg.CopyDataStruct.lpData);

  imax := WordCount(s,[#$0d]);

  Application.BringToFront;

  for i:= 1 to imax do
    // You can use some your function here...
   OpenNewWindow(ShortToLongPath(ExtractWord(i,s,[#$0d])));
end;



// This is processing of the parameters when the first instance opens.
// Assign to the OnShow event of the main form...

procedure TfrmYourApplicationMainForm.FormShow(Sender: TObject);
var i:integer;
begin
  for i:=1 to ParamCount do
    // You can use some your function here...
    OpenNewWindow(ShortToLongPath(ParamStr(i)));
end;


end.





// The following text is from .dpr file of the application:

uses
  Windows, messages;


var handle: THandle;
    cds: TCopyDataStruct;
    fMutex: THandle;
    s:string;
    i:integer;
begin
  // This is check for opened instance of the application.
  // The mutex name must be specific for this application.

  fMutex := CreateMutex(nil,false,'YourMutexName');     // <-- change mutex
name.
  if ( GetLastError() = ERROR_ALREADY_EXISTS ) or
     ( WaitForSingleObject(fMutex,100) = WAIT_TIMEOUT ) or
     ( fMutex = 0 ) then
    begin
      // Another instance opened;
      // Find main window handle...
      handle := FindWindow('TfrmYourApplicationMainForm',nil); // <-- Use
the class name of your main form.

      // If found send ParamStr.
      if handle <> 0 then
        begin
          // Making one string from all Params.
          s := '';
          for i:= 1 to ParamCount do
            s:=s+ParamStr(i)+#$0d;

          with cds do
            begin
              dwData := 0;
              cbData := length(s);
              lpData := PChar(s);
            end;
          // Sending parameters to the other instance of the application.
          SendMessage(handle,WM_COPYDATA,Application.Handle,integer(@cds));
          exit;
        end;
    end;

// This is Delphi generated source...
  Application.Initialize;
  Application.HelpFile := '';
  Application.CreateForm(TfrmYourApplicationMainForm,
frmYourApplicationMainForm);
  Application.Run;
end.





// OPTIONAL... ;-)


// Here is 3 functions from unit StrUtils of RxLib library.
// if you don't use this library, you must include this functions
// somewhere in your source.
// If you have installed RxLib, simply include "uses StrUtils" in your
// main form of the project.

type
  TSysCharSet = set of Char;


function WordCount(const S: string; const WordDelims: TSysCharSet): Integer;
var
  SLen, I: Cardinal;
begin
  Result := 0;
  I := 1;
  SLen := Length(S);
  while I <= SLen do begin
    while (I <= SLen) and (S[I] in WordDelims) do Inc(I);
    if I <= SLen then Inc(Result);
    while (I <= SLen) and not(S[I] in WordDelims) do Inc(I);
  end;
end;


function WordPosition(const N: Integer; const S: string;
  const WordDelims: TCharSet): Integer;
var
  Count, I: Integer;
begin
  Count := 0;
  I := 1;
  Result := 0;
  while (I <= Length(S)) and (Count <> N) do begin
    { skip over delimiters }
    while (I <= Length(S)) and (S[I] in WordDelims) do Inc(I);
    { if we're not beyond end of S, we're at the start of a word }
    if I <= Length(S) then Inc(Count);
    { if not finished, find the end of the current word }
    if Count <> N then
      while (I <= Length(S)) and not (S[I] in WordDelims) do Inc(I)
    else Result := I;
  end;
end;



function ExtractWord(N: Integer; const S: string; const WordDelims:
TSysCharSet): string;
var
  I: Integer;
  Len: Integer;
begin
  Len := 0;
  I := WordPosition(N, S, WordDelims);
  if I <> 0 then
    { find the end of the current word }
    while (I <= Length(S)) and not(S[I] in WordDelims) do begin
      { add the I'th character to result }
      Inc(Len);
      SetLength(Result, Len);
      Result[Len] := S[I];
      Inc(I);
    end;
  SetLength(Result, Len);
end;

Cheers,
Nicholas Sherlock


---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz
To UnSub, send email to: [EMAIL PROTECTED] 
with body of "unsubscribe delphi"
Web Archive at: http://www.mail-archive.com/delphi%40delphi.org.nz/

Reply via email to