🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Continuing problem with SendMessage and WM_CCOPYDATA

#4270
From: Remy Lebeau (TeamB)
Date: Mon, 27 Feb 2006 21:11
140 lines
4766 bytes
"Nate Lockwood" <RNLockwoodREMOVE@attglobal.net> wrote in message
news:44021981$1@newsgroups.borland.com...

> Here is some code

Your code is still incorrect for several reasons:

1) you are accessing a NULL TStringList pointer when constructing the
message to be sent

2) you are never checking the WM_MYMSG global for 0 before using it, in case
RegisterWindowMessage() fails

3) you are still not freeing the mutex properly.

4) you are still catching the message in the completely wrong place (I
explained that to you earlier).


Use this code instead:

    UINT WM_MYMSG = 0;

    HANDLE CheckForInstance(const AnsiString &sTitle, TStrings *FileList)
    {
        ::SetLastError(0);

        HANDLE hMutex ::CreateMutex(NULL, TRUE,
"USDA.FS.FireScience.GooleEarthTool.mutex");
        if( hMutex != NULL ) // exists or created
        {
            if( ::GetLastError() != ERROR_ALREADY_EXISTS )
                return hMutex;

            if( (WM_MYMSG != 0) && (FileList != NULL) )
            {
                HWND hReceiver  = ::FindWindow("TApplication",
sTitle.c_str());
                if( hReceiver != NULL )
                {
                    AnsiString sBuf = FileList->Text;

                    COPYDATASTRUCT cds = {0};
                    cds.dwData = WM_MYMSG;
                    cds.cbData = sBuf.Length();
                    cds.lpData = pBuf.c_str();

                    ::SendMessage(hReceiver, WM_COPYDATA,
reinterpret_cast<WPARAM>(Application->Handle),
reinterpret_cast<LPARAM>(&cds));
                }
            }

            ::CloseHandle(hMutex);
        }

        return NULL;
    }

    WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
    {
        const AnsiString sTitle = "Google Earth (C) Tool";
        WM_MYMSG =
RegisterWindowMessage("USDA.FS.FireScience.GooleEarthTool.Awake");

        TStringList * FileList = NULL;
        try
        {
            FileList = new TStringList;
            // fill FileList as needed ...

            HANDLE mutex = CheckForInstance(sTitle, FileList);
            if( mutex != NULL )
            {
                Application->Title = """;
                Application->Title = sTitle;
                Application->Initialize();
                Application->CreateForm(__classid(TFormMain), &FormMain);
                Application->CreateForm(__classid(TAboutBox), &AboutBox);
                Application->Run();
            }
        }
        catch (Exception &exception)
        {
            Application->ShowException(&exception);
        }
        catch (...)
        {
            try
            {
                throw Exception("");
            }
            catch (Exception &exception)
            {
                Application->ShowException(&exception);
            }
        }

        if (mutex != NULL)
            ::CloseHandle(mutex);

        return 0;
    }


    extern UINT WM_MYMSG;

    __TFormMain::TFormMain(TComponent *Owner)
        : TForm(Owner)
    {
        Application->HookMainWindow(AppMessageHook);
    }

    __TFormMain::~TFormMain()
    {
        Application->UnhookMainWindow(AppMessageHook);
    }

    bool __fastcall TFormMain::AppMessageHook(TMessage &Message)
    {
        if( Message.Msg == WM_COPYDATA )
        {
            LPCOPYDATASTRUCT lpcds =
reinterpret_cast<LPCOPYDATASTRUCT>(Message.LParam);
            if( (lpcds->dwData == WM_MYMSG) && (WM_MYMSG != 0) )
            {
                AnsiString sData(static_cast<char*>(lpcds->lpData),
lpcds->cbData);
                // use sData as needed ...
                Application->Restore();
                Show();
                return true;
            }
        }
        return false;
    }


Gambit
Technical Details
Message-ID: <44036ab4$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news3.google.com!news2.volia.net!solnet.ch!solnet.ch!newsfeed.inode.at!reader.uucp.at!feeder02.uucp.at!newsfeed.uucp.at!news.uucp.at!newsgroups.borland.com!not-for-mail
References: <44021981$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,6afbc9bf5041d787
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <44021981$1@newsgroups.borland.com>
Subject: Re: Continuing problem with SendMessage and WM_CCOPYDATA
Date: Mon, 27 Feb 2006 13:11:00 -0800
Lines: 142
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.69.17.41
Message-ID: <44036ab4$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1141074612 68.69.17.41 (27 Feb 2006 13:10:12 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news3.google.com!news2.volia.net!solnet.ch!solnet.ch!newsfeed.inode.at!reader.uucp.at!feeder02.uucp.at!newsfeed.uucp.at!news.uucp.at!newsgroups.borland.com!not-for-mail