Click here to Skip to main content
15,896,606 members

How to drop files in an external windows app?

F.moghaddampoor asked:

Open original thread
Well I have an external app, which I can Identify its windows handles by spying or searching their class names. One of this windows is a tree type windows, showing folders and files inside the window. I am wondering how I can send drop message to the specified window to simulate drag drop of several files.

So I have searched the internet, and found some information, I have interest to do this in c#:

I found this info:

WM_DROPFILES message: Sent when the user drops a file on the window of an application that has registered itself as a recipient of dropped files.

C#
PostMessage(

    (HWND) hWndControl,   // handle to destination control

    (UINT) WM_DROPFILES,  // message ID

    (WPARAM) wParam,      // = (WPARAM) (HDROP) hDrop;

    (LPARAM) lParam       // = 0; not used, must be zero 

);


So on what I know: hDrop: A handle to an internal structure describing the dropped files.

1) And the function return should be zero on success. 2) The HDROP is a handle to an internal structure describing the dropped files which is declared in Shellapi.h. 3) WM_DROPFILES number is 0x233 4) lParam: Must be zero.

So the code should be something like this:
private const UInt32 WM_DROPFILES = 0x0233;

PostMessage( SomeHWND, WM_DROPFILES, wParam, intPtr.Zero );


Anyone knows how to fill wparam in this case?

What I have tried:

C++

// DragAndDrop.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <windows.h>
#include <shlobj.h>
#include <tchar.h>


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

for (int i = 0; i <= WM_DROPFILES; i++)
{
ChangeWindowMessageFilter (i, MSGFLT_ADD);
}

if (HWND hwnd = FindWindow ("OpusApp", NULL)) {

//HGLOBAL hGlobal = GlobalAlloc (GMEM_FIXED,
//sizeof ("d:\\DragMe.txt") + 2);
//char *strFile = (char*) GlobalLock
//(hGlobal);
//strcpy (strFile, "d:\\DragMe.txt");
//strFile [strlen ("d:\\DragMe.txt") +
//1] = NULL;
char filename[] = "d:\\DragMe.txt";

POINT point;
point.x = 480;
point.y = 480;

HGLOBAL hMem = GlobalAlloc(GHND, sizeof(DROPFILES) + strlen(filename)+2);

DROPFILES *dfiles = (DROPFILES*) GlobalLock(hMem);
if (!dfiles)
{
GlobalFree(hMem);
return NULL;
}

dfiles->pFiles = sizeof(DROPFILES);
dfiles->pt = point;
dfiles->fNC = TRUE;
dfiles->fWide = FALSE;
memcpy(&dfiles[1], filename, strlen(filename));
GlobalUnlock(hMem);

printf ("Sending Message...\n");

if (!PostMessage(hwnd, WM_DROPFILES, (WPARAM)hMem, 0)) {
printf("Error Posting Message!");
GlobalFree(hMem);
}
}

int temp = 0;
scanf("&d", temp);
return 0;
}
Tags: C#, WinAPI

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900