🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.nativeapi
3 messages
3 total messages Started by sez Mon, 19 Jun 2006 00:48
Opening a file from WM_COPYMESSAGE
#3393
Author: sez
Date: Mon, 19 Jun 2006 00:48
65 lines
2637 bytes
I have coded this but what is wrong?

//-------main.cpp--------------------------------------------------------------
void __fastcall TMainForm::WmCopyData(TWMCopyData &Msg)
{
  ShowMessage("test"); // fails
}



//-------main.h--------------------------------------------------------------

private:
        void __fastcall WMDropFiles(TWMDropFiles &Msg); // DragDrop
        void __fastcall WmCopyData(TWMCopyData &Msg);
public:

        BEGIN_MESSAGE_MAP
        MESSAGE_HANDLER(WM_COPYDATA, TWMCopyData, WmCopyData)
        MESSAGE_HANDLER(WM_DROPFILES,TWMDropFiles,WMDropFiles)
        END_MESSAGE_MAP(TForm)

//-------App.cpp--------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

USEFORM("main.cpp", MainForm);

WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR cmdLine, int)
{
// My
HANDLE hSemaphore = ::CreateSemaphore(NULL,1,1, "App.SEMAPHORE");
if(hSemaphore && WaitForSingleObject(hSemaphore, 0) != WAIT_OBJECT_0) {
    CloseHandle(hSemaphore);

         HWND hWnd = ::FindWindow("TApplication", "App 1.0");
         if (::IsIconic(hWnd))     // If it is minimized
         {
            ::ShowWindow(hWnd, SW_SHOWDEFAULT);     // Show it
         }
         ::SetForegroundWindow(hWnd);     // Bring it to the foreground


      if (strlen(cmdLine) != 0) {
        COPYDATASTRUCT cds;
        cds.cbData = strlen(cmdLine) + 1;
        cds.lpData = cmdLine;
        SendMessage(hWnd, WM_COPYDATA, 0, (LPARAM)&cds);
      }

    return 0;
}


 Application->Initialize();
 Application->Title = "App 1.0";
                 Application->CreateForm(__classid(TMainForm), &MainForm);
                 Application->Run();

ReleaseSemaphore(hSemaphore,1,NULL);
CloseHandle(hSemaphore);

 return 0;
} 
Re: Opening a file from WM_COPYMESSAGE
#3394
Author: Remy Lebeau (TeamB)
Date: Mon, 19 Jun 2006 02:33
47 lines
3001 bytes
"sez" <www@sezginserin.com> wrote in message
news:4495f47e@newsgroups.borland.com...

> I have coded this but what is wrong?

You are trying to handle the message in the TMainForm class, but you are
sending the message to the TApplication class instead.  You need to do one
of two different things to make this work correctly:

1) leave your WinMain() code as it currently is, and then have TMainForm
call TApplication::HookMainWindow().  The hook method will then be able to
receive the message.

2) leave your TMainForm code as it currently is, but have your WinMain()
code look for the TMainForm window instead of the TApplication window.

With that said, all VCL applications have a TApplication class, so that is
not a very good window to use for sharing data between instances.  You very
well could end up sending the data to the completely wrong application.  You
should rename your TMainForm class to a more unique name, such as
TMyAppMainForm, and then have WinMain() look for that instead.

> HANDLE hSemaphore = ::CreateSemaphore(NULL,1,1, "App.SEMAPHORE");
> if(hSemaphore && WaitForSingleObject(hSemaphore, 0) != WAIT_OBJECT_0) {
>     CloseHandle(hSemaphore);

Why are you doing a wait on the semaphore?  You should be using
GetLastError() instead to know whether the semaphore already existed from a
previous instance or not.

>         COPYDATASTRUCT cds;
>         cds.cbData = strlen(cmdLine) + 1;
>         cds.lpData = cmdLine;
>         SendMessage(hWnd, WM_COPYDATA, 0, (LPARAM)&cds);

You are not providing any value for the dwData member of the COPYDATASTRUCT
structure.  The VCL uses WM_COPYDATA internally for its own purposes, so you
should always provide your own unique 32-bit value for the dwData member,
such as a value returned by RegisterWindowMessage().  That way, you can
validate that any received WM_COPYDATA message is really your own message
before processing its data.  If it is not your own message, then pass it on
unprocessed to the default handler so that you are not breaking the VCL's
messages.


Gambit
Re: Opening a file from WM_COPYMESSAGE
#3399
Author: sez
Date: Mon, 19 Jun 2006 12:41
2 lines
1042 bytes
The problem has been resolved.
Once again, thank you Remy. 
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads