Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Send HDROP data with WM_DROPFILES message.

674 views
Skip to first unread message

Joseph Scandura

unread,
Oct 20, 2003, 3:04:41 PM10/20/03
to
We assume MS SoundRecorder is running and the file
c:\temp\test.wav exists.

We're trying to send a WM_DROPFILES message to MS
SoundRecorder.
We don't know exactly the format of the structure to send
as an HDROP parameter to the SendMessage (hWndSoundRec,
WM_DROPFILES, (WPARAM) &oMedium, 0L) function.

The message is actually recieved by the Sound Recorder.
However it pops up a message saying:
"File cannot be opened" with random characters (indicating
reading from a non-allocated, or corrupted memory
location) at the end.

We're using the structures STGMEDIUM and DROPFILES as
below.

What are we missing?


int main(int argc, char* argv[])
{
HWND hWndSoundRec = FindWindow ("SoundRec", NULL);

if (hWndSoundRec)
{
HGLOBAL hGlobal = GlobalAlloc (GMEM_FIXED,
sizeof ("k:\\temp\\test.wav") + 2);
char *strFile = (char*) GlobalLock
(hGlobal);
strcpy (strFile, "c:\\temp\\test.wav");
strFile [strlen ("c:\\temp\\test.wav") +
1] = NULL;

POINT point;
point.x = 100;
point.y = 100;

DROPFILES oDropFiles;
oDropFiles.pFiles = (long) &strFile;
oDropFiles.fNC = FALSE;
oDropFiles.fWide = FALSE;
oDropFiles.pt = point;


STGMEDIUM oMedium;
oMedium.tymed = 1;
oMedium.hGlobal = &oDropFiles;

//Send Message with a file reference.
BOOL bSent = SendMessage (hWndSoundRec,
WM_DROPFILES, (WPARAM) &oMedium, 0L);

BOOL bRet = GlobalUnlock (hGlobal);
GlobalFree (hGlobal);
}
return 0;
}

Henk Devos

unread,
Oct 20, 2003, 6:52:34 PM10/20/03
to
wParam should be a HGLOBAL not a STGMEDIUM.
i.e. use the hGlobal you have as the wParam.

Henk Devos
http://www.whirlingdervishes.com


"Joseph Scandura" <cust...@scandura.com> wrote in message
news:03c301c3973d$0433f8e0$a101...@phx.gbl...

> file://Send Message with a file reference.

Jim Barry

unread,
Oct 20, 2003, 7:24:05 PM10/20/03
to
Joseph Scandura wrote:
> We're trying to send a WM_DROPFILES message to MS
> SoundRecorder.
> We don't know exactly the format of the structure to send
> as an HDROP parameter to the SendMessage (hWndSoundRec,
> WM_DROPFILES, (WPARAM) &oMedium, 0L) function.
>
> The message is actually recieved by the Sound Recorder.
> However it pops up a message saying:
> "File cannot be opened" with random characters (indicating
> reading from a non-allocated, or corrupted memory
> location) at the end.

As Henk correctly points out, the WPARAM should be an HGLOBAL containing the DROPFILES structure. Try the following code snippet. It seems to be important to use PostMessage rather than SendMessage, I'm not too sure why that is.

int main(int argc, char* argv[])
{

if (HWND hwnd = FindWindow ("SoundRec", 0))
{
char filename[] = "c:\\temp\\test.wav";
if (HGLOBAL hGlobal =
GlobalAlloc(GHND, sizeof(DROPFILES) + strlen(filename) + 2))
{
DROPFILES * df = static_cast<DROPFILES *>(GlobalLock(hGlobal));
df->pFiles = sizeof(DROPFILES);
strcpy(reinterpret_cast<char *>(df + 1), filename);
GlobalUnlock(hGlobal);
if (!PostMessage(hwnd, WM_DROPFILES, (WPARAM)hGlobal, 0))
GlobalFree(hGlobal);
}
}
return 0;
}

--
Jim Barry, MVP for Windows SDK
"Honor Bound to Defend Freedom" - slogan displayed at the gates of Camp
Delta, Guantanamo Bay, Cuba, where over 600 prisoners are held indefinitely
by the U.S. military, without trial, in defiance of international law.

anon...@discussions.microsoft.com

unread,
Oct 21, 2003, 1:58:09 PM10/21/03
to
Thanks a lot guys, we got it fixed.

ilm

