Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Open a file.myext via contxt menu in my application

Open a file.myext via contxt menu in my application

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestionc++comlinux
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    robert_s
    wrote on last edited by
    #1

    Hi. I recently wrote a small MFC app that saves files in my own format .cmp I also managed to associate this file extension with windows so my files have their own cutom icon rather than the default windows one. I learned how to do it from here (cool stuff): http://www.codeproject.com/w2k/extendingshell.asp Now I am facing one problem. When I double-click on any saved file say stored on my desktop then my application opens as it should but it doesnt open my file. Obviously its because when my application initializes it should know the name of the file that I selected to open but I am not sure how to retieve this info. I guess when I right-click on the saved file and select from a context menu "Open this (.cmp) in my app" the shell Command will call my app and somehow pass the name of the file. So basically I am not sure how to read this command line arguments in my MFC app so I could get the name of the file and open it immediately after my app initialization. Actually would it be a command line or some other way of doing it? how do I tell my app what file I am opening.? Please let me know if its very easy solution for it. In that tutorial the author calls: C:\Windows\Notepad.exe %1 but I guess "%1" means nothing to my app as my app would not recognise what that is. I think I should replace %1 with my own command like "open" or "print" to distinguish the action my app should perform. Am I right? Please let me know if you could. Thank you..

    D 1 Reply Last reply
    0
    • R robert_s

      Hi. I recently wrote a small MFC app that saves files in my own format .cmp I also managed to associate this file extension with windows so my files have their own cutom icon rather than the default windows one. I learned how to do it from here (cool stuff): http://www.codeproject.com/w2k/extendingshell.asp Now I am facing one problem. When I double-click on any saved file say stored on my desktop then my application opens as it should but it doesnt open my file. Obviously its because when my application initializes it should know the name of the file that I selected to open but I am not sure how to retieve this info. I guess when I right-click on the saved file and select from a context menu "Open this (.cmp) in my app" the shell Command will call my app and somehow pass the name of the file. So basically I am not sure how to read this command line arguments in my MFC app so I could get the name of the file and open it immediately after my app initialization. Actually would it be a command line or some other way of doing it? how do I tell my app what file I am opening.? Please let me know if its very easy solution for it. In that tutorial the author calls: C:\Windows\Notepad.exe %1 but I guess "%1" means nothing to my app as my app would not recognise what that is. I think I should replace %1 with my own command like "open" or "print" to distinguish the action my app should perform. Am I right? Please let me know if you could. Thank you..

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      Did you use AppWizard to create your application? If so, a .reg file would have been created in the project. Has RegisterShellFileTypes() been called from the application object? This is what registers the document types with the shell (e.g., Windows Explorer). After that, you should see a .cmp key under the HKEY_CLASSES_ROOT hive. Note the value of the (Default) value. That should also exist under the HKEY_CLASSES_ROOT hive. For example, if the extension were .txt instead, you'd see a .txt key under the HKEY_CLASSES_ROOT hive. The (Default) value is txtfile, so you'd also see a txtfile key under the HKEY_CLASSES_ROOT hive. The HKEY_CLASSES_ROOT\txtfile\shell key will have keys named open, print, and printto. Note the difference (the /p command-line parameter) between the command that opens versus the command that prints.


      "When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen

      R 1 Reply Last reply
      0
      • D David Crow

        Did you use AppWizard to create your application? If so, a .reg file would have been created in the project. Has RegisterShellFileTypes() been called from the application object? This is what registers the document types with the shell (e.g., Windows Explorer). After that, you should see a .cmp key under the HKEY_CLASSES_ROOT hive. Note the value of the (Default) value. That should also exist under the HKEY_CLASSES_ROOT hive. For example, if the extension were .txt instead, you'd see a .txt key under the HKEY_CLASSES_ROOT hive. The (Default) value is txtfile, so you'd also see a txtfile key under the HKEY_CLASSES_ROOT hive. The HKEY_CLASSES_ROOT\txtfile\shell key will have keys named open, print, and printto. Note the difference (the /p command-line parameter) between the command that opens versus the command that prints.


        "When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen

        R Offline
        R Offline
        robert_s
        wrote on last edited by
        #3

        No. I did update registry myself. all keys are added and working well! its just when you right-click on the filename.cmp it opens teh program which is correct but the programas isnt able to know what file was selected.

        D 1 Reply Last reply
        0
        • R robert_s

          No. I did update registry myself. all keys are added and working well! its just when you right-click on the filename.cmp it opens teh program which is correct but the programas isnt able to know what file was selected.

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          robert_s wrote: ...but the programas isnt able to know what file was selected. Did you add the %1 parameter to the registry entry? That is what gets replaced with the selected file? In your application, you can get access to the command-line parameters via the m_lpCmdLine member.


          "When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen

          R 1 Reply Last reply
          0
          • D David Crow

            robert_s wrote: ...but the programas isnt able to know what file was selected. Did you add the %1 parameter to the registry entry? That is what gets replaced with the selected file? In your application, you can get access to the command-line parameters via the m_lpCmdLine member.


            "When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen

            R Offline
            R Offline
            robert_s
            wrote on last edited by
            #5

            Yes I added %1 in registry c:\Documents and Settings\user\My Documents\Prog\Prog.exe %1 Is it correct? but I am not sure whether it should be separated by space or a comma ',' when I add the following code to: BOOL CProgView::PreCreateWindow(CREATESTRUCT& cs) {..... LPTSTR str = ::GetCommandLine(); AfxMessageBox(str); ..} The str contains one long string "c:\Documents and Settings\user\My Documents\Prog\Prog.exe c:\Docum~1\use~\Desktop\test~1.cmp" I guess its what I should get but its a bit silly to have one long string as it will be a hassel to separate one argument from the other as the are blank spaces all over the place so it will be hard to determine where to split it.

            D 1 Reply Last reply
            0
            • R robert_s

              Yes I added %1 in registry c:\Documents and Settings\user\My Documents\Prog\Prog.exe %1 Is it correct? but I am not sure whether it should be separated by space or a comma ',' when I add the following code to: BOOL CProgView::PreCreateWindow(CREATESTRUCT& cs) {..... LPTSTR str = ::GetCommandLine(); AfxMessageBox(str); ..} The str contains one long string "c:\Documents and Settings\user\My Documents\Prog\Prog.exe c:\Docum~1\use~\Desktop\test~1.cmp" I guess its what I should get but its a bit silly to have one long string as it will be a hassel to separate one argument from the other as the are blank spaces all over the place so it will be hard to determine where to split it.

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              robert_s wrote: c:\Documents and Settings\user\My Documents\Prog\Prog.exe %1 Is it correct? Be sure and put double-quotes around the %1. robert_s wrote: its a bit silly to have one long string as it will be a hassel to separate one argument from the other as the are blank spaces all over the place so it will be hard to determine where to split it. Can you use the CCommandLineInfo class? If not, __argv[1] is always available.


              "When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups