How to associate a file type with program?

Hello,

How can I associate a file type with my program? I think I can do this by editing registry but maybe there's an API function for this?

Thanks for help.
Take a look at this link: http://technet.microsoft.com/en-us/magazine/ff687021.aspx

There are the 2 windows in built command lines for handling this kind of thing, it would be fairly easy to use from a "system" call ( http://www.cplusplus.com/reference/clibrary/cstdlib/system/ )

Hope that helps.
@chris: have you read this? http://cplusplus.com/forum/articles/11153/
I figured this out myself, here's the code if anyone needs it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46


int main()
{
HKEY hkey;

string extension=".002";            // file extension
string desc="my file type";         // file type description

string app="C:\\MyProgram.exe %1";      // Open with MyProgram.exe an pass filename as 1st argument (Probably)
string action="Open with my program";

string sub="\\shell\\";

string path=extension+
            sub+
            action+
            "\\"
            "command\\";


// 1: Create subkey for extension -> HKEY_CLASSES_ROOT\.002
if(RegCreateKeyEx(HKEY_CLASSES_ROOT,extension.c_str(),0,0,0,KEY_ALL_ACCESS,0,&hkey,0)!=ERROR_SUCCESS)
  {
      cerr <<"Could not create or open a registrty key\n";
      return 1;
  }
RegSetValueEx(hkey,"",0,REG_SZ,(BYTE*)desc.c_str(),sizeof(desc)); // default vlaue is description of file extension
RegCloseKey(hkey);



// 2: Create Subkeys for action ( "Open with my program" )
// HKEY_CLASSES_ROOT\.002\Shell\\open with my program\\command
if(RegCreateKeyEx(HKEY_CLASSES_ROOT,path.c_str(),0,0,0,KEY_ALL_ACCESS,0,&hkey,0)!=ERROR_SUCCESS)
  {
      cerr <<"Could not create or open a registrty key\n";
      return 1;
  }
RegSetValueEx(hkey,"",0,REG_SZ,(BYTE*)app.c_str(),app.length());


RegCloseKey(hkey);

return 0;
}

Last edited on
Topic archived. No new replies allowed.