Sign in to follow this  

Associating icons to an extension via c++

This topic is 3290 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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++?

Share this post


Link to post
Share on other sites
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"

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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);
}


Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 3290 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this