Pelles C forum

C language => Beginner questions => Topic started by: Tsiku on March 15, 2010, 12:33:45 PM

Title: File location
Post by: Tsiku on March 15, 2010, 12:33:45 PM
We are learning programming in c, and using pelles c for it. I need to make project where i drag file in to pelle c console and it shows its full file path. Can anyone help me get me started with it?
Title: Re: File location
Post by: Tsiku on March 18, 2010, 08:25:46 PM
anyone ?
Title: Re: File location
Post by: TimoVJL on March 18, 2010, 09:56:08 PM
Look these functions:

VOID DragAcceptFiles(
    HWND hWnd,
    BOOL fAccept
);
UINT DragQueryFile(
    HDROP hDrop,
    UINT iFile,
    LPTSTR lpszFile,
    UINT cch
);
VOID DragFinish(
    HDROP hDrop
);

This windows message: WM_DROPFILES

Must it to be console application ?
Title: Re: File location
Post by: Tsiku on March 20, 2010, 11:19:24 AM
Look these functions:

VOID DragAcceptFiles(
    HWND hWnd,
    BOOL fAccept
);
UINT DragQueryFile(
    HDROP hDrop,
    UINT iFile,
    LPTSTR lpszFile,
    UINT cch
);
VOID DragFinish(
    HDROP hDrop
);

This windows message: WM_DROPFILES

Must it to be console application ?


Yes a console application, and it must have 3  .lib files and 3  .dll files .
Title: Re: File location
Post by: Tsiku on March 22, 2010, 11:46:40 AM
how to get started with those example codes ?
Title: Re: File location
Post by: TimoVJL on March 22, 2010, 12:44:34 PM
Those works only with GUI (windowed) program.

Code: [Select]
#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);
}
Title: Re: File location
Post by: Tsiku on March 22, 2010, 03:19:30 PM
But any code for console ?
Title: Re: File location
Post by: Tsiku on March 24, 2010, 07:42:16 PM
Can that program done in pelles c console app ?
Title: Re: File location
Post by: Bitbeisser on March 25, 2010, 03:03:55 AM
Can that program done in pelles c console app ?
As you already got the answer: NO.

Drag&Drop is a GUI operation, there is no such thing in a console...

Ralf
Title: Re: File location
Post by: AlexN on March 25, 2010, 08:16:25 AM
Can that program done in pelles c console app ?
As you already got the answer: NO.

Drag&Drop is a GUI operation, there is no such thing in a console...

Ralf
I am not sure that this is correct, because if you drop a file in a console window you get the file name in the command line.

I think you must try to find the handle of the console window and then use a code like the code from timovjl.
Title: Re: File location
Post by: Stefan Pendl on March 25, 2010, 08:53:12 AM
I think it is similar to handling a simple paste action.

Have you already tried to drag and drop a file on your application window, while waiting for input?
Title: Re: File location
Post by: TimoVJL on March 25, 2010, 09:42:58 AM
perhaps cmd.exe's console window isn't window with normal message loop.

You can get window handle thisway:
HWND hWndCon = GetConsoleWindow();

But i can't get messages from it with PeekMessage();
Title: Re: File location
Post by: DMac on March 25, 2010, 06:24:19 PM

We are learning programming in c, and using pelles c for it. I need to make project where i drag file in to pelle c console and it shows its full file path.

There is a very simple way to do just this if this is all you have to do.
I played around and came up with a one line application in about 5 minutes.

Notice that you can open a command prompt window and whatever file you drag into it causes the prompt to display the file's path.  Since this is the default behavior of the command prompt there is no need to try to duplicate it.

What you want to do is launch the command prompt from your application.  Here is a little snippet to get you started:

Code: [Select]
#include <stdlib.h>

int main(int argc, char *argv[])
{
     return [YOUR ONE LINE OF HOMEWORK CODE HERE];
}

Tip: when you launch cmd.exe use the /k switch followed by cd C:\\ to set the prompt to the root directory.
Title: Re: File location
Post by: Bitbeisser on March 26, 2010, 04:19:55 AM
Can that program done in pelles c console app ?
As you already got the answer: NO.

Drag&Drop is a GUI operation, there is no such thing in a console...

Ralf
I am not sure that this is correct, because if you drop a file in a console window you get the file name in the command line.

I think you must try to find the handle of the console window and then use a code like the code from timovjl.
Well, I am not sure that you get any window handle in a true console program, the only way to accomplish what the OP seems to try and achieve would be via commandline arguments, and I assume that this is what DMac tried to say (I guess)...

Ralf
Title: Re: File location
Post by: DMac on March 26, 2010, 05:23:05 PM
Quote
Well, I am not sure that you get any window handle in a true console program

