How to use shell to implement the program group shortcut to the author Bood E-mail address Boodweb@263.net Keywords: shell function com
(1) Foreword Once I saw a method of implementing the DDE in the program group to add a project in "Computer Programming and Maintenance", it is clear that the MSDN should be implemented with more advanced shell functions, so the author carefully looks This method finally found in an article called "Shortcut: a SampleThat Manipulates Shortcuts". Because the author's beginners COM, if there is something wrong, please be pointed out, grateful!
(2) Prepare the contents of the program group in Windows, in fact, some of the folders and files in a specific directory (generally in the C: / Windows / Start Menu / Program Directory, we can use the shell function ShgetspecialFolderPath with csidl_programs parameters Get the storage directory of the program group), Windows dynamically creates the start menu and the submenus under these contents. The folder represents a pop-up menu, and the file is a shortcut (.lnk file), so you have to create a shortcut in the program group, actually to create some .lnk files in this specific directory, fortunately, WINDOWS provides us with This interface is located. (Of course, you have to study. LNK file structure then fill in one byte by one byte, and I don't oppose it) These interface functions are part of the so-called shell function. The shell function is actually a function of some Windows built-in COM object interface. Therefore, you should know some details about COM, you can take a look at Pan Aimin's "COM principle and application". And the interface we have to use is iShellLink (some information to set the shortcut to build) and IPERSISTFILE (to save shortcuts in the file form) (all of the shelllink object), first we can create one with the COCREATEINSTANCE library function CoM object called ShellLink, and get one of the interface pointers, and query another interface with QueryInterface. Detailed implementation and use of interface functions, please see the following code.
(3) This is my implementation, pay attention to the use of COM library functions, call Coinitialize () initialization in advance, and call Couninitialize before the program exits
// badd = 1 indicates the addition shortcut, BADD = 0 means deleting this shortcut // This example is "Auto Run .Lnk", created in the boot group BOOL CTESTDLG :: setAutorun (bool badd) {HRESULT Hres; ishelllink * psl; // ishelllink interface pointer BOOL BOOL BRET = false; char pszdespath [max_path]; // Created target path char pszshiotcutfile [max_path]; // created source file
:: getModuleFileName (NULL, PSZSHORTCUTFILE, MAX_PATH); // Get this program path ShgetSpecialFolderPath (m_hwnd, pszdespath, csidl_programs, 0); // program group path strcat (pszdespath, "// start / automatic run .lnk"); // start group if return DeleteFile (pszDesPath) (bAdd!); hres = CoCreateInstance (CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void **) & psl); // get the object if COM interfaces IShellLink CLSID_ShellLink identified (the SUCCEEDED! (hres)) goto error; ipersistfile * ppf; // ipersistfile interface pointer // Query the iPersistFile interface for shortcut storage operation hres = psl-> queryinterface (IID_IPERSISTFILE, (VOID **) & PPF); if (!! " HRES)) Goto Error; Word WSZ [MAX_PATH]; // Unicode String Buffer Address / / To Adapt to the COM Standard Be sure to Use the Source File Address HRES = PSL-> setPath (pszshcutfile); if (! successdeed) (hres)) Goto error; // Setting parameters hres = psl-> setarguments ("/ argumentshere"); if (! successted (hres)) goto error; // Set shortcuts HRES = PSL-> setDescription (" Shortcut to screencolor "); if (! Succeeded (hres)) goto err OR; // convert the ANSI string into a Unicode string multibytetowideChar (CP_ACP, 0, PSZDesPATH, -1, WSZ, Max_Path); // Call the Save method to store HRES = PPF-> Save (WSZ, TRUE); IF "Succeeded (hres)) goto error; bret = true;
Error: // Release the interface PPF-> Release (); psl-> release ();
Return True;}