Click here to Skip to main content
15,881,791 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,

I created my own file extension .sos and i registered it in HKEY_CLASSES_ROOT manually .It is working.

But
How can i register & unregister it using vc++ code?

guide me to implement the code.






Thanks in advance.
Posted
Updated 20-May-12 17:37pm
v2

You said when you manually register your extension it works, then you must know what keys to write. It it not working in code, maybe your app does not have privileges to write to those registry keys.
 
Share this answer
 
Comments
rajesh482 21-May-12 5:28am    
Thank you.
What i mean is suppose we have notepad setup file,when we double click on it automatically it register the file extension and all the things what application required no need do it manually. These things are achieve through inbuilt code only.

So i want to prepare exe File When we Double click on it.It will so the all the above process.
Could you please tell me How can i do this one?
Mudaka 21-May-12 6:02am    
Sorry I might not have gotten your question correctly. If you want to know how to write a registry key, there are these two Windows functions that you need: RegOpenKeyEx and RegSetValueEx. You can find their usage in MSDN.
There are a number of keys that need to be set in order for this to work.

Create a new key in HKEY_CLASSES_ROOT for this file extension string (including the leading dot character) and set its default value to a unique name that you can use to link the two together. This will give the following key and value:
HKEY_CLASSES_ROOT\.ext
Default extfile

Create a subkey OpenWithList\application.exe where application.exe is the name of the application that will handle files with this extension. You now have the key:
HKEY_CLASSES_ROOT\.ext\OpenWithList\application.exe

Create a key with the same name as the default value in the HKEY_CLASSES_ROOT\.ext key and set its default value to the unadorned name of the application as used in OpenWithList\application.exe, giving the key and value:
HKEY_CLASSES_ROOT\extfile
Default application file

Add a subkey to the Applications key in HKEY_CLASSES_ROOT as follows, and set its default value to the full path of your application followed by a placeholder for the filename when you click on it, thus:
HKEY_CLASSES_ROOT\Applications\application.exe\shell\open\command
Default C:\Program Files\Utilities\application.exe %1

Add a similar subkey to the extfile key thus:
HKEY_CLASSES_ROOT\extfile\shell\open\command
Default C:\Program Files\Utilities\application.exe %1

Note how the extension, the "extension type" name and the program name are linked in order for this to work.
 
Share this answer
 
Comments
rajesh482 7-Jun-12 10:49am    
thank you.
I did this already.We need to do this manually.

How can i acheive this through the code? (means when i double on my exe it has do this registration process)
Richard MacCutchan 7-Jun-12 11:00am    
Well, I have explained that to you in the above answer. Write a program which will set the various registry keys as shown.
rajesh482 8-Jun-12 11:48am    
thank you sir

But how can i place this code in proper manner into my source code
rajesh482 5-Jun-13 5:51am    
This is the code to register extenstion
=============================================
int RegisterExtenstion( )
{
HKEY hkey;

if (ERROR_SUCCESS==RegOpenKey(HKEY_CLASSES_ROOT,".mfe",&hkey))
{
RegCloseKey(hkey);
AfxMessageBox("This Extension is already registered");
GetDlgItem(IDOK)->ShowWindow(SW_HIDE);
return 0;
}
CString sExt=".mfe";
CString sDescription="My File Extension";

sDescription.Replace(" ",""); //revoing spaces in Description
sExt.Replace(" ",""); // sExt – extension to register

sExt.Replace(".","");
sExt="."+sExt;


CString app=GetCommandLine(); // Getting the application Path

app.TrimLeft(_T("\"")); app.TrimRight(_T("\""));

CString str,strIcon;
str.Format(_T("%s"),app);
PathRemoveFileSpec((LPTSTR)(LPCTSTR)str) ; //for this #include "shlwapi.h"
//#pragma comment(lib,"Shlwapi.lib")
strIcon.Format(_T("%s\\Sample.ico \0"),str);//place "Sample icon" in exe folder

app+=" \"%1\"";

RegOpenKeyEx(HKEY_CLASSES_ROOT,"",0,KEY_QUERY_VALUE,&hkey);
DWORD dw;

RegCreateKeyEx(hkey, sExt.operator LPCTSTR() , 0L, NULL,
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
NULL,&hkey, &dw );

CString key=sDescription;

RegSetValueEx(hkey,"",0,REG_SZ,(BYTE *)key.operator LPCTSTR(),
key.GetLength());
RegCloseKey(hkey);

RegOpenKeyEx(HKEY_CLASSES_ROOT,"",0,KEY_QUERY_VALUE,&hkey);

RegCreateKeyEx (hkey, key, 0L, NULL,
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
NULL, &hkey, &dw);

RegCreateKeyEx (hkey, "shell", 0L, NULL,
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
NULL,&hkey, &dw);

RegCreateKeyEx (hkey, "open", 0L, NULL,
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
NULL,&hkey, &dw);

RegCreateKeyEx (hkey, "command", 0L, NULL,
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
NULL,&hkey, &dw);

RegSetValueEx(hkey,"",0,REG_SZ, (LPBYTE)(LPCTSTR)app,app.GetLength());
RegCloseKey(hkey);

RegOpenKeyEx(HKEY_CLASSES_ROOT,key,0,KEY_QUERY_VALUE,&hkey);
RegCreateKeyEx (hkey, "DefaultIcon", 0L, NULL,
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
NULL,&hkey, &dw);

RegSetValueEx(hkey,"",0,REG_SZ, (LPBYTE)(LPCTSTR)strIcon,strIcon.GetLength());
RegCloseKey(hkey);

return 1;
}
Richard MacCutchan 5-Jun-13 5:52am    
And?

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