The console does indeed run inside a window and it is possible to get the handle of that window.

I once launched a windows dialog from the console, then, by artful means, got the consol window's handle.  I then set the dialog I created to be the consol window's parent.

Quote
[T]he only way to accomplish what the OP seems to try and achieve would be via commandline arguments, and I assume that this is what DMac tried to say (I guess)...

I tried to give the OP enough clues to find the solution without giving him the actual solution.  However I guess I was not clear enough.

I found that I could simply launch cmd.exe from my console app just as I would do it manually by Start>Run and typing "cmd.exe /k cd c:\"

What is realy happening is that my console app runs, launches another command prompt, and exits.
The command prompt that I launched remains open and I can drag a file into it and see the path to the file echoed there.

DMac
Title: Re: File location
Post by: Bitbeisser on March 27, 2010, 04:13:21 AM
Quote
Well, I am not sure that you get any window handle in a true console program

The console does indeed run inside a window and it is possible to get the handle of that window.

I once launched a windows dialog from the console, then, by artful means, got the consol window's handle.  I then set the dialog I created to be the consol window's parent.

Quote
[T]he only way to accomplish what the OP seems to try and achieve would be via commandline arguments, and I assume that this is what DMac tried to say (I guess)...

I tried to give the OP enough clues to find the solution without giving him the actual solution.  However I guess I was not clear enough.

I found that I could simply launch cmd.exe from my console app just as I would do it manually by Start>Run and typing "cmd.exe /k cd c:\"

What is realy happening is that my console app runs, launches another command prompt, and exits.
The command prompt that I launched remains open and I can drag a file into it and see the path to the file echoed there.

DMac
Sorry, but I think you really need a bit more detailed as to how the console program is getting the path of the drag&dropped file, all that you describe above is the indeed normal behavior of a windows command prompt (that it echoes the file path) but that doesn't mean at all that a console program running in the "DOS Shell/Command prompt" has any knowledge of that path...  ???

I have the feeling that we are talking about different things here, you seem to be talking of the command/shell process (cmd.exe) itself, not about a program, written in (Pelle's C) that is running on top of this very same command prompt/DOS Shell...

Ralf
Title: Re: File location
Post by: DMac on March 29, 2010, 06:04:31 PM
Quote
I have the feeling that we are talking about different things here, you seem to be talking of the command/shell process (cmd.exe) itself, not about a program, written in (Pelle's C) that is running on top of this very same command prompt/DOS Shell...

Ralf

Correct, the solution I had in mind was to simply launch another instance of the command prompt and exit.  The command prompt, I launched, by default then satisfies the assignment.  That is, to drag a file into the command prompt and have it display the path.  The OP said nothing about the application knowing the path.  Simply displaying the path of a file dragged and dropped in the command prompt window.
Title: Re: File location
Post by: Bitbeisser on March 30, 2010, 06:32:33 PM
Quote
I have the feeling that we are talking about different things here, you seem to be talking of the command/shell process (cmd.exe) itself, not about a program, written in (Pelle's C) that is running on top of this very same command prompt/DOS Shell...

Ralf

Correct, the solution I had in mind was to simply launch another instance of the command prompt and exit.  The command prompt, I launched, by default then satisfies the assignment.  That is, to drag a file into the command prompt and have it display the path.  The OP said nothing about the application knowing the path.  Simply displaying the path of a file dragged and dropped in the command prompt window.
I think that was a bit oversimplified, specially considering that the original question might not have been that clear due to language issues...

Ralf
Title: Re: File location
Post by: Tsiku on March 30, 2010, 06:56:53 PM
ok this is what i have so far

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
   char *cKv = "C:\\Windows\\System32\\cmd.exe /Q /C";
   FILE *fp = _popen(cKv, "r");
   char buf[255];
   int i = 0;
   while (fgets(buf, sizeof(buf), fp)) ;
   char b[255];
   char *a = gets(b);
   printf("1 file %s\n", b);
   a = gets(b);
   printf("2 file %s\n", b);
   system("pause");
   _pclose(fp);
   return 0;
}


decided to use cmd for it, and now can u help me make this programm also so it switches files locations
like about so

New Folder\folder\file1.exe
New Folder(2)\folder\file2.exe

and after switch its so

New Folder\folder\file2.exe
New Folder(2)\folder\file1.exe
Title: Re: File location
Post by: TimoVJL on March 31, 2010, 10:37:06 AM
If this is homework, just this
- make 2 buffers for new path/file and copy old paths to those.
- find last occurrence of '\' and put those into pointers. (strrchr()?)
- strip filenames from new buffers using those pointers.
- copy switched filenames to new buffers using pointers. (strcat()?)
- move those files to new places. (rename()?)