CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Mar 2004
    Location
    Kennesaw, GA, USA
    Posts
    39

    Question How do I make "Open With" work in MyApp?

    I've deleted Open from my file menu and placed my own Open BMP instead and it works fine in my MFC CScrollView SDI app. My OpenBMP function gets the filename from the user; then in OnDraw, I load and display the bitmap.

    However, "Open With" (on the context menu) does not work, so I used the bitmap filename found in the command line argument (m_lpCmdLine) and tried to open the file the same way. ((HBITMAP)::LoadImage) returns NULL and concurrently a Windows error message pops up saying windows is unable to find the file. The error message includes the full path to the file including the filename and it is correct. Also, the filename I'm passing to LoadImage is correct. The same LoadImage code is used successfully when opening a file from my Open BMP function. The bitmap file I'm trying to open is on a local drive.

    Any advice? Is there a better way to do this?

    Regards,
    Lowell.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: How do I make "Open With" work in MyApp?

    Show your code.
    Victor Nijegorodov

  3. #3
    Join Date
    Mar 2004
    Location
    Kennesaw, GA, USA
    Posts
    39

    Re: How do I make "Open With" work in MyApp?

    The applicable code is shown below. The first AfxMessageBox in LoadBMPFile displays the correct path and filename. However, the LoadImage returns a NULL.

    In MyApp::InitInstance,
    Code:
    m_sCmdLineFileName.Format("%s",m_lpCmdLine);
    In MyApp.h
    Code:
    CString m_sCmdLineFileName;
    In MyAppView::OnDraw,
    Code:
    CDC bmDC;
     
    if(theApp.m_sCmdLineFileName!=""){
        sBMPFileName = theApp.m_sCmdLineFileName;    //View Member CString
        bLoadBitmap = true;                          //View Member Bool
    }
    bmDC.CreateCompatibleDC(pDC);
    if(bLoadBitmap) LoadBMPFile();
    In MyAppView::LoadBMPFile,
    Code:
    void CChartDataDigitizerView::LoadBMPFile()
    {
        //loads the bitmap into memory
        //display is handled in OnDraw
    
        bmp.Detach(); 
        if(sBMPFileName != ""){ 
            AfxMessageBox(sBMPFileName);
            hBmp = (HBITMAP)::LoadImage(
                    NULL,
                    sBMPFileName,
                    IMAGE_BITMAP,
                    0,
                    0,
                    LR_LOADFROMFILE|LR_CREATEDIBSECTION
                    );
            if(hBmp != NULL){
                bmp.Attach(hBmp);
            }
            else{
                AfxMessageBox("Unable to LoadImage");
            }
        }
    }
    In MyAppView.h,
    Code:
        HBITMAP hBmp;
        CBitmap bmp;
    Regards,
    Lowell

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: How do I make "Open With" work in MyApp?

    Quote Originally Posted by lspecht View Post
    The applicable code is shown below. The first AfxMessageBox in LoadBMPFile displays the correct path and filename. However, the LoadImage returns a NULL.
    in this case you should call GetLastError to obtain the extended error information.

    Quote Originally Posted by lspecht View Post
    ...
    In MyAppView::LoadBMPFile,
    Code:
    void CChartDataDigitizerView::LoadBMPFile()
    {
        //loads the bitmap into memory
        //display is handled in OnDraw
    
        bmp.Detach(); 
    ....
    you must call DeleteObject rather than Detach to prevent resource leaking

    Besides, did you try to debug your App to be sure all variables ant file path/names are correct?
    Victor Nijegorodov

  5. #5
    Join Date
    Mar 2004
    Location
    Kennesaw, GA, USA
    Posts
    39

    Re: How do I make "Open With" work in MyApp?

    Thanks for the tip on DeleteObject.

    I did as you suggested and used GetLastError. The error is,

    "LoadBMPFile failed with error 2: The system cannot find the file specified."

    I have not figured out how to invoke the debugger when doing an "Open With" from the context menu. Perhaps you can tell me how to do that? I can attach to the process and debug after it tries to bitblt the bitmap that didn't get loaded and Asserts but that doesn't help me since the LoadImage has already executed. That is why I used the AfxMessageBox to display the file name.

    I've verified that the file "c:\bees.bmp" exists and has read permission for Everyone.

    Any other suggestions?
    Regards,
    Lowell

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: How do I make "Open With" work in MyApp?

    Quote Originally Posted by lspecht View Post
    I have not figured out how to invoke the debugger when doing an "Open With" from the context menu.
    Set a break point (F9) at the begin of LoadBMPFile
    Start debugging (F5)
    After you will come to the break point in the beginning of LoadBMPFile use F10 to go to the next line(s) ans see what happens with all your variables.
    Victor Nijegorodov

  7. #7
    Join Date
    Mar 2004
    Location
    Kennesaw, GA, USA
    Posts
    39

    Re: How do I make "Open With" work in MyApp?

    I'm confused. If I start the debugger using F5, the filename from the command line,

    theApp.m_sCmdLineFileName,

    will be blank and LoadBMPFile will never get called unless I open the file from the File menu. The problem I'm having only occurs if I right-click on a file in Windows Explorer and select "Open With" MyApp.

    The code works fine for the case where I start the debugger and open the file from the File menu using my Open BMP function.
    Regards,
    Lowell

  8. #8
    Join Date
    Mar 2004
    Location
    Kennesaw, GA, USA
    Posts
    39

    Re: How do I make "Open With" work in MyApp? Problem Solved!

    Thanks Victor for all your help and time on this issue. I finally saw what my message boxes were showing me all along and that was the double quotes around the filename coming in off the command line. I was passing

    "c:\bees.bmp"

    to LoadImage instead of

    c:\bees.bmp

    without the quotes. First time I've used the command line in an MFC app and didn't realize what was going on.

    I would still like to know how to invoke the debugger when doing an Open With or simply double clicking on a file name to launch the app.
    Regards,
    Lowell

  9. #9
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: How do I make "Open With" work in MyApp?

    Interesting problem.
    • Are you sure proper permissions are set for the file?
    • What is the complete command line argument? Does it involve any DDE related arguments?
    • Anything related to Unicode/non-Unicode stuff that is creeping in.
    • Open the file directly, instead of LoadImage. I mean CreateFile, CFile or things like that, to make sure file is openable.
    • Does file-name comes as fully-qualified path, or as relative path?
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  10. #10
    Join Date
    Mar 2004
    Location
    Kennesaw, GA, USA
    Posts
    39

    Re: How do I make "Open With" work in MyApp?

    Thanks for responding Ajay. Our pathes crossed. See my previous post.
    Regards,
    Lowell

  11. #11
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: How do I make "Open With" work in MyApp?

    I would still like to know how to invoke the debugger when doing an Open With or simply double clicking on a file name to launch the app.
    Put DebugBreak(); in application! Probably in InitInstance.
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  12. #12
    Join Date
    Mar 2004
    Location
    Kennesaw, GA, USA
    Posts
    39

    Re: How do I make "Open With" work in MyApp?

    DebugBreak(); works in that it allows me to attach the debugger however, when I do, it is launching my Compaq Visual Fortran Debugger. How do I get it to launch my .Net Debugger? Is there a precedence list for debuggers kept somewhere?
    Regards,
    Lowell

  13. #13
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: How do I make "Open With" work in MyApp?

    Try: Tools -> Options -> Debugging -> Just-in-time
    • Disable the 'Native' debugging (or others also, if you need).
    • Confirm the operation.
    • Optionally, restart VS.
    • Now Re-enable the debugging.
    Hope this works.

    You can also check:
    http://support.microsoft.com/default...b;en-us;103861
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  14. #14
    Join Date
    Mar 2004
    Location
    Kennesaw, GA, USA
    Posts
    39

    Re: How do I make "Open With" work in MyApp?

    Thanks, Ajay! That worked. Our work here is done.
    Regards,
    Lowell

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured