File Association (Windows SDK)

Source: Internet
Author: User

Associated with the hosts file.

File Association is set in the hkey_classes_root Root Key of the Registry. To set an association for an extension, you must set two subkeys under the hkey_classes_root root key. The first subkey name is ". extension "(corresponding to the data file extension). The default value must be set for this subkey. The default value is the name of another subkey under the hkey_classes_root Root Key, you can continue to set the executable file name associated with the data file under this subkey.

If the associated operation method is "Open", you can continue to create the "shell/Open/command" sub-key in the second sub-key, set the default value of the command sub-Key to the executable file name. Double-click the data file to execute the executable file. If the associated operation is "print ", you can create the "shell/print/command" subkey In the second subkey, and set the default value of the command subkey to the executable file name for the print operation. Of course, you can also set only the association of "open" operations. After these two steps, the file association is set.

With "*. for example, create a data file named ". test sub-key, set its default value to "testfile", and then create a "testfile/Shell/Open/command" sub-key, set the default value of the command key to the specified executable file name.

The following sectionCodeAssociate the "*. Test" data file with the current executable file:

// Associate. c
// Book Program Set the registry and associate the. Test file with the program. After the association is successful, double-click the. Test file.
// Windows will automatically open this file
// To associate a program with a file with a specific extension, perform the following two steps:
// 1. Create a subkey with the name extension under hkey_classes_root. For example, you need to associate the. Test File
// You need to create a. Test sub-key under it, and then create a default key value, that is, only the key value has no key name
// This key value is the key name that creates the 2nd key. For example, we take the key name testfile.
// 2. Create another sub-key under hkey_classes_root. This sub-key is the key name created in step 1, such as testfile
// If you want to open a file, create the shell/Open/command sub-key under this sub-key with the key value:
// Executable file name % 1
//
// The structure after successful creation may be as follows:
// Hkey_classes_root
// |
// | ---. Test (there is a default key value testfile) [default = testfile]
// |
// | --- Testfile
// |
// | -- Shell
// |
// | -- Open
// |
// | -- Command (there is a default key value, such as C:/associate.exe % 1)
// [Default = C:/associate.exe % 1]
# Include <windows. h>
# Include "resource. H"

Hwnd hwinmain = NULL;

Char szkeyenter [] = text ("testfile ");
Char szkeyext1 [] = text (". test ");
Char szkeyext2 [] = text ("testfile // shell // open // command ");
Char szparam [] = text ("/" % 1 /"");

Char szdelsub1 [] = text ("testfile // shell // open ");
Char szdelsub2 [] = text ("testfile // shell ");

Void setassociate ();
Void delassociate ();
Bool isassociate ();

Lresult callback dialogproc (hwnd, uint umsg, wparam, lparam );

Int winapi winmain (in hinstance, in hinstance hprevinstance, in lpstr lpcmdline, in int nshowcmd)
{
Dialogboxparam (hinstance, makeintresource (idd_main), null, dialogproc, 0 );
Return 0;
}

Void setassociate ()
{
Hkey;
Char szfilename [max_path];

If (error_success = regcreatekey (hkey_classes_root, szkeyext1, & hkey ))
{
Regsetvalueex (hkey, null, 0, REG_SZ, szkeyenter, sizeof (szkeyenter ));
Regclosekey (hkey );
}

If (error_success = regcreatekey (hkey_classes_root, szkeyext2, & hkey ))
{
Getmodulefilename (null, szfilename, sizeof (szfilename ));
Lstrcat (szfilename, szparam );
Regsetvalueex (hkey, null, 0, reg_expand_sz, szfilename, strlen (szfilename) + 1 );
Regclosekey (hkey );
}
}

Void delassociate ()
{
Hkey;
Regdeletekey (hkey_classes_root, szkeyext1 );
// You can only delete one layer at a time under NT.
// Testfile // shell // open // command
If (error_success = regopenkeyex (hkey_classes_root, szdelsub1, 0, key_write, & hkey ))
{
Regdeletekey (hkey, "command"); // deletes the command layer.
Regclosekey (hkey );
}
If (error_success = regopenkeyex (hkey_classes_root, szdelsub2, 0, key_write, & hkey ))
{
Regdeletekey (hkey, "open"); // Delete the open Layer
Regclosekey (hkey );
}
If (error_success = regopenkeyex (hkey_classes_root, szkeyenter, 0, key_write, & hkey ))
{
Regdeletekey (hkey, "shell"); // Delete the shell layer
Regclosekey (hkey );
}
Regdeletekey (hkey_classes_root, szkeyenter); // Delete the testfile layer.
}

Bool isassociate ()
{
Hkey;

If (error_success = regopenkeyex (hkey_classes_root, szkeyext1, 0, key_write, & hkey ))
{
Regclosekey (hkey );
Return true;
}
Return false;
}

Lresult callback dialogproc (hwnd, uint umsg, wparam, lparam)
{
Switch (umsg)
{
Case wm_close:
Enddialog (hwnd, 0 );
Break;
Case wm_initdialog:
Hwinmain = hwnd;
If (isassociate ())
{
Checkdlgbutton (hwnd, idc_associate, bst_checked );
}
Break;
Case wm_command:
If (loword (wparam) = idc_associate &&
Hiword (wparam) = bn_clicked)
{
If (isdlgbuttonchecked (hwinmain, idc_associate ))
Setassociate ();
Else
Delassociate ();
}
Break;
Default:
Return false;
}
Return true;
}

// Resource. h
# Define idd_main 101
# Define idc_associate 1000
# Define idc_static-1

// Associate. RC
# Include "resource. H"
# Include "afxres. H"
//////////////////////////////////////// /////////////////////////////////////
//
// Dialog
//

Idd_main dialog discardable 0, 0,143, 73
Style ds_modalframe | ws_popup | ws_caption | ws_sysmenu
Caption "File Association example"
Font 10, ""
Begin
Control "associates the program with the. Test File", idc_associate, "button ",
Bs_autocheckbox | bs_multiline | ws_tabstop, 7,7, 129,18
Ltext "NOTE: After Association, When you double-click the. Test file, Windows will automatically open the program, cancel the association, and remove the preceding check box ",
Idc_static, 7,31, 129,35
End

After the program runs, select the check box and associate the program with the. Test file. In this case, you can close the program and create an empty one. test file, such as. test. Double-click. test file, you find the program is started, and then you cancel the check box to close the program, and then double-click. test. This is a simple association implementation. You can refer to this program to design more complex associations, or even change the associated file icons.

the theory of this program is partly based on Luo yunbin's 32-bit assembly language book. Source Code is also rewritten by the assembly language. See this book to view the assembly version of the program.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.