Open a file.myext via contxt menu in my application
-
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..
-
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..
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
-
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
-
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.
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
-
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
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.
-
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.
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