Special Folders Startmenü und Desktop



  • Hallo,
    ich habe folgendes Problem. Der Code unten kann zwar ein Shortcut auf dem Desktop erzeugen, aber nicht im Startmenü. Verwirrend ist hierbei das
    LPITEMIDLIST DesktopPidl;
    zwar den Desktopordner hierbei findet

    if(FAILED(SHGetSpecialFolderLocation(NULL, SpecialFolder , &DesktopPidl)))
    return;

    if(FAILED(SHGetSpecialFolderLocation(NULL, SpecialFolder , &StartMenu)))
    return;

    if(!SHGetPathFromIDList(DesktopPidl, wcDesktopDir))
    {
    ShellMalloc->Free(DesktopPidl);
    ShellMalloc->Release();
    return;
    }

    aber CSIDL_COMMON_PROGRAMS als Parameter erzeugt einen Fehler.

    Hier der gesamte Quelltext

    void TForm1::CreateShortCut(int SpecialFolder, AnsiString file,
    							 AnsiString Path,AnsiString Description,
    							 AnsiString WorkDir,AnsiString Args)
    {
        // IShellLink allows us to create the shortcut. IPersistFile allows us
        // to save the link to the hard drive.
        IShellLink*   pLink;
        IPersistFile* pPersistFile;
        LPMALLOC      ShellMalloc;
    	LPITEMIDLIST  DesktopPidl;
    	char DesktopDir[MAX_PATH];
    	wchar_t wcDesktopDir[MAX_PATH];
    	// Übersetzungs - Strings
    	int iCount;
    	char cFile[MAX_PATH];
    	wchar_t wcFile[MAX_PATH];
    	char cPath[MAX_PATH];
    	wchar_t wcPath[MAX_PATH];
    	char cDescription[260];
    	wchar_t wcDescription[260];
    	char cWorkDir[MAX_PATH];
    	wchar_t wcWorkDir[MAX_PATH];
    	char cArgs[260];
    	wchar_t wcArgs[260];
    
    	memset(cPath, '\0', MAX_PATH);
    	memset(wcPath, '\0', MAX_PATH);
    	memset(cDescription, '\0', 260);
    	memset(wcDescription, '\0', 260);
    	memset(cWorkDir, '\0', MAX_PATH);
    	memset(wcWorkDir, '\0', MAX_PATH);
    	memset(cArgs, '\0', 260);
    	memset(wcArgs, '\0', 260);
    	memset(cFile, '\0', MAX_PATH);
    	memset(wcFile, '\0', MAX_PATH);
    
    	strcpy (cFile, file.c_str());
    
    	for (iCount = 0; iCount < file.Length(); iCount++) {
    		wcFile[iCount] = cFile[iCount];
    	}
    
    	strcpy(cPath, Path.c_str());
    
    	for (iCount = 0; iCount < Path.Length(); iCount++) {
    		wcPath[iCount] = cPath[iCount];
    	}
    
    	strcpy(cDescription, Description.c_str());
    
    	for (iCount = 0; iCount < Description.Length(); iCount++) {
            wcDescription[iCount] = cDescription[iCount];
    	}
    
    	strcpy(cWorkDir, WorkDir.c_str());
    
    	for (iCount = 0; iCount < WorkDir.Length(); iCount++) {
    		wcWorkDir[iCount] = cWorkDir[iCount];
    	}
    
    	strcpy(cArgs, Args.c_str());
    
    	for (iCount = 0; iCount < Args.Length(); iCount++) {
    		wcArgs[iCount] = cArgs[iCount];
    	}
    
        if(FAILED(SHGetMalloc(&ShellMalloc)))
            return;
    
    	if(FAILED(SHGetSpecialFolderLocation(NULL, SpecialFolder , &DesktopPidl)))
    		return;
    
    	if(FAILED(SHGetSpecialFolderLocation(NULL, SpecialFolder , &StartMenu)))
    		return;
    
    //CSIDL_COMMON_PROGRAMS
    
    	if(!SHGetPathFromIDList(DesktopPidl, wcDesktopDir))
    	{
    		ShellMalloc->Free(DesktopPidl);
    		ShellMalloc->Release();
    		return;
    	}
    
    	if(!SHGetPathFromIDList(CSIDL_COMMON_PROGRAMS, wcDesktopDir))
    	{
    		ShellMalloc->Free(DesktopPidl);
    		ShellMalloc->Release();
    		return;
    	}
    
    	for (iCount = 0; iCount < MAX_PATH; iCount++) {
    		DesktopDir[iCount] = wcDesktopDir[iCount];
    	}
    
    	ShellMalloc->Free(DesktopPidl);
    	ShellMalloc->Release();
    
        // First, we have to initialize the COM library
        if(SUCCEEDED(CoInitialize(NULL)))
        {
            // If CoInitialize doesn't fail, then instantiate an IShellLink object
            // by calling CoCreateInstance.
            if(SUCCEEDED(CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
                                         IID_IShellLink, (void **) &pLink)))
            {
                // if that succeeds, then fill in the shortcut attributes
    //			pLink->SetPath(wcPath);
    			pLink->SetPath(wcPath);
                pLink->SetDescription(wcDescription);
                pLink->SetWorkingDirectory(wcWorkDir);
                pLink->SetArguments(wcArgs);
                pLink->SetShowCmd(SW_SHOW);
    
                // Now we need to save the shortcut to the hard drive. The IShellLink
                // object also doubles as an IPersistFile object. Fetch the IPersistFile
                // part of the object by calling QueryInterface.
                if (SUCCEEDED (pLink->QueryInterface(IID_IPersistFile, (void **) &pPersistFile)))
                {
                    // If that succeeds, then call the Save method of the
                    // IPersistFile object to write the shortcut to the desktop.
                    WideString strShortCutLocation(DesktopDir);
                    strShortCutLocation += "\\";
                    strShortCutLocation += file.c_str();
                    pPersistFile->Save(strShortCutLocation.c_bstr(), TRUE);
    				pPersistFile->Release();
                }
                pLink->Release();
    		}
    
            // Calls to CoInitialize need a corresponding CoUninitialize call
            CoUninitialize();
        }
    }
    

  • Mod

    Gib uns eine Chance :
    Was für eine Fehler und wo?



  • Hallo,
    habe den Fehler schon gefunden. Hab leider das Übergabeargument übersehen, dieses legt ja fest, welcher Spezialordner gemeint ist.
    Also einmal ein Auftruf mit CSIDL_DESKTOP
    und einmal mit CSIDL_COMMON_PROGRAMS funktioniert.

    void TForm1::CreateShortCut(int SpecialFolder, AnsiString file,
    							 AnsiString Path,AnsiString Description,
    							 AnsiString WorkDir,AnsiString Args)
    {
        // IShellLink allows us to create the shortcut. IPersistFile allows us
        // to save the link to the hard drive.
        IShellLink*   pLink;
        IPersistFile* pPersistFile;
    	LPMALLOC      ShellMalloc;
    	LPITEMIDLIST  LinkPidl;
    	char LinkDir[MAX_PATH];
    	wchar_t wcLinkDir[MAX_PATH];
    //	char DesktopDir[MAX_PATH];
    //	wchar_t wcDesktopDir[MAX_PATH];
    
    	// Übersetzungs - Strings
    	int iCount;
    	char cFile[MAX_PATH];
    	wchar_t wcFile[MAX_PATH];
    	char cPath[MAX_PATH];
    	wchar_t wcPath[MAX_PATH];
    	char cDescription[260];
    	wchar_t wcDescription[260];
    	char cWorkDir[MAX_PATH];
    	wchar_t wcWorkDir[MAX_PATH];
    	char cArgs[260];
    	wchar_t wcArgs[260];
    
    	memset(cPath, '\0', MAX_PATH);
    	memset(wcPath, '\0', MAX_PATH);
    	memset(cDescription, '\0', 260);
    	memset(wcDescription, '\0', 260);
    	memset(cWorkDir, '\0', MAX_PATH);
    	memset(wcWorkDir, '\0', MAX_PATH);
    	memset(cArgs, '\0', 260);
    	memset(wcArgs, '\0', 260);
    	memset(cFile, '\0', MAX_PATH);
    	memset(wcFile, '\0', MAX_PATH);
    
    	strcpy (cFile, file.c_str()); // Von AnsiString zum Char-Array
    
    	// Vom Char-Array ins wide char-Array kopieren
    	for (iCount = 0; iCount < file.Length(); iCount++) {
    		wcFile[iCount] = cFile[iCount];
    	}
    
    	strcpy(cPath, Path.c_str()); // Von AnsiString zum Char-Array
    
    	// Vom Char-Array ins wide char-Array kopieren usw....
    	for (iCount = 0; iCount < Path.Length(); iCount++) {
    		wcPath[iCount] = cPath[iCount];
    	}
    
    	strcpy(cDescription, Description.c_str());
    
    	for (iCount = 0; iCount < Description.Length(); iCount++) {
            wcDescription[iCount] = cDescription[iCount];
    	}
    
    	strcpy(cWorkDir, WorkDir.c_str());
    
    	for (iCount = 0; iCount < WorkDir.Length(); iCount++) {
    		wcWorkDir[iCount] = cWorkDir[iCount];
    	}
    
    	strcpy(cArgs, Args.c_str());
    
    	for (iCount = 0; iCount < Args.Length(); iCount++) {
    		wcArgs[iCount] = cArgs[iCount];
    	}
    
        if(FAILED(SHGetMalloc(&ShellMalloc)))
            return;
    
    	if(FAILED(SHGetSpecialFolderLocation(NULL, SpecialFolder , &LinkPidl)))
            return;
    
    	if(!SHGetPathFromIDList(LinkPidl, wcLinkDir))
        {
    		ShellMalloc->Free(LinkPidl);
            ShellMalloc->Release();
            return;
    	}
    
    	for (iCount = 0; iCount < MAX_PATH; iCount++) {
    		LinkDir[iCount] = wcLinkDir[iCount];
    	}
    
    	ShellMalloc->Free(LinkPidl);
    	ShellMalloc->Release();
    
        // First, we have to initialize the COM library
        if(SUCCEEDED(CoInitialize(NULL)))
        {
            // If CoInitialize doesn't fail, then instantiate an IShellLink object
            // by calling CoCreateInstance.
            if(SUCCEEDED(CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
                                         IID_IShellLink, (void **) &pLink)))
            {
                // if that succeeds, then fill in the shortcut attributes
    //			pLink->SetPath(wcPath);
    			pLink->SetPath(wcPath);
                pLink->SetDescription(wcDescription);
                pLink->SetWorkingDirectory(wcWorkDir);
                pLink->SetArguments(wcArgs);
                pLink->SetShowCmd(SW_SHOW);
    
                // Now we need to save the shortcut to the hard drive. The IShellLink
                // object also doubles as an IPersistFile object. Fetch the IPersistFile
                // part of the object by calling QueryInterface.
                if (SUCCEEDED (pLink->QueryInterface(IID_IPersistFile, (void **) &pPersistFile)))
                {
                    // If that succeeds, then call the Save method of the
                    // IPersistFile object to write the shortcut to the desktop.
                    WideString strShortCutLocation(LinkDir);
                    strShortCutLocation += "\\";
                    strShortCutLocation += file.c_str();
                    pPersistFile->Save(strShortCutLocation.c_bstr(), TRUE);
    				pPersistFile->Release();
    			}
    			pLink->Release();
    		}
    
    		// Calls to CoInitialize need a corresponding CoUninitialize call
    		CoUninitialize();
    	}
    }
    

Anmelden zum Antworten