Creating shortcuts to files or applications is a convenient way to access them quickly. In Windows, shortcuts are represented by .lnk files. These files contain information about the target file or application and can be customized to suit your needs. In this article, we will explore how to programmatically set the target of a .lnk file using PowerShell or C++.
Using PowerShell
PowerShell is a powerful scripting language that can be used to automate various tasks in Windows. To set the target of a .lnk file using PowerShell, we can utilize the Windows Script Host Object Model.
Here's an example of how to set the target of a .lnk file using PowerShell:
$shell = New-Object -ComObject WScript.Shell
$shortcut = $shell.CreateShortcut("C:\path\to\shortcut.lnk")
$shortcut.TargetPath = "C:\path\to\target.exe"
$shortcut.Save()
In the above code, we first create a new instance of the Windows Script Shell object. We then create a shortcut object using the CreateShortcut method and specify the path to the .lnk file. Next, we set the TargetPath property of the shortcut object to the desired target file or application. Finally, we save the changes using the Save method.
Using C++
If you prefer to use C++ to programmatically set the target of a .lnk file, you can utilize the Windows Shell API. This API provides functions and interfaces to manipulate shell objects, including shortcuts.
Here's an example of how to set the target of a .lnk file using C++:
#include <windows.h>
#include <shlobj.h>
int main()
{
CoInitialize(NULL);
IShellLink* pShellLink = NULL;
HRESULT hr = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&pShellLink);
if (SUCCEEDED(hr))
{
IPersistFile* pPersistFile = NULL;
hr = pShellLink->QueryInterface(IID_IPersistFile, (LPVOID*)&pPersistFile);
if (SUCCEEDED(hr))
{
hr = pShellLink->SetPath(L"C:\\path\\to\\target.exe");
if (SUCCEEDED(hr))
{
hr = pPersistFile->Save(L"C:\\path\\to\\shortcut.lnk", TRUE);
}
pPersistFile->Release();
}
pShellLink->Release();
}
CoUninitialize();
return 0;
}
In the above code, we first initialize the COM library using the CoInitialize function. We then create an instance of the IShellLink interface using the CoCreateInstance function. Next, we obtain the IPersistFile interface from the IShellLink interface using the QueryInterface function.
We set the target path of the shortcut using the SetPath function and save the changes using the Save function of the IPersistFile interface. Finally, we release the interfaces and uninitialize the COM library.
Conclusion
Setting the target of a .lnk file programmatically can be useful when automating tasks or customizing shortcuts. In this article, we explored how to achieve this using PowerShell and C++. By utilizing these techniques, you can easily create shortcuts that point to the desired files or applications.
| Reference | Link |
|---|---|
| Windows Script Host Object Model | https://docs.microsoft.com/en-us/previous-versions//98591fh7(v=vs.85) |
| Windows Shell API | https://docs.microsoft.com/en-us/windows/win32/shell/portal |