Search code examples
c++windows-8windows-installerstartmenu

How to pin Application icon to the metro start screen in windows 8 programmatically


How can I pin application icon to metro start screen in win8 programmatically(c++)? I know how to do it manually. I also know that it will be added automatically once I launch that application.


Solution

  • I found this solution here

    BOOL PinToStart( LPCWSTR szFilePath )
    {
    BOOL bSuccess = FALSE;
    
    // break into file name and path
    WCHAR lpszDirectoryName[ MAX_PATH ] = { 0 };
    LPCWSTR lpszFileName = ::PathFindFileName( szFilePath );
    
    wcscpy_s( lpszDirectoryName, szFilePath );
    ::PathRemoveFileSpec( lpszDirectoryName );
    
    // load shell32.dll
    HMODULE hShell32 = LoadLibrary( L"SHELL32" );
    if( hShell32 != NULL )
    {
        // get the localized translation of 'Pin to Start' verb
        WCHAR szPinToStartLocalized[ MAX_PATH ] = { 0 };
        int nPinToStartLocalizedLength = LoadString( (HINSTANCE)hShell32, 51201, szPinToStartLocalized, MAX_PATH );
    
        if(  nPinToStartLocalizedLength > 0 )
        {
            // create the shell object
            IShellDispatch *pShellDispatch = NULL;
            HRESULT hr = CoCreateInstance(CLSID_Shell, NULL, CLSCTX_INPROC_SERVER, IID_IShellDispatch, (void**)&pShellDispatch);
            if( SUCCEEDED( hr ) )
            {
                Folder *pFolder = NULL;
                variant_t vaDirectory( lpszDirectoryName );
    
                // get the namespace
                if( SUCCEEDED( pShellDispatch->NameSpace( vaDirectory, &pFolder ) ) )
                {
                    FolderItem *pItem = NULL;
                    bstr_t vaFileName( lpszFileName );
    
                    // parse the name
                    if( SUCCEEDED( pFolder->ParseName( vaFileName, &pItem ) ) )
                    {
                        FolderItemVerbs* pVerbs = NULL;
    
                        // get the verbs
                        if( SUCCEEDED( pItem->Verbs(&pVerbs) ) )
                        {
                            long nCount = 0;
    
                            if( SUCCEEDED ( pVerbs->get_Count( &nCount ) ) )
                            {
                                variant_t vaIndex;
                                vaIndex.vt = VT_I4;
    
                                // iterate through verbs
                                for( vaIndex.lVal = 0; vaIndex.lVal<nCount; vaIndex.lVal++ )
                                {
                                    FolderItemVerb* pVerb = NULL;
    
                                    if( SUCCEEDED( pVerbs->Item( vaIndex, &pVerb ) ) )
                                    {
                                        BSTR bstrVerbName = NULL;
    
                                        // check for 'Pin to Start' verb
                                        if( SUCCEEDED( pVerb->get_Name( &bstrVerbName ) ) )
                                        {
                                            if( 0 == wcscmp( bstrVerbName, szPinToStartLocalized ) )
                                            {
                                                bSuccess = SUCCEEDED( pVerb->DoIt() );
                                                vaIndex.lVal = nCount; // break for
                                            }
    
                                            ::SysFreeString( bstrVerbName );
                                        }
                                        pVerb->Release();
                                    } // if
                                } // for
                            }
                            pVerbs->Release();
                        }
                        pItem->Release();
                    }
                    pFolder->Release();
                }
                pShellDispatch->Release();
            }
        }
    
        ::FreeLibrary( hShell32 );
    }
    
    return bSuccess;
    }
    

    Hope it's help you