🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: CommandLine Parameter to own Application...

#9811
From: Remy Lebeau (TeamB)
Date: Tue, 08 Jun 2004 18:21
90 lines
3873 bytes
"Oren Halvani" <NoSpam@fdtd.com> wrote in message
news:40c573b7@newsgroups.borland.com...

> so do i need realy to create mutex at
> all for solving the problem...?

The mutex has nothing to do with sending messages across instances of a
program.  It is merely a helper to detect whether a program is already
running or not.  Using FindWindow() alone will do the same thing, as long as
your window title does not change.  The code you already showed earlier
should work fine, just make the changes I suggested, ie:

    // the VCL uses WM_COPYDATA internally as well, so
    // you don't want your message handler to intercept the VCL's
    // messages by accident.  You should assign your own ID number
    // to any messages you send yourself.  You can use whatever value
    // you want for the ID, just make sure that it is unique...

    #define MY_COPY_DATA_ID    0x00ABCDEF

    void __fastcall TMyApp::WmCopyData(TWMCopyData& Message)
    {
        if( Message.CopyDataStruct->dwData == MY_COPY_DATA_ID )
        {
            AnsiString S =
static_cast<char*>(Message.CopyDataStruct->lpData);
            RichEdit1->Lines->LoadFromFile(S);
        }
        else
            TForm::Dispatch(&Message);
    }

    WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR cmdLine, int)
    {
        HANDLE hMutex = NULL;
        bool bFound = false;

        try
        {
            Application->Initialize();

            HANDLE hMutex = CreateMutex(0, 0, "MyApp");
            if( hMutex )
            {
                if( GetLastError() == ERROR_ALREADY_EXISTS )
                {
                    Application->Title = "";

                    HWND hWnd = FindWindow("TMyApp", "MyApp");
                    if( hWnd )
                    {
                        bFound = true;
                        SetForegroundWindow(hWnd);

                        if( (cmdLine) && (*cmdLine != '\0') )
                        {
                            COPYDATASTRUCT cds = {0};
                            cds.dwData = MY_COPY_DATA_ID;
                            cds.cbData = lstrlen(cmdLine) + 1;
                            cds.lpData = cmdLine;
                            SendMessage(hWnd, WM_COPYDATA, 0,
reinterpret_cast<LPARAM>(&cds));
                        }
                    }
                }

                if( !bFound )
                {
                    Application->CreateForm(__classid(TMyApp), &MyApp);
                    Application->Run();
                }
            }
        }
        catch (Exception &exception)
        {
            Application->ShowException(&exception);
        }

        if( hMutex )
        {
            ReleaseMutex(hMutex);
            CloseHandle(hMutex);
        }

        return 0;
    }


Gambit
Technical Details
Message-ID: <40c604bb$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.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: <40c49e06@newsgroups.borland.com> <40c4b952$1@newsgroups.borland.com> <40c50020@newsgroups.borland.com> <40c51c6c@newsgroups.borland.com> <40c573b7@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10cc67,e90b072bab983bf1
X-Google-Attributes: gid10cc67,public
From: "Remy Lebeau \(TeamB\)" <gambit47.no.spam@no.spam.yahoo.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <40c49e06@newsgroups.borland.com> <40c4b952$1@newsgroups.borland.com> <40c50020@newsgroups.borland.com> <40c51c6c@newsgroups.borland.com> <40c573b7@newsgroups.borland.com>
Subject: Re: CommandLine Parameter to own Application...
Date: Tue, 8 Jun 2004 11:21:56 -0700
Lines: 92
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: 68.169.168.124
Message-ID: <40c604bb$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1086719163 68.169.168.124 (8 Jun 2004 11:26:03 -0700)
Path: arc1-ca.pugleaf.net!g2news1.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