Skocz do zawartości

taki sam


Duckling

Recommended Posts

jak żywcem to żywcem :)

The CreateLink function in the following example creates a shortcut. The parameters include a pointer to the name of the file to link to, a pointer to the name of the shortcut that you are creating, and a pointer to the description of the link. The description consists of the string, "Shortcut to filename," where filename is the name of the file to link to.

Because CreateLink calls the CoCreateInstance function, it is assumed that the CoInitialize function has already been called. CreateLink uses the IPersistFile interface to save the shortcut and the IShellLink interface to store the filename and description.

// CreateLink - uses the shell's IShellLink and IPersistFile interfaces
// to create and store a shortcut to the specified object.
// Returns the result of calling the member functions of the interfaces.
// lpszPathObj - address of a buffer containing the path of the object
// lpszPathLink - address of a buffer containing the path where the
// shell link is to be stored
// lpszDesc - address of a buffer containing the description of the
// shell link

HRESULT CreateLink(LPCSTR lpszPathObj,
LPSTR lpszPathLink, LPSTR lpszDesc)
{
HRESULT hres;
IShellLink* psl;

// Get a pointer to the IShellLink interface.
hres = CoCreateInstance(&CLSID_ShellLink, NULL,
CLSCTX_INPROC_SERVER, &IID_IShellLink, &psl);
if (SUCCEEDED(hres)) {
IPersistFile* ppf;

// Set the path to the shortcut target, and add the
// description.
psl->lpVtbl->SetPath(psl, lpszPathObj);

psl->lpVtbl->SetDescription(psl, lpszDesc);

// Query IShellLink for the IPersistFile interface for saving the
// shortcut in persistent storage.
hres = psl->lpVtbl->QueryInterface(psl, &IID_IPersistFile,
&ppf);

if (SUCCEEDED(hres)) {
WORD wsz[MAX_PATH];

// Ensure that the string is ANSI.
MultiByteToWideChar(CP_ACP, 0, lpszPathLink, -1,
wsz, MAX_PATH);

// Save the link by calling IPersistFile::Save.
hres = ppf->lpVtbl->Save(ppf, wsz, TRUE);
ppf->lpVtbl->Release(ppf);
}
psl->lpVtbl->Release(psl);
}
return hres;
}

Link do komentarza
Udostępnij na innych stronach

Znalazłem taki kod, ale on również nie działa:

/*PARAMETERS
   fname_to_create_link  = (e.g.) "c:\\mytextfile.txt"
   lnk_fname = (e.g.) "yourname.lnk"
   */ 

   void CreateLinkThenChangeIcon(LPTSTR fname_to_create_link,
                                 LPTSTR lnk_fname)
   {
   HRESULT hres;
   IShellLink *psl = NULL;
   IPersistFile *pPf = NULL;
   WORD wsz[256];
   TCHAR buf[256];
   int id;
   LPITEMIDLIST pidl;

   hres = CoCreateInstance(  CLSID_ShellLink,

                           NULL,
                           CLSCTX_INPROC_SERVER,
                           IID_IShellLink,
                           (LPVOID*)&psl);
   if(FAILED(hres))
      goto cleanup;
   hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&pPf);
   if(FAILED(hres))
      goto cleanup;
   hres = psl->SetPath(fname_to_create_link);
   if(FAILED(hres))

      goto cleanup;
   //place the shortcut on the desktop
   SHGetSpecialFolderLocation(hwnd, CSIDL_DESKTOP, >pidl);

   SHGetPathFromIDList(pidl, buf);

   lstrcat(buf,"\\");
   lstrcat(buf,lnk_fname);

   MultiByteToWideChar(CP_ACP, 0, buf, -1, wsz, MAX_PATH);

   hres = pPf->Save(wsz, TRUE);

   if(FAILED(hres))

      goto cleanup;

   GetSystemDirectory(buf, 256);

   lstrcat(buf,"\\shell32.dll");

   hres = psl->SetIconLocation(buf, 1);

   if(FAILED(hres))

      goto cleanup;

   hres = psl->GetIconLocation(buf, 256, &id);

   if(FAILED(hres))

      goto cleanup;

   pPf-&Save(wsz, TRUE);

   cleanup:

   if(pPf)

      pPf->Release();

   if(psl)

      psl->Release();

}

Czy potrafi ktoś w prosty sposób utwożyć skrót?

Link do komentarza
Udostępnij na innych stronach

Gotowa funkcja. Tworzy skrót o podanej nazwie "lnk" do objektu "obj" np:
CreateShortCut("E:\\czytaj.txt","C:\\WINDOWS\\Pulpit\\skrót do
czytaj.txt.lnk")


bool CreateShortCut(AnsiString obj,AnsiString lnk)
{
  bool bWynik = false;
  IShellLink *pShellLink;
  ::CoInitialize( NULL );

  if ( SUCCEEDED( ::CoCreateInstance( CLSID_ShellLink, NULL,
                                      CLSCTX_INPROC_SERVER,
                                      IID_IShellLink,
(void**)&pShellLink )))
  {
    IPersistFile *pPersistFile;

    pShellLink->SetPath( obj.c_str() );
    if ( SUCCEEDED( pShellLink->QueryInterface( IID_IPersistFile,
(void**)&pPersistFile )))
    {
      wchar_t wcNazwaSkrotu[MAX_PATH];

      lnk.WideChar( wcNazwaSkrotu, MAX_PATH );
      if ( SUCCEEDED( pPersistFile->Save( wcNazwaSkrotu, true )))
        bWynik = true;
      pPersistFile->Release();
    } pShellLink->Release();
  } 
  ::CoUninitialize();

  return bWynik;
}

Tak propo to tez zywcem z grupy dyskusyjnej. Nie sprawdzałem czy działa ;)

Link do komentarza
Udostępnij na innych stronach

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Gość
Odpowiedz...

×   Wkleiłeś zawartość bez formatowania.   Usuń formatowanie

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Utwórz nowe...