Thread: I concede!!!

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    39

    I concede!!!

    I just can't figure this one out. Here is the problem:

    I have associated .txt files with my text editing program, but, of course, when I click on a .txt file it launches my application but does not load the text file. How does Windows communicate to an application, like my text editor, the path information associated with the .txt file clicked by the user? (The path is all the information I need to load the file.)

    If anyone can point me to a resource that describes this process, or offer a suggestion, that would be great.

    Thanks

    Echidna

  2. #2
    Former Member
    Join Date
    Oct 2001
    Posts
    955
    on WinMain declaration you can see

    int WINAPI WinMain(HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,
    int nCmdShow)

    it can also be LONG FAR PASCAL, but I use int WINAPI

    lpCmdLine is a string containing the command line used to call the program, if it's used to open a file, it calls for example

    "c:\\myprogram\\runit.exe d:\\hello\\world.txt"

    I use \\ because it represents a slash

    so you just have to parse the line and get the file that's being opened...

    Oskilian

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    39
    Thanks for responding Oskilian,

    my WinAPI WinMain looks like this:

    int WINAPI WinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst, LPSTR lpszArgs, int nWinMode)

    ...at least that is part of it.

    I still don't get it. I have never come across any reference to command line in WinMain, unless I am missing something.
    I always, of course, define the window class (WNDCLASSEX), create my message loop, etc., but nothing like what you mentioned.

    Could you show me what you meant with more detail.

    Thanks again

    Echidna

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    4
    In your case, the lpszArgs variable hold the command line.

  5. #5
    Unregistered
    Guest
    lpszArgs is the name of the .txt file

    Here is a textreader you can associate to .txt files

    #include <windows.h>

    HINSTANCE hInst;
    HWND hwndMain;
    LPSTR lpszArgs;

    void fileread (HWND window, char * FileName)
    {
    HANDLE file;
    DWORD taille;
    HANDLE hBuffer ;
    LPSTR lpstrBuffer ;
    DWORD dwBytesRead;

    file = CreateFile(
    FileName,
    GENERIC_READ,
    FILE_SHARE_READ,
    NULL,
    OPEN_EXISTING,
    FILE_ATTRIBUTE_NORMAL,
    NULL
    );

    taille = GetFileSize(
    file,
    NULL
    );

    hBuffer = GlobalAlloc (GHND, taille + 1);
    lpstrBuffer = GlobalLock ( ( HGLOBAL)hBuffer) ;

    ReadFile(
    file,
    lpstrBuffer,
    taille,
    &dwBytesRead,
    NULL
    );

    SetWindowText (window, lpstrBuffer) ;
    GlobalUnlock (hBuffer) ;
    GlobalFree (hBuffer) ;
    CloseHandle(file);
    return 0 ;
    }

    LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
    {
    switch (msg) {

    case WM_COMMAND:
    break;
    case WM_DESTROY:
    PostQuitMessage(0);
    break;
    default:
    return DefWindowProc(hwnd,msg,wParam,lParam);
    }

    return 0;
    }

    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, INT nCmdShow)
    {
    HWND text;
    MSG msg;
    hInst = hInstance;
    lpszArgs = lpCmdLine;

    WNDCLASS wc;
    wc.style = CS_HREDRAW|CS_VREDRAW |CS_DBLCLKS ;
    wc.lpfnWndProc = (WNDPROC)WndProc;
    wc.hInstance = hInst;
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszClassName = "editWndClass";
    wc.lpszMenuName = NULL;
    wc.hCursor = LoadCursor(NULL,IDC_ARROW);
    wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);

    if (!RegisterClass(&wc))
    return 0;
    hwndMain = CreateWindow("editWndClass","edit",
    WS_MINIMIZEBOX|WS_VISIBLE|WS_CLIPSIBLINGS|WS_CLIPC HILDREN|WS_MAXIMIZEBOX|WS_CAPTION|WS_BORDER|WS_SYS MENU|WS_THICKFRAME,
    CW_USEDEFAULT,0,CW_USEDEFAULT,0,
    NULL,
    NULL,
    hInst,
    NULL);
    text = CreateWindow( "EDIT", "aa", WS_CHILD|WS_VSCROLL,
    0,0,600,600,
    hwndMain,
    NULL,
    hInst,
    NULL);
    if(lpszArgs!="")
    fileread(text,lpszArgs);
    ShowWindow(text,SW_SHOW);
    ShowWindow(hwndMain,SW_SHOW);
    while (GetMessage(&msg,NULL,0,0)) {

    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
    return msg.wParam;
    }

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    39
    I see that now, LrdChaos. When I had time I conducted a search for " lpCmdLine," I came across several pages that made it clear to me what he meant.

    Prior to yesterday, I never saw WinMain include lpCmdLine or int nCmdShow. Obviously, I haven't seen much.

    Thanks for the starting point Oskilian, and thanks for the code unregistered dude. I will examine it. (I am reading your reply now)

    Echidna

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    39
    Thanks Unregistered, whoever you are. :-)

    All I needed to do was add these lines to WinMain:

    if(lpszArgs!=" ")
    {
    if(LoadFile(hEditCtrl, lpszArgs)) /* I already had a file loading function, thanks */
    {
    SetWindowText(hwnd, lpszArgs);
    /* Had to add the second if statement because, for some reason, the first if statement always ends up TRUE, which would set the window text to nothing, even when no file was being loaded -- like when I launched my application. Hmmm...I'll figure that out later, thanks */
    }
    }

    I Don't understand why you included ShowWindow(text,SW_SHOW); The above lines were all I needed.

    Thanks again to everyone who offered suggestions.

    Echidna

Popular pages Recent additions subscribe to a feed