Discussion:
DragQueryFile Error C++
(too old to reply)
Allen
2005-07-26 09:10:53 UTC
Permalink
I am trying to capture the filename from a dragged/dropped file into a
list control. I have read every single Tutorial, How-To and other
documentation I can find and I still can't get it to work. The drag
procedure works properly (the application captures it) but the filename
is passed as complete gibberish characters. As a test, I have been
tryingto get the name to display in either a list box or a message box.
I am using Windows XP and Dev-CPP.

Here is the source:

case WM_DROPFILES:
{
HDROP hdrop2;
TCHAR anewname[256];
DragQueryFile(hdrop2,0, anewname,
256);//sizeof(anewname));

SendMessage(hListBox,LB_ADDSTRING,0,(LPARAM)TEXT(anewname));
MessageBox(hwnd,anewname, "Dropped File:", MB_OK );
DragFinish(hdrop2);

}
Alf P. Steinbach
2005-07-26 09:18:26 UTC
Permalink
Post by Allen
I am trying to capture the filename from a dragged/dropped file into a
list control. I have read every single Tutorial, How-To and other
documentation I can find and I still can't get it to work. The drag
procedure works properly (the application captures it) but the filename
is passed as complete gibberish characters. As a test, I have been
tryingto get the name to display in either a list box or a message box.
I am using Windows XP and Dev-CPP.
{
HDROP hdrop2;
TCHAR anewname[256];
DragQueryFile(hdrop2,0, anewname,
256);//sizeof(anewname));
SendMessage(hListBox,LB_ADDSTRING,0,(LPARAM)TEXT(anewname));
MessageBox(hwnd,anewname, "Dropped File:", MB_OK );
DragFinish(hdrop2);
}
Sorry I sent you here from [clc++], it seems you just have an uninitialized
variable, namely hdrop2.

When you got that in order there may be other problems, e.g. Unicode versus
ANSI, but first fix that initialization.

I haven't checked whether the rest of the code is meaningful, been a long
time since I did API-level drag-drop.

Cheers,

- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Allen
2005-07-26 09:32:47 UTC
Permalink
I thought the HDROP hdrop2; was all I needed to initialize (with ole2.h
included for oleinitialization). Since the characters all come out
wrong, maybe it is ANSI/Unicode. Any suggestions for further research
resources?

Thanks.
Stefan Vetsch
2005-07-26 09:46:36 UTC
Permalink
Hi Allen

you haven't initialized hdrop2 with the correct drop-handle. the drop
handle comes from windows and you can get it via the wParam value of
your window procedure.

since i have no life and plenty of time here some code that should work
(i haven't tested it :-\):

// this constant is used to determine the number of files droped
const UINT DragQueryCountIndex = 0xFFFFFFFF;

case WM_DROPFILES:
{
// wParam parameter contains the HDROP [1]
HDROP hdrop2 = (HDROP)wParam;

// use MAX_PATH instead of the constant 255
TCHAR anewname[MAX_PATH];

// DragQueryFile with the index 0xFFFFFFFF returns the number of
// files dropped [2]
UINT fileCount = DragQueryFile(hdrop2, DragQueryCountIndex, NULL, 0);

for(UINT i = 0; i < fileCount; ++i)
{
if(DragQueryFile(hdrop2, i, anewname, MAX_PATH) > 0)
{
// the TEXT() macro here was useless, beceause its only
// for constant strings like "bla" or 'b'
SendMessage(hListBox,LB_ADDSTRING,0,(LPARAM)anewname);

// here it's okay to use the TEXT macro
MessageBox(hwnd,anewname, TEXT("Dropped File:"), MB_OK );
}
}

// Free the resources [3]
DragFinish(hdrop2);
}


[1]
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/reference/messages/wm_dropfiles.asp
[2]
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/reference/functions/dragqueryfile.asp
[3]
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/reference/functions/dragfinish.asp

cheers
stefan
Allen
2005-07-26 10:09:44 UTC
Permalink
IT WORKS!!!! IT WORKS!!!!

I must have tried every combination of what did not work. I tried it
all (except what works) and you made it work.

I am extrememly grateful.

Allen
Post by Stefan Vetsch
Hi Allen
you haven't initialized hdrop2 with the correct drop-handle. the drop
handle comes from windows and you can get it via the wParam value of
your window procedure.
since i have no life and plenty of time here some code that should work
// this constant is used to determine the number of files droped
const UINT DragQueryCountIndex = 0xFFFFFFFF;
{
// wParam parameter contains the HDROP [1]
HDROP hdrop2 = (HDROP)wParam;
// use MAX_PATH instead of the constant 255
TCHAR anewname[MAX_PATH];
// DragQueryFile with the index 0xFFFFFFFF returns the number of
// files dropped [2]
UINT fileCount = DragQueryFile(hdrop2, DragQueryCountIndex, NULL, 0);
for(UINT i = 0; i < fileCount; ++i)
{
if(DragQueryFile(hdrop2, i, anewname, MAX_PATH) > 0)
{
// the TEXT() macro here was useless, beceause its only
// for constant strings like "bla" or 'b'
SendMessage(hListBox,LB_ADDSTRING,0,(LPARAM)anewname);
// here it's okay to use the TEXT macro
MessageBox(hwnd,anewname, TEXT("Dropped File:"), MB_OK );
}
}
// Free the resources [3]
DragFinish(hdrop2);
}
[1]
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/reference/messages/wm_dropfiles.asp
[2]
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/reference/functions/dragqueryfile.asp
[3]
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/reference/functions/dragfinish.asp
cheers
stefan
Loading...