Associating icons to an extension via c++

Started by
4 comments, last by ValMan 15 years, 5 months ago
The subject was descriptive enough, but to clarify what I'm trying to do in a little more detail, I made a simple extension that I have an icon for, and I'd like to automatically associate it with a program of my choice and give it an icon to see in explorer without setting it manually through some control panel form. How can I accomplish this in C++?
Advertisement
I'm not sure if there's a Win32 api call already existing for it, but you could use RegCreateKey to modify the registry yourself, it's not much complicated. Here is an example of what the .txt association look like in the registry. Of course you don't need a print command :

[HKEY_CLASSES_ROOT\.txt]
@="textfile"

[HKEY_CLASSES_ROOT\textfile\shell\open\command]
@="C:\\WINDOWS\\NOTEPAD.EXE %1"

[HKEY_CLASSES_ROOT\textfile\shell\print\command]
@="C:\\WINDOWS\\NOTEPAD.EXE /p %1"

[HKEY_CLASSES_ROOT\textfile\DefaultIcon]
@="C:\\WINDOWS\\SYSTEM\\shell32.dll,-152"
Thanks!... but I'm still confused about a couple of things...
What is the first value? Is it just some arbitrary title for the file type?
If I wanted to make something like a ".car" extension, I would use "carfile"?

How would I know what those paramaters (control characters?) mean after a program I want to use? More so, how do I know which one's are available? Are they even needed?

I assume that param after shell32.dll is just the icon index. is that zero based?
Here's some C code stripped from one of my programs:

- pszDotExt is extention prefixed with dot, can be any number of chars not just 3.

- pszShort is "short name" for file type. Example: cabfile, textfile, etc.

- pszLong is "long name" for file type. Example: Cabinet File, Text File, etc.

- pszIcon is name of file that contains icon in its resources, followed by comma, followed by icon index. Or, can specify the name of .ico file directly.

- pszCommand is the command line. Typically it's exename followed by space, followed by quote, followed by %1, followed by quote. This means that inside your exe when you call GetCommandLine(), you will get the %1 parameter (file name) enclosed in quotes.

void RegisterFileType(const char* pszDotExt, const char* pszShort, const char* pszLong, const char* pszIcon, const char* pszCommand){	const char SZ_KSHELL[] = "Shell";	const char SZ_KOPEN[] = "Open";	const char SZ_KCOMMAND[] = "Command";	const char SZ_KICON[] = "DefaultIcon";	HKEY hKey = NULL;	HKEY hKeyIcon = NULL;	HKEY hKeyShell = NULL;	HKEY hKeyOpen = NULL;	HKEY hKeyCommand = NULL;	/* Create ext key */	if(RegOpenKey(HKEY_CLASSES_ROOT, pszDotExt, &hKey) != ERROR_SUCCESS)	{		if(RegCreateKey(HKEY_CLASSES_ROOT, pszDotExt, &hKey) != ERROR_SUCCESS)		{			g_bErrors = TRUE;			return;		}	}	if(RegSetValue(hKey, NULL, REG_SZ, pszShort, strlen(pszShort)) != ERROR_SUCCESS)	{		RegCloseKey(hKey);		g_bErrors = TRUE;		return;	}	RegCloseKey(hKey);	/* Create main key */	if(RegOpenKey(HKEY_CLASSES_ROOT, pszShort, &hKey) != ERROR_SUCCESS)	{		if(RegCreateKey(HKEY_CLASSES_ROOT, pszShort, &hKey) != ERROR_SUCCESS)		{			g_bErrors = TRUE;			return;		}	}	if(RegSetValue(hKey, NULL, REG_SZ, pszLong, strlen(pszLong)) != ERROR_SUCCESS)		g_bErrors = TRUE;	/* Create icon key */	if(RegOpenKey(hKey, SZ_KICON, &hKeyIcon) != ERROR_SUCCESS)	{		if(!RegCreateKey(hKey, SZ_KICON, &hKeyIcon))			g_bErrors = TRUE;	}	if(!RegSetValue(hKeyIcon, NULL, REG_SZ, pszIcon, strlen(pszIcon)))		g_bErrors = TRUE;	/* Create shell key */	if(RegOpenKey(hKey, SZ_KSHELL, &hKeyShell) != ERROR_SUCCESS)	{		if(RegCreateKey(hKey, SZ_KSHELL, &hKeyShell) != ERROR_SUCCESS)			g_bErrors = TRUE;	}	/* Create open key */	if(RegOpenKey(hKeyShell, SZ_KOPEN, &hKeyOpen) != ERROR_SUCCESS)	{		if(!RegCreateKey(hKeyShell, SZ_KOPEN, &hKeyOpen) != ERROR_SUCCESS)			g_bErrors = TRUE;	}	/* Create command key */	if(RegCreateKey(hKeyOpen, SZ_KCOMMAND, &hKeyCommand) != ERROR_SUCCESS)	{		if(!RegCreateKey(hKeyOpen, SZ_KCOMMAND, &hKeyCommand) != ERROR_SUCCESS)			g_bErrors = TRUE;	}	if(RegSetValue(hKeyCommand, NULL, REG_SZ, pszCommand, strlen(pszCommand)) != ERROR_SUCCESS)		g_bErrors = TRUE;	/* Clean up */	RegCloseKey(hKeyCommand);	RegCloseKey(hKeyOpen);	RegCloseKey(hKeyShell);	RegCloseKey(hKeyIcon);	RegCloseKey(hKey);}
Much thanks! Does the icon loading part of the code make use of DLLs as well?
If so, I access it with a '-' followed by the index of the icon?
It should work with DLL's. I believe the dash means that you are specifying icon resource ID instead of icon index. But really, just try and see.

This topic is closed to new replies.

Advertisement