[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 345
  • Last Modified:

Open a file in my program after double clicking it in Windows Explorer.

Hi All,
I have written code in my program that sets a key in the registry when my program is started so that if at a latter time a user double clicks a file with the extension that my program uses it will open my program. Now I need help with making my program open that file as well. I have no Idea how to have a file that is double clicked in Windows Explorer open in my program. Can someone show me some code to do this? What I am looking for is like when you double click a text.txt file it opens notepad and notepad opens the file.

Thanks,
Arnold.

0
arnold100
Asked:
arnold100
  • 15
  • 8
  • 4
1 Solution
 
r-kCommented:
This is a two step process:

(1) Make a registry entry in :

HKEY_CLASSES_ROOT\myfiletype\Shell\open\command

so the default value is something like:

 "C:\path\myprogram.exe" %1

(of course, change "myfiletype" and "myprogram" to match your conditions.)

It appears you've already done the above.

(2) Handle the command line argument in your program. When you double-click on the file, Windows will launch your program and pass it the file name as the first argument. It is upto your program to deal with it.

If you need more advice please post back.
0
 
arnold100Author Commented:
I have already set the registry key. My program launches fine. There is no comand line argument because I am working in mfc. I am using the OFN file structure. I need a way to get the file name that is double clicked passed to a CString object in my program.
0
 
waysideCommented:
Mfc programs still have access to the command line.

Check out the value of m_lpCmdLine  in your CWinApp-derived class, it should contain the name of the file you've double-clicked on. You can put this check in your InitInstance() function:

BOOL CMyApp::InitInstance()
{
   // ...
 
   if (m_lpCmdLine[0] == _T('\0'))
   {
      // Create a new (empty) document.
      OnFileNew();
   }
   else
   {
      // Open a file passed as the first command line parameter.
      OpenDocumentFile(m_lpCmdLine);
   }
 
   // ...
}
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
arnold100Author Commented:
I will give it a try. I am working with a dialog based program.
0
 
arnold100Author Commented:
Compiles fine but still doesn't open the file.
0
 
arnold100Author Commented:
Also get a This program has performed an illegal operation messabebox on exit of the program.
0
 
waysideCommented:
For a dialog app, you wouldn't use OpenDocumentFile() or OnFileNew(), those are for SDI/MDI app.

Instead, you would want to pass in the file name to your dialog, and have the dialog code open the file.

For example, add a member variable to your dialog to hold the file name:

classs CMyDialog {
...

public:
CString m_fileName;

}

In InitInstance, set this member to the file:

CMyDialog dlg;
dlg.m_fileName = m_lpCommandLine;

Then in your dialog's OnInitDialog() code (or wherever is the right spot) you can open the file:

CMyDialog::OnInitDialog() {
...
  if (m_fileName != "") {
     MyOpenFile();
  }


You could also pass the file name in as a constructor argument, or add a function, or whatever.
0
 
arnold100Author Commented:
Now I get the error messagebox on startup.

I am using the CFile f.Open() to open the file.
0
 
arnold100Author Commented:
What's not happening is the name of the file I am double clicking on is not getting passed to m_lpCmdLine.
0
 
waysideCommented:
I just created a new dialog app fram scratch, and added the following line at the top of InitInstance:

AfxMessageBox(m_lpCmdLine);

I then created a key in HKEY_CLASSES_ROOT called .xxx, added shell, open, and command subkeys, and then set the default value of the command key run my program. The whole thing looks like:

HKEY_CLASSES_ROOT\.xxx\shell\open\command  C:\utils\testdlg\Debug\testdlg.exe "%1"

I then created a file with a .xxx suffix, and when I double-clicked it my app opened, and the file name showed in the message box, showing that it was passed in to the app.

What is the exact value of your HKEY_CLASSES_ROOT\myfiletype\Shell\open\command key? Did you add a %1 or "%1" to the value? If you don't, the file name will not be passed in to your app.


0
 
arnold100Author Commented:
Here is what I had.
HKEY_CLASSES_ROOT\crp_auto_file\Shell\open\command\C:\My PROGRAM\PROGRAM.EXE"%1""

Changed to: HKEY_CLASSES_ROOT\.crp\shell\open\command\C:\MY PROGRAM\PROGRAM.EXE"%1""

Still no luck. Where are you creating m_lpCmdLine?
0
 
r-kCommented:
I think you need a space between the .exe and the %1

You don't need to create m_lpCmdLine. it's a member of CWinApp.

0
 
waysideCommented:
m_lpCmdLine is a data member of your CwinApp-dervied class. It doesn't need to be created explicitly, it is set up for you when your program starts.

Your command key looks a little garbled. If you use regedit to browse to this key, when you click on "command" in the left pane, the right pane shows one name of (Default) with a value. For you that value looks like it should be

"C:\My PROGRAM\PROGRAM.EXE" "%1"

The spaces and the double quotes are important.

This should cause the file name of any .crp file to be passed in to the program when you double-click on it, and the name should be in the m_lpCmdLine variable.
0
 
arnold100Author Commented:
Ok, I get the path "C:\MYDOCU~1\FILE.CRP" which is not right. It should be "C:\My Documents\File.crp"

The file doesn't open.
0
 
r-kCommented:
That's OK, that is just the "short name" that Windows uses to point to folders with names longer than 8 characters. You should still be able to open that file in your program.
0
 
arnold100Author Commented:
For some reason in the OnInitDialog function the m_fileName variable gets sey back to null before opening the file.
0
 
waysideCommented:
Show us your code, please.

Both where you are setting the m_fileName variable, and your OnInitDialog function.

0
 
arnold100Author Commented:
InitInstance()
{
     dlg.m_fileName=m_lpCmdLine;
//........
}

OnInitDialog()
{
    CFile f;
    if(m_fileName!="")
    {
         f.Open(m_sfileName,CFile::modeRead);
         
         CArchive ar(&f,CArchive::load);
         ar>>m_Edit1>>m_Edit2;
         ar.Close();
         f.Close();
      }
}
0
 
waysideCommented:
>          f.Open(m_sfileName,CFile::modeRead);

What is m_sfileName? The string used in the if is named m_fileName.

0
 
arnold100Author Commented:
InitInstance()
{
     dlg.m_fileName=m_lpCmdLine;
    AfxMessageBox(dlg.m_fileName) // Shows the file I double click.
//........
}

OnInitDialog()
{
    AfxMessageBox(m_fileName)//Shows blank.
    CFile f;
    if(m_fileName!="")//Here m_fileName = null for some reason.
    {
         f.Open(m_sfileName,CFile::modeRead);
         
         CArchive ar(&f,CArchive::load);
         ar>>m_Edit1>>m_Edit2;
         ar.Close();
         f.Close();
      }
}
0
 
r-kCommented:
I think it might help if you could cut and paste the actual code you're using. You can trim out non-essential lines, but not re-type anything. For example, what is dlg?
0
 
arnold100Author Commented:
I have found the problem. I now have both massageboxs
showing the file name like this "C:\Myfile.crp" the problem is that m_lpCmdLine is adding the "" to the file name. The messageboxs should read C:\Myfile.crp with no "". I haven't figured out how to remove the "" yet.
When there gone the problem will be gone.
0
 
waysideCommented:
The CFile object should be able to take a file name that is enclosed in quotes, but if you really want to remove them do something like this:

     dlg.m_fileName=m_lpCmdLine;
     dlg.m_fileName = dlg.m_fileName.Mid(1, dlg.m_fileName.Length()-2);

This will extract the characters between the quotes and put them back in the string.
0
 
arnold100Author Commented:
I get a compiler error: Length is not a member of CString.
0
 
arnold100Author Commented:
I will give GetLength a try.
0
 
arnold100Author Commented:
Works perfectly wayside.
Thank you very much for your time and help,
I sure hope they pay you what your worth.

Sincerely your,
Arnold.
0
 
waysideCommented:
Thanks for the kind words!
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 15
  • 8
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now