Click here to Skip to main content
15,899,474 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am developing a C++ MFC dialog based application.
I've read a couple articles about how to use my own extension for my C++ app.
I think I'll manage to edit the registry and set the assocation of the extension.

My question is:
How can I read the actual data from the file when I am double clicking on the file and my application runs. I want to set one variable with data from the file.

For example: I have a txt file with nothing more than a string containing "Hello".
When I click on the txt file and open with: MyApp.exe , the string "Hello" needs to be assigned to a CString in the C++ MFC App.

I searched for a solution, but couldn't find it.

Thanks in advance.

Michael.
Posted

Use the CFile[^] class to access your file and read its contents.
 
Share this answer
 
I know how to use CFile and read a file, but I cannot use this because I don't know the path of the file.
I am not running the app and then reading the file..
I want to run the file, it opens my app, because it has my own extension, and then I want to use the content of that file in my App. I don't know where the file is located on the client computer.

Do you want me to still use CFile to get what I want?

This answer is just to make sure we understand each other.

Regards, Michael.
 
Share this answer
 
Comments
Stefan_Lang 20-Oct-11 11:17am    
Please do not post a comment or response to another user as a solution!

To respond to a specific solution, press the 'Have a Question or Comment?' link at the bottom right of the solution.

To reply to a comment, press the 'Reply' link at the top right of the comment.

To add information about your problem, click the 'Improve Question' Link at the bottom center of your original posting.
Once you manage to associate a specific file name extension with your application, a double-click on a file with such an extension will automatically call your application with the file name as a command line parameter. So, what you need to do is read the command line arguments.

Here is an example from http://msdn.microsoft.com/en-US/library/hwbccf8z%28v=VS.80%29.aspx[^]. I simplified it a bit for your purpose:

C++
#include <afx.h>
#include <afxwin.h>
#include <iostream>

using namespace std;

CWinApp theApp;

int main(int argc, char *argv[])
{
   if (!AfxWinInit(GetModuleHandle(NULL), NULL, GetCommandLine(), 0))
   {
      cout << "panic: MFC couldn't initialize!" << endl;
      return 1;
   }

   // see that we have a reasonable number of arguments
   if (argc != 2)
   {
      cout << "usage: " << argv[0];
      cout << " <source>" << endl;
      cout << endl;
      return 1;
   }

   // the string object to hold the file contents
   CString result;

   // constructing this file object doesn't open it
   CFile sourceFile;

   // we'll use a CFileException object to get error information
   CFileException ex;

   // open the source file for reading
   if (!sourceFile.Open(argv[1],
      CFile::modeRead | CFile::shareDenyWrite, &ex))
   {
      // complain if an error happened
      // no need to delete the ex object
      TCHAR szError[1024];
      ex.GetErrorMessage(szError, 1024);
      cout << "Couldn't open source file: ";
      cout << szError;
      return 1;
   }
   else
   {
      BYTE buffer[4096];
      DWORD dwRead;

      // Read in 4096-byte blocks, This loop ends
      // when there are no more bytes to read.
      do
      {
         dwRead = sourceFile.Read(buffer, 4096);
         result += buffer;
      }
      while (dwRead > 0);

      // Close file
      sourceFile.Close();

      // print out the total string
      cout << result << endl;
   }

   return 0;
}</source>
 
Share this answer
 
Comments
Demmers 21-Oct-11 6:34am    
Stefan_Lang,

Thanks a lot for this answer.
As you see, I found out how to reply a comment :) Thanks.

I just don't know where to use this int main() function.
I have a MFC C++ dialog based application.

I searched on Google for the a commandline function and found it.
I use the following code to get the commandline of the opened file:

CString commandline;
commandline = GetCommandLine();

After this...
The application removes the information I don't need from the CString and open the file, read it and use the content.

Thanks a lot

Michael.
Stefan_Lang 21-Oct-11 6:49am    
Ok, after rereading your original post I realize I may have misunderstood your problem:

My understanding was that your application isn't running, in which case double-clicking a file with an extension that is associated to your application will start that application and pass it's name on the command line.

However, you said your application is already running, and then you double-click on a file. In this case, the command line is irrelevant, as it was executed before you ever clicked on the file.

This is going to be tricky... I'll consider how best to go about that and post a separate response if I can think about anything.
Stefan_Lang 21-Oct-11 7:32am    
The only suggestion I can think of is to open a file dialog and select the file from there. You can find more info (and a link to an example) on this page: http://msdn.microsoft.com/de-de/library/wh5hz49d%28v=vs.80%29.aspx

There are other ways, but I don't know how to implement any of them.
Demmers 21-Oct-11 9:57am    
Forget about the GetCommandLine() function.
The solution for my application is as following:

When I include windows.h , I can use the arguments.

CString commandline;

commandline = __argv[1];

This is enough to get the full path of the file.

Further,
The __argc integer tells you the number of arguments.

Thanks again for all the help and quick reply's.
Demmers 21-Oct-11 6:56am    
Your first understanding that my application isn't running was right!

My application isn't running when the associated file is double clicked.
The application then starts. When I press a button on the application the GetCommandLine() function will be executed.
I use this information to read the file. Here is my code to make sure you understand what I am doing now.

CString commandline;
CFile ConfigFile;
char szSampleText[100] = "";
int positionExe = 0;

commandline = GetCommandLine();

positionExe = commandline.Find(".exe");
positionExe += 7;
commandline.Delete(0, positionExe);
commandline.Remove('"');

ConfigFile.Open( commandline, CFile::modeRead|CFile::modeNoTruncate|CFile::shareDenyNone);

codelength = ConfigFile.GetLength();

UINT lBytesRead = ConfigFile.Read (szSampleText, codelength);

code = szSampleText;

if(codelength)
AfxMessageBox(code, MB_OK | MB_ICONINFORMATION);




Thanks,

Michael.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900