getting location of a file

Started by
8 comments, last by shuma-gorath 17 years, 9 months ago
hello all. i want that if someone is dragging somthing into my program i will have its location. example: someone went into my computer-->c-->he drag a.exe to my program then a veriable named t will be t = "c:/a.exe" and idea how i do this? thanks in advance
Advertisement
If someone drags something onto your .exe, then the full path to the item dropped is given on the command line (argv[1] in a console app, lpszCmdLine in a Win32 app).
In most windowing environments, including Windows, the name(s) of the dragged file(s) will be passed as command-line parameters to your program. In C and C++ you use the arguments (typically named argv and argc) of the main() function to get at them.

~phil
~phil
Moved to General Programming.
This depends on what you want exactly. If you want to drag a file onto the icon that represents your program, then your program will start, and the filename will be passed in as the others mentioned.

If on the other hand you want to have your program running and want to be able to drag and drop file onto the window (for a modelviewer, or a texteditor for example) check out the tutorials here. They are pretty straightforward, and I was able to implement drap-and-drop very easily with them.
Quote:Original post by rick_appleton
This depends on what you want exactly. If you want to drag a file onto the icon that represents your program, then your program will start, and the filename will be passed in as the others mentioned.

If on the other hand you want to have your program running and want to be able to drag and drop file onto the window (for a modelviewer, or a texteditor for example) check out the tutorials here. They are pretty straightforward, and I was able to implement drap-and-drop very easily with them.


i want while my program is running and its on a loop (so it wont get closed...)
to drag a File to my program windows window.
like.. i got a.exe ... i'll drag it to my window and i'll have the location of it.. (for ex: c:/a/a.exe).
i didnt understand from the tutorial you gave me... and i downloaded the microsoft examples... are you sure this is what i ment?
do you have any Ez example with not much lines like the one you gave me?
i only want to have its location and nothing else...
thanks in advance
i dont think you guys understaned me.
i dont want just a drag and drop...
I want to drag files from DESKTOP
to my program and not somthing else
Be sure that your main window is created with CreateWindowEx(), and that it is created with the WS_EX_ACCEPTFILES extended style.

For your users' sake, it may be a good idea to add support for shortcuts, as this is the sort of functionality they'd expect in a program...for the most part.

In the switch block of your WndProc:

case WM_DROPFILES:    /*Attain name of file dragged onto window; it can be an exe or shortcut file*/    char path[MAX_PATH];    //Path of file    DragQueryFile((HDROP)wparam,0,path,MAX_PATH);   //Attain the path    DragFinish((HDROP)wparam);  //Complete drag-n-drop process    /*This next block resolves the shortcut (i.e. it retrieves the target of the shortcut)*/    //The selected file is a LNK file that the program might be able to use    if(strncmp(&path[strlen(path)-8],".exe.lnk",8)==0)    {        CoInitialize(NULL); //Just know that this call is needed        /*Create an instance of Interface IShellLink, the one that handles shortcuts*/        IShellLink *shortcut;        CoCreateInstance(CLSID_ShellLink,NULL,CLSCTX_INPROC_SERVER,IID_IShellLink,(void**)&shortcut);        /*Create an instance of the interface for working w/ the LNK file*/        IPersistFile *file;        shortcut->QueryInterface(IID_IPersistFile,(void**)&file);        //Create wide-character string; "wide_string" will store the result*/        WCHAR wide_string[MAX_PATH];        MultiByteToWideChar(CP_ACP,0,path,-1,wide_string,MAX_PATH);        file->Load(wide_string,STGM_READ);  //Load the LNK file        char resolved_path[MAX_PATH];   //Buffer to hold path of file referenced by shortcut        shortcut->GetPath(resolved_path,MAX_PATH,NULL,SLGP_RAWPATH);    //Resolve the shortcut        file->Release();    //Close the LNK file and delete the instance of the IPersistFile interface        strcpy(path,resolved_path); //Replace the LNK file path with that of the target file        shortcut->Release();    //Delete the instance of the IShellLink interface        CoUninitialize();   //This call is needed    }    /*---Using "path", do your file loading here---*/return 0;


Be sure to link ole32.lib and uuid.lib, and to include shlwapi.h, shellapi.h, shlguid.h, shobjidl.h,objbase.h, and wtypes.h. You may need to slightly alter the inclusion list, but it should go something like that. Now, if you don't want LNK file support, simply omit the if block. You may also omit the file inclusions and library linkings if this is the case. Also note that this code will not resolve shortcuts whose targets have been moved. To acheive such functionality would require a little bit more code, but not too much.
Quote:Original post by shuma-gorath
Be sure that your main window is created with CreateWindowEx(), and that it is created with the WS_EX_ACCEPTFILES extended style.

For your users' sake, it may be a good idea to add support for shortcuts, as this is the sort of functionality they'd expect in a program...for the most part.

In the switch block of your WndProc:

*** Source Snippet Removed ***

Be sure to link ole32.lib and uuid.lib, and to include shlwapi.h, shellapi.h, shlguid.h, shobjidl.h,objbase.h, and wtypes.h. You may need to slightly alter the inclusion list, but it should go something like that. Now, if you don't want LNK file support, simply omit the if block. You may also omit the file inclusions and library linkings if this is the case. Also note that this code will not resolve shortcuts whose targets have been moved. To acheive such functionality would require a little bit more code, but not too much.



i used ur code and i tried to drag a file to my Window but its not working..
can anyone give me a full working code of this?
(i want to make like they made in winamp ... that u can drag files to the window if u didnt understand me so far)
so i just want the Path of the file that was dragged and thats it.

thanks in advance
Did you actually add file loading as the final comment in the source suggested?

case WM_DROPFILES:    /*Attain name of file dragged onto window*/    char path[MAX_PATH];    //Path of file    DragQueryFile((HDROP)wparam,0,path,MAX_PATH);   //Attain the path    DragFinish((HDROP)wparam);  //Complete drag-n-drop process    /*This is one way to load the file*/    ifstream fin(path);    fin.close();return 0;


The only other thing I can think of that could have gone wrong is that you didn't pass WS_EX_ACCEPTFILES as the first argument of CreateWindowEx(). If you still run into trouble, I would suggest temporarily replacing the file loading segment with a simple MessageBox() call to display the collected file path. Obviously, if the path is blank, we know there is a problem somewhere.

This topic is closed to new replies.

Advertisement