AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Programmierung allgemein Win32/Win64 API (native code) Delphi WM_COPYDATA macht große Probleme
Thema durchsuchen
Ansicht
Themen-Optionen

WM_COPYDATA macht große Probleme

Ein Thema von Mazel · begonnen am 17. Jul 2006 · letzter Beitrag vom 19. Jul 2006
Antwort Antwort
Benutzerbild von Mazel
Mazel

Registriert seit: 11. Nov 2005
Ort: Leipzig
831 Beiträge
 
#1

Re: WM_COPYDATA macht große Probleme

  Alt 17. Jul 2006, 16:12
Hi,

es ist das richtige Handle, denn ich habe beim Start der ersten Anwendung mir eine Message zeigen lassen und als die zweite Anwendung getsartet wurde
ShowMessage(IntToStr(Application.Handle)); und
ShowMessage(IntToStr(Wnd)); beides mal war es gleich, sonst würde er auch nicht mit SetForegroundWindow(Wnd) die richtige Anwendung nach vorne bringen.

Aber hier trotzdem der Code (aus einer OneInst):
Delphi-Quellcode:
unit OneInst;

interface

{ Make a call to this procedure in your project source
  immediately before the first call to CreateForm.
  That should ensure it is after the Application.Title
  assignment that can muck up the logic.

  If you haven't set an application title yet, then
  do so to ensure this works }


uses
 Windows, Messages, SysUtils, Classes, Controls, Forms;

procedure EnsureSingleInstance (MyGUID : string) ;

implementation

uses
  WinTypes, WinProcs, Dialogs, UnitConst, Unit1;

procedure EnsureSingleInstance (MyGUID : string) ;
var
  Wnd: HWnd;
  WndClass, WndText: array[0..255] of char;
  MyCopyDataStruct: TCopyDataStruct;
begin
{$ifdef Win32} 
  { Try and create a semaphore. If we succeed, then check }
  { if the semaphore was already present. If it was }
  { then a previous instance is floating around. }
  { Note the OS will free the returned semaphore handle }
  { when the app shuts so we can forget about it }
  if (CreateSemaphore(nil, 0, 1,
        PChar(MyGUID)) <> 0) and
     (GetLastError = Error_Already_Exists) then
{$else}
  if HPrevInst <> 0 then
{$endif}
  begin
    Wnd := GetWindow(Application.Handle, gw_HWndFirst);
    while Wnd <> 0 do
    begin
      { Look for the other TApplication window out there }
      if Wnd <> Application.Handle then
      begin
        { Check it's definitely got the same class and caption }
        GetClassName(Wnd, WndClass, Pred(SizeOf(WndClass)));
        GetWindowText(Wnd, WndText, Succ(Length(Form1.Caption)));
        if (StrPas(WndClass) = Application.ClassName) and
           (StrPas(WndText) = Form1.Caption) then
        begin
          { This technique is used by the VCL: post }
          { a message then bring the window to the }
          { top, before the message gets processed }
          PostMessage(Wnd, wm_SysCommand, sc_Restore, 0);
{$ifdef Win32}
           If ParamStr(1) <> 'then with MyCopyDataStruct do
           begin
            dwData:=WM_COMMUNICATE;
            cbData:=Length(PChar(ParamStr(1))) + 1;
            lpData:=PChar(ParamStr(1));
            SendMessage(Wnd, WM_COPYDATA, Application.Handle, Integer(@MyCopyDataStruct));
           end;

          SetForegroundWindow(Wnd);
{$else} 
          BringWindowToTop(Wnd);
{$endif}
          Halt
        end
      end;
      Wnd := GetWindow(Wnd, gw_HWndNext)
    end
  end
end;

end.
Gruß
Mazel
Marcel Jänicke
www.mj-software.net
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 23:59 Uhr.
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz