🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Run Once

#9966
From: Todd Brylski
Date: Tue, 01 Jun 2004 14:23
106 lines
4674 bytes
"JD" <nospam@nospam.com> wrote in message news:40bc42cd$1@newsgroups.borland.com...
>
>    I'm not crazy!!
>

No, you are not. I have encountered ALL of the same issues that you have.
I am using BCB5 on Win2k Pro.

Anyways, this is where the problem is:
  With the Application minimized, we do a FindWindow for our MainForm,
we then tell the MainForm to restore, but the Application is minimized and
the VCL is now screwed up and doesn't know what to do, so idunno, it punts,
and ignores the minimize command???. That's the best explanation I can come
up with, but, actually that is where the problem lies. The solution is to
get the first instances 'Application' to restore, not the MainForm.

So, this is what I did to rid the problem:


//----------Application cpp file---------------------------------------------

void ReportLastError()
{
    char* S;
    FormatMessage( (DWORD)FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER,
                   NULL, GetLastError(), 0, (char*)&S, 0, 0 );
    ShowMessage( S );
    LocalFree( (HLOCAL)S );
}
//---------------------------------------------------------------------------

WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR CmdLine, int)
{
    extern char WinClassName[];

    HANDLE hMutex = ::CreateMutex( 0, true, WinClassName );
    DWORD LastError = ::GetLastError();
    if( LastError == ERROR_ALREADY_EXISTS )
    {
        HWND hPrevApp = ::FindWindow( WinClassName, NULL );

        if( hPrevApp == NULL )
        {
            ReportLastError();
            return 0;
        }

        // I need to send the command line over but you could just send
        // your own message and achieve the same result

        COPYDATASTRUCT Data;
        Data.dwData = 0x55AA55AA; //this value currently has no meaning in the app
        Data.cbData = strlen( CmdLine )+1;
        Data.lpData = CmdLine;

        ::SendMessage( hPrevApp, WM_COPYDATA, (WPARAM)0, (LPARAM)&Data );
    }
    else if( LastError == 0 )
    {
        try
        {
            Application->Initialize();
            Application->Title = "Uncle Toddy's MP3 Player";
            Application->CreateForm(__classid(TMainForm), &MainForm);
            Application->Run();
        }
        catch (Exception &exception)
        {
             Application->ShowException(&exception);
        }

        ::ReleaseMutex( hMutex );
    }
    return 0;
}
//--------------------------------------------------------------------------



//----- MainForm cpp file ---------------------------------------------------

// use a GUID
char WinClassName[] = "419802D0-D812-4053-BF6B-C3185B73A2C7";

//---------------------------------------------------------------------------

void __fastcall TMainForm::CreateParams(TCreateParams& Params)
{
    TForm::CreateParams(Params);
    memcpy( Params.WinClassName, WinClassName, sizeof(WinClassName) );
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::WMCopyData(Messages::TWMCopyData& Message)
{
    Application->Restore();        // And here is where
    Application->BringToFront();   //  it all work well

    TStrings* List = new TStringList;
    List->CommaText = (char*)Message.CopyDataStruct->lpData;
    AddFiles( List );
    delete List;
}
//---------------------------------------------------------------------------

Todd
Technical Details
Message-ID: <40bc9150$3@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!controlnews3.google.com!news2.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: <40b9cff4$1@newsgroups.borland.com> <k5ikb0dppip15jep979bhuf7g148fsp3lj@4ax.com> <40ba78cc$1@newsgroups.borland.com> <40ba7ce2$1@newsgroups.borland.com> <40ba7f15$1@newsgroups.borland.com> <40ba9d87$1@newsgroups.borland.com> <40bb3815$1@newsgroups.borland.com> <40bba7d2$1@newsgroups.borland.com> <40bbec83$1@newsgroups.borland.com> <40bc0835$1@newsgroups.borland.com> <40bc42cd$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10cc67,55fa8aeaf57fcad1
X-Google-Attributes: gid10cc67,public
From: "Todd Brylski" <tbrylski@yahoo.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <40b9cff4$1@newsgroups.borland.com> <k5ikb0dppip15jep979bhuf7g148fsp3lj@4ax.com> <40ba78cc$1@newsgroups.borland.com> <40ba7ce2$1@newsgroups.borland.com> <40ba7f15$1@newsgroups.borland.com> <40ba9d87$1@newsgroups.borland.com> <40bb3815$1@newsgroups.borland.com> <40bba7d2$1@newsgroups.borland.com> <40bbec83$1@newsgroups.borland.com> <40bc0835$1@newsgroups.borland.com> <40bc42cd$1@newsgroups.borland.com>
Subject: Re: Run Once
Date: Tue, 1 Jun 2004 09:23:15 -0500
Lines: 108
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: 24.166.145.151
Message-ID: <40bc9150$3@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1086099792 24.166.145.151 (1 Jun 2004 07:23:12 -0700)
Path: arc1-ca.pugleaf.net!controlnews3.google.com!news2.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