Those works only with GUI (windowed) program.
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <shellapi.h>
#pragma lib "shell32.lib"
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void WM_DropFiles(HWND hWnd, HDROP hDrop);
char szAppName[] = "WinDragDrop";
char szFrameClass[] = "cFrame";
HWND hFrame;
HANDLE hInst;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASS wc;
MSG msg;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC) WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground= (HBRUSH)1;
wc.lpszMenuName = NULL;
wc.lpszClassName= szFrameClass;
if (!RegisterClass(&wc)) return 0;
hInst = hInstance;
hFrame = CreateWindow(szFrameClass, szAppName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInst, NULL);
if(!hFrame) return 0;
ShowWindow(hFrame, nCmdShow);
UpdateWindow(hFrame);
DragAcceptFiles(hFrame, TRUE);
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
DragAcceptFiles(hFrame, FALSE);
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
{
switch(wMsg) {
case WM_DROPFILES:
WM_DropFiles(hWnd, (HDROP)wParam);
return 0;
// case WM_CREATE:
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hWnd, wMsg, wParam, lParam);
}
}
void WM_DropFiles(HWND hWnd, HDROP hDrop)
{
int nCnt, nLen, nIdx;
char szFile[260];
nCnt = DragQueryFile(hDrop, -1, NULL, 0);
for (nIdx = 0; nIdx < nCnt; nIdx++) {
nLen = DragQueryFile(hDrop, nIdx, NULL, 0);
DragQueryFile(hDrop, nIdx, szFile, sizeof(szFile));
}
DragFinish(hDrop);
}