?
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();
}