How Open a user-defined extension file in Program?

586 Views Asked by At

I'm programming a data logger GUI software using Borland C++Builder 2006. It's my Company desired platform.

I'm saving received data after some analyzing in specific files with my defined extension as ".evp" . I have defined Save and Open Functions in my programs that worked fine.

Now I have problem with how to open my saved file directly without using my software. I mean when I double-click on a saved file, it open automatically with my software and show data. Then there is two Question :

  1. How I tell windows to open this ".evp" file with my software.
  2. How I handle the opened file in my software and use it my defined Open function.
1

There are 1 best solutions below

5
On BEST ANSWER

by Borland C2006 you mean Embarcadero BDS2006 Turbo(explorer) C++?

  1. File associations are stored somewhere in registry

    See Create registry entry to associate file extension with application in C++. So you can create reg file or add registry entries programaticaly by WinAPI.

    I never updated registry in my apps but this is example of how you can read registry (obtaining CPU frequency):

    double getregfcpu()
    {
    DWORD keyhnd;
    HKEY  hKey=NULL;
    DWORD dwSize;
    DWORD dwFrequency;
    double f=0.0;
    for(;;)
        {
        keyhnd=RegOpenKeyEx(HKEY_LOCAL_MACHINE,"Hardware\\Description\\System\\CentralProcessor\\0",0,KEY_QUERY_VALUE,&hKey);
        if (keyhnd!=ERROR_SUCCESS) break;
        dwSize = sizeof(dwFrequency);
        keyhnd = RegQueryValueEx (hKey,"~MHz",NULL,NULL,(LPBYTE)&dwFrequency,&dwSize );
        if (keyhnd!=ERROR_SUCCESS) break;
        f=dwFrequency; f*=1e6;
        break;
        }
    if (hKey!=NULL) RegCloseKey(hKey);
    return f;
    }
    

    so experiment/use RegCreateKeyEx instead of RegOpenKeyEx. Strongly recommend to experiment first on some own registry entry not related to windows so you do not mess something up. And only when working as should change the entry name to desired location.

  2. Application side

    Go to your App source file (the one that opens when you open project in IDE). Find there WinMain function and change it like this:

    WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR cmdl, int)
    {
        try
        {
            Application->Initialize();
            Application->Tag=(int)cmdl; // *** ADD THIS LINE ***
            Application->CreateForm(__classid(TMain), &Main);
            Application->Run();
        }
        catch (Exception &exception)
        {
            Application->ShowException(&exception);
        }
        catch (...)
        {
            try
            {
                throw Exception("");
            }
            catch (Exception &exception)
            {
                Application->ShowException(&exception);
            }
        }
        return 0;
    }
    

    The cmdl holds the pointer to the command line with which your application was called. So copy it to your Application->Tag which is intend for any user defined behavior. This way the cmdl is accessible from whole project (in any window/form). Now to extract the file to open you just do in your Form constructor something like this:

    AnsiString cmdl=(LPSTR)Application->Tag;    // access the command line parameters
    if (cmdl.Length()>2) // file is encapsulated with ""
     if (cmdl[1]=='"')
      if (cmdl[cmdl.Length()]=='"')
        {
        AnsiString s="";
        for (int i=2;i<cmdl.Length();i++) s+=cmdl[i]; // cut of the ""
        // here load file: s
        }
    

    If you want to have more command line options then you need to upgrade this a bit by searching for the "" first and process all the command line switches ... Sometimes it is useful to use Application exe local path. You can get it like this:

    AnsiString exepath=ExtractFilePath(Application->ExeName);