My code:
void CWGAccountFilterDlg::OnDropFiles(HDROP hDropInfo)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CDialogEx::OnDropFiles(hDropInfo);
wchar_t lpFilePath[MAX_PATH] = { 0 };
int nCount = DragQueryFile(hDropInfo, -1, NULL, 0);
DragQueryFile(hDropInfo, nCount, lpFilePath, _countof(lpFilePath));
DragFinish(hDropInfo);
GetDlgItem(IDC_EDIT_FILE)->SetWindowText(lpFilePath);
m_FilePath.Format(L"%s", lpFilePath);
}
The 2nd calling of DragQueryFile
returns 0 (this situation is correct) and lpFilePath
(this is not the expect) has no data in it. I'm sure that the nCount
value is valid, no buffer overflow.
The dialog and the CEdit control all set 'accept files' to true. And there is a class named CMyEdit
implement from CEdit
, and processed the OnDropFiles
function.
From documentation for DragQueryFileW:
In the second call to
DragQueryFile
, the second parameter should be between0
andnCount
. Test the value fornCount
to make sure it is greater than zero, and pass zero if you are only interested in the first file.This assumes there is only one file being dropped. If there are more files, then add a loop.