unread,
Oct 21, 2003, 2:06:57 PM10/21/03
to
Jim Barry wrote:
> int main(int argc, char* argv[])
> {
> if (HWND hwnd = FindWindow ("SoundRec", 0))
> {
> char filename[] = "c:\\temp\\test.wav";
> if (HGLOBAL hGlobal =
> GlobalAlloc(GHND, sizeof(DROPFILES) + strlen(filename) + 2))
> {
> DROPFILES * df = static_cast<DROPFILES *>(GlobalLock(hGlobal));
> df->pFiles = sizeof(DROPFILES);
> strcpy(reinterpret_cast<char *>(df + 1), filename);
> GlobalUnlock(hGlobal);
> if (!PostMessage(hwnd, WM_DROPFILES, (WPARAM)hGlobal, 0))
> GlobalFree(hGlobal);
> }
> }
> return 0;
> }
>

Hey, that's extremely interesting. I suppose a similar technique could
be used to "relay" any WM_DROPFILES message my window receives to a
window in another process? Any idea how that would look like?

Simon

Jim Barry

unread,
Oct 21, 2003, 7:32:29 PM10/21/03
to
"ilm" <i...@msnews.account> wrote:
> Hey, that's extremely interesting. I suppose a similar technique could
> be used to "relay" any WM_DROPFILES message my window receives to a
> window in another process? Any idea how that would look like?

How do you mean, other than making sure that DefWindowProc does not get a chance to free the HGLOBAL before it reaches the target?

ilm

unread,
Oct 23, 2003, 8:32:05 AM10/23/03
to
Jim Barry wrote:

> "ilm" <i...@msnews.account> wrote:
>
>>Hey, that's extremely interesting. I suppose a similar technique could
>>be used to "relay" any WM_DROPFILES message my window receives to a
>>window in another process? Any idea how that would look like?
>
>
> How do you mean, other than making sure that DefWindowProc does not get a chance to free the HGLOBAL before it reaches the target?

Well I actually meant making a copy of the entire original message and
all its associated data.

Jim Barry

unread,
Oct 23, 2003, 8:18:47 PM10/23/03
to
"ilm" <i...@msnews.account> wrote:
> Well I actually meant making a copy of the entire original message and
> all its associated data.

There shouldn't be any need to copy the data, the HGLOBAL can just be passed on as-is.

- Jim

Raymond Chen

unread,
Oct 24, 2003, 2:45:25 AM10/24/03
to
No it can't. Separate address spaces, y'know.

"Jim Barry" <j...@mvps.org> wrote in message
news:eNFYlRcm...@tk2msftngp13.phx.gbl...

Henk Devos

unread,
Oct 24, 2003, 4:29:20 AM10/24/03
to
But in the specific case of HDROP, Windows handles this in some way and
copies the data to the other address space.

"Raymond Chen" <http://blogs.gotdotnet.com/raymondc/> wrote in message
news:#F2cmpfm...@TK2MSFTNGP12.phx.gbl...

Paul Baker

unread,
Oct 24, 2003, 8:57:54 AM10/24/03
to
I agree with Jim and Henk. You're thinking too much!

Paul

"Henk Devos" <in...@dontspamme.regxplor.com> wrote in message
news:bnao4l$va$1...@reader08.wxs.nl...

George

unread,
Oct 26, 2003, 1:01:14 AM10/26/03
to
This technique works for a lot applications, but not work for:
Window Media Player, RealOne Player;
Netscape, Internet Explorer;
Window Explorers.

Jim Barry

unread,
Oct 26, 2003, 4:51:46 PM10/26/03
to
Henk Devos wrote:
> But in the specific case of HDROP, Windows handles this in
> some way and copies the data to the other address space.

Yes, exactly. (Well, not for HDROP generally, but for WM_DROPFILES in particular.) DoDragDrop would otherwise have great difficulty in delivering WM_DROPFILES messages to windows with WS_EX_ACCEPTFILES set. I must correct my earlier statement about freeing the handle - the destination process will receive a copy of the HGLOBAL, so the source process should free the original handle (i.e. call DragFinish) after forwarding it.

- Jim

Jim Barry

unread,
Oct 26, 2003, 4:55:33 PM10/26/03
to
"George" <x...@no.com> wrote:
> This technique works for a lot applications, but not work for:
> Window Media Player, RealOne Player;
> Netscape, Internet Explorer;
> Window Explorers.

That will be because these applications do not actually handle the WM_DROPFILES message. Instead, they implement IDropTarget and call RegisterDragDrop. Unfortunately, there is currently no documented method of retrieving the IDropTarget interface associated with a window.

- Jim

0 new messages