Wie erzeuge ich Link-(*.lnk)Dateien?
-
Eines meiner Programme soll 'Link'-Dateien selber erzeugen. Das Problem ist nur, ich weiß nicht wie man das macht!
Kann mir dabei jemand helfen?
-
I'm back
Also, es gab da mal vor Zeiten einen Beitrag, in welchem ausführlich eine API-Fuktion erklärt wurde mit welcher man solche LNK-Verknüpfungen anlegen kann.
Ohne groß rumzureden schicke ich dir einfach mal den Code:#include <shlobj.h> //---------Typedefs-----------------------------------------------------------// typedef IShellLink *LPSHELLLINK; typedef IPersistFile *LPPERSISTFILE; /******************************************************************************/ /* CreateShortcut: Hilfsfunktion zur Erzeugung eines Shortcuts */ //----------------------------------------------------------------------------// /* Parameter: pszPath - Pfad, auf den der Shortcut verweist */ /* pszArguments - Programmargumente */ /* pszLocation - Wo und unter welchem Namen soll der */ /* Shortcut gespeichert werden? */ /* pszWorkingDir - Arbeitsverzeichnis */ /* pszIcon - Icon */ /* nCmdShow - Wie soll das Applikationsfenster */ /* nach dem Start dargestellt werden? */ /* */ /* Rückgabewert: TRUE - Shortcut wurde erzeugt */ /* FALSE - Fehler */ /******************************************************************************/ BOOL CreateShortcut( LPSTR pszPath, LPSTR pszArguments, LPSTR pszLocation, LPSTR pszWorkingDir, LPSTR pszIcon, int nCmdShow ) { LPSHELLLINK pShellLink; HRESULT hrCoInit; // OLE-Installations-Ergebnis HRESULT hr; // Ergebnis der Objektmethoden hrCoInit = CoInitialize(NULL); // OLE initialisieren hr = CoCreateInstance(CLSID_ShellLink, // ShellLink-Objekt erzeugen NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)&pShellLink); if(SUCCEEDED(hr)) { LPPERSISTFILE pPersistFile; // PersistFile Interface ermitteln -------------------------------------- if(SUCCEEDED(pShellLink->QueryInterface(IID_IPersistFile, (void**)&pPersistFile))) { WORD wsz[MAX_PATH]; // Programmpfad setzen ---------------------------------------------- hr = pShellLink->SetPath(pszPath); // Argumente setzen ------------------------------------------------- if(SUCCEEDED(hr)) hr = pShellLink->SetArguments(pszArguments); // Arbeitsverzeichnis setzen ---------------------------------------- if(SUCCEEDED(hr)) hr = pShellLink->SetWorkingDirectory(pszWorkingDir); // Icon setzen ------------------------------------------------------ if(SUCCEEDED(hr)) hr = pShellLink->SetIconLocation(pszIcon, 0); // ShowCommand setzen ----------------------------------------------- if(SUCCEEDED(hr)) hr = pShellLink->SetShowCmd(nCmdShow); // Pfad setzen, an dem Shortcut abgespeichert werden soll ----------- if(SUCCEEDED(hr)) { MultiByteToWideChar(CP_ACP, 0, pszLocation, -1, wsz, MAX_PATH); hr = pPersistFile->Save(wsz, TRUE); hr = pPersistFile->SaveCompleted(wsz); } //******************************************** // hr = pPersistFileRelease(); //******************************************** } pShellLink->Release(); } // Wenn OLE hier initialisiert wurde, muß es auch hier wieder deinstalliert // werden if(SUCCEEDED(hrCoInit)) CoUninitialize(); return SUCCEEDED(hr); }
Das ist alles was du dafür brauchst
-
Danke für den Code!
-
In Konsole erzeugt man das wie folgt:
//uuid.lib //ole32.lib //im Linker einstellen #include <iostream> #include <string> using namespace std; #include <windows.h> #include <string.h> #include <stdlib.h> #include <objidl.h> #include <objbase.h> #include <shlobj.h> class L { public: string pszLinkFileName; char* pszPath; /* path to file */ INT iMaxPath; /* max number of chars for get operation */ char* pszArguments; /* arguments of call */ INT iMaxArguments; /* max number of chars for get operation */ char* pszWorkingDirectory; /* the working directory */ INT iMaxWorkingDirectory; /* max number of chars for get operation */ char* pszDescription; /* description of link */ INT iMaxDescription; /* max number of chars for get operation */ char* pszIconPath; /* path to icon file */ INT iMaxIconPath; /* max number of chars for get operation */ INT iIcon; /* number of icon */ INT iShowCmd; /* command how to show application */ WORD wHotkey; /* hot key */ L(); virtual ~L(); void DesktopDirectory(); void StartMenu(); void StartUp(); bool ShellCreateLink(); }; L::L(){} L::~L() {} bool L::ShellCreateLink() { BOOL bOk; WCHAR wsz [MAX_PATH]; HRESULT hres; HRESULT hrCoInit; hrCoInit = CoInitialize(NULL); // Initializes the COM library on the current apartment and identifies the concurrency model as single-thread apartment (STA). IShellLinkA * psl; // The IShellLink interface allows shell links to be created, modified, and resolved. IPersistFile * ppf; // The IPersistFile interface provides methods that permit an object to be loaded from or saved to a disk file, rather than a storage object or stream. Because the information needed to open a file varies greatly from one application to another, the implementation of IPersistFile::Load on the object must also open its disk file. bOk = FALSE; hres = CoCreateInstance (CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void**) &psl); if (SUCCEEDED (hres)) { if (pszPath != NULL) psl->SetPath (pszPath); if (pszArguments != NULL) psl->SetArguments (pszArguments); if (pszWorkingDirectory != NULL) psl->SetWorkingDirectory (pszWorkingDirectory); if (pszDescription != NULL) psl->SetDescription (pszDescription); if (pszIconPath != NULL) psl->SetIconLocation (pszIconPath, iIcon); if (iShowCmd != 0) psl->SetShowCmd (iShowCmd); if (wHotkey != 0) psl->SetHotkey (wHotkey); hres = psl->QueryInterface (IID_IPersistFile, (void**) &ppf); if (SUCCEEDED (hres)) { MultiByteToWideChar (CP_ACP, 0, pszLinkFileName.c_str(), -1, wsz, MAX_PATH); // Ensure that the string is Unicode hres = ppf->Save ( wsz, TRUE); hres = ppf->SaveCompleted(wsz); bOk = SUCCEEDED (hres); ppf->Release(); } /* if */ psl->Release (); } /* if */ if (SUCCEEDED (hrCoInit)) CoUninitialize( ); // Closes the COM library on the current apartment, unloads all DLLs loaded by the apartment, frees any other resources that the apartment maintains, and forces all RPC connections on the apartment to close. return (bOk); } /* ShellCreateLink */ //////////////////////////////////////////////////////////////////////////////////// //Pfad für speziellen Windows-Ordner finden: void L::DesktopDirectory() { char pszSpecialFolderLocation[MAX_PATH]; //Pfadangabe für speziellen Windows-Ordner int iFolder = CSIDL_DESKTOPDIRECTORY; //Pfadangabe für Desktop-Objekte BOOL bOk= FALSE; LPITEMIDLIST pidl; LPMALLOC pMalloc; pszLinkFileName = " "; //Initialisierung des Strings *pszSpecialFolderLocation = '\0'; //Initialisierung des Strings if (SUCCEEDED (SHGetMalloc(&pMalloc))) { if(SUCCEEDED (SHGetSpecialFolderLocation(NULL, iFolder, &pidl))) { bOk = SHGetPathFromIDList (pidl, pszSpecialFolderLocation); pMalloc->Free(pidl); } pMalloc->Release(); } pszLinkFileName = pszSpecialFolderLocation; //Pfadangabe für Desktop-Directory } void L::StartMenu() { char pszSpecialFolderLocation[MAX_PATH]; //Pfadangabe für speziellen Windows-Ordner int iFolder = CSIDL_STARTMENU ; //Pfadangabe für StartMenü links über Startbutton BOOL bOk= FALSE; LPITEMIDLIST pidl; LPMALLOC pMalloc; pszLinkFileName = " "; //Initialisierung des Strings *pszSpecialFolderLocation = '\0'; //Initialisierung des Strings if (SUCCEEDED (SHGetMalloc(&pMalloc))) { if(SUCCEEDED (SHGetSpecialFolderLocation(NULL, iFolder, &pidl))) { bOk = SHGetPathFromIDList (pidl, pszSpecialFolderLocation); pMalloc->Free(pidl); } pMalloc->Release(); } pszLinkFileName = pszSpecialFolderLocation; //Pfadangabe für Desktop-Directory } void L::StartUp() { char pszSpecialFolderLocation[MAX_PATH]; //Pfadangabe für speziellen Windows-Ordner int iFolder = CSIDL_STARTUP ; //Pfadangabe für StartMenü links über Startbutton BOOL bOk= FALSE; LPITEMIDLIST pidl; LPMALLOC pMalloc; pszLinkFileName = " "; //Initialisierung des Strings *pszSpecialFolderLocation = '\0'; //Initialisierung des Strings if (SUCCEEDED (SHGetMalloc(&pMalloc))) { if(SUCCEEDED (SHGetSpecialFolderLocation(NULL, iFolder, &pidl))) { bOk = SHGetPathFromIDList (pidl, pszSpecialFolderLocation); pMalloc->Free(pidl); } pMalloc->Release(); } pszLinkFileName = pszSpecialFolderLocation; //Pfadangabe für Desktop-Directory } //////////////////////////////////////////////////////////////////////////////////// int main() { char szName[] = "\\test.lnk"; L link; link.iMaxDescription = 100; link.iMaxArguments = 100; link.iIcon = 0; link.iMaxIconPath = 100; link.iMaxPath = 100; link.iMaxWorkingDirectory = MAX_PATH; link.iShowCmd = SW_NORMAL; link.pszPath = "C:\\Temp\\test.txt"; //Ziel der Verknüpfung link.DesktopDirectory(); link.pszLinkFileName = link.pszLinkFileName + szName; //Name des Link-Files link.pszIconPath =""; link.pszWorkingDirectory = "C:\\Temp"; //Arbeitsverzeichnis link.pszArguments =""; link.pszDescription="TEST"; link.wHotkey = MAKEWORD(VK_NUMPAD0, HOTKEYF_CONTROL ); //Siehe IShellLink::GetHotkey link.ShellCreateLink(); }