Chinaunix首页 | 论坛 | 博客
  • 博客访问: 756135
  • 博文数量: 128
  • 博客积分: 7079
  • 博客等级: 少将
  • 技术积分: 1326
  • 用 户 组: 普通用户
  • 注册时间: 2006-03-16 08:53
文章分类

全部博文(128)

文章存档

2011年(3)

2010年(12)

2009年(9)

2008年(23)

2007年(61)

2006年(20)

我的朋友

分类: WINDOWS

2007-12-10 17:53:32

先介绍一下这两个类:

IShellLink


The IShellLink interface allows Shell links to be created, modified, and resolved.

Methods

IShellLink supports the following methods:

Retrieves the command-line arguments associated with a Shell link object.
Retrieves the description string for a Shell link object.
Retrieves the hot key for a Shell link object.
Retrieves the location (path and index) of the icon for a Shell link object.
Retrieves the list of item identifiers for a Shell link object.
Retrieves the path and file name of a Shell link object.
Retrieves the show (SW_) command for a Shell link object.
Retrieves the name of the working directory for a Shell link object.
Resolves a Shell link by searching for the Shell link object and updating the Shell link path and its list of identifiers (if necessary).
Sets the command-line arguments associated with a Shell link object.
Sets the description string for a Shell link object.
Sets the hot key for a Shell link object.
Sets the location (path and index) of the icon for a Shell link object.
Sets the list of item identifiers for a Shell link object.
Sets the path and file name of a Shell link object.
Sets the relative path for a Shell link object.
Sets the show (SW_) command for a Shell link object.
Sets the name of the working directory for a Shell link object.

Remarks

Note: The IShellLink interface has an ANSI version (IShellLinkA) and a Unicode version (IShellLinkW). The version that will be used depends on whether you compile for ANSI or Unicode. However, Microsoft® Windows 95 and Microsoft® Windows 98 only support IShellLinkA.

IPersistFile

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.

The IPersistFile interface inherits its definition from , so all implementations must also include the GetClassID method of IPersist.

When to Implement

Implement IPersistFile when you want to read or write information from a separate file, which could be of any file format.

This interface should be implemented on any objects that support linking through a file moniker, including the following:

  • Any object that supports links to its files or to pseudo-objects within its files.
  • A container application that supports links to objects within its compound file.

Typically, you implement the IPersistFile interface as part of an aggregate object that includes other interfaces that are appropriate for the type of moniker binding that is supported.

For example, in either of the cases mentioned above, the moniker for the linked object can be a composite moniker. In the first case, a composite moniker identifies the pseudo-object contained within the file. In the second case, a composite moniker identifies the embedded object contained within the compound file. In either case of composite monikers, you must implement the IPersistFile interface as part of the same object on which the IOleItemContainer interface is implemented. Then, when the application for the linked object is run, OLE queries for the IOleItemContainer interface to locate the embedded object or the pseudo-object contained in the file.

As another example, if the moniker is a simple file moniker (i.e., the link is to the entire file), OLE queries for the interface that the initiator of the bind operation requested. Typically, this is one of the compound document interfaces, such as IOleObject, , or .

When to Use

Call methods in the IPersistFile interface to load or save a linked object in a specified file.

When IPersistFile is implemented on an object that supports linking through a file moniker and the application for the linked object is run, OLE calls . Once the file is loaded, OLE calls IPersistFile::QueryInterface to get another interface pointer to the loaded object. The IPersistFile interface is typically part of an aggregate object that offers other interfaces.

In this case, the only IPersistFile method that OLE calls is the Load method to load a file linked to a container, running the application associated with the file. It would also be unusual for applications to call other methods in this case, which support saving an object to a file. Generally, it is left to the end user and the application for the linked object to decide when to save the object. This differs from the situation for an embedded object, in which the container application uses the interface to provide the storage and to tell the object when to save itself.

Methods in Vtable Order

Methods Description
Returns pointers to supported interfaces.
Increments the reference count.
Decrements the reference count.
Method Description
Returns the class identifier (CLSID) for the component object.
IPersistFile Methods Description
Checks an object for changes since it was last saved to its current file.
Opens the specified file and initializes an object from the file contents.
Saves the object into the specified file.
Notifies the object that it can revert from NoScribble mode to Normal mode.
Gets the current name of the file associated with the object.

 

  Windows NT/2000/XP: Requires Windows NT 3.1 or later.
  Windows 95/98: Requires Windows 95 or later.
  Header: Declared in objidl.h.

上面是MSDN种两个类的描述,下面看看怎么用吧!
1、创建快捷方式
//创建快捷方式, SourcePath 源文件路径,  lnkPath 快捷方式路径, pParam 快捷方式的参数
BOOL CreateLnk( char * SourcePath,  char * lnkPath, char* pParam )
{
 WORD wsz[ MAX_PATH] ;
 MultiByteToWideChar( CP_ACP, 0, lnkPath, -1, wsz, MAX_PATH) ;
 HRESULT hr = CoInitialize(NULL);
 if (SUCCEEDED(hr))
 {
  IShellLink *pisl;
  hr = CoCreateInstance(CLSID_ShellLink, NULL,
   CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)&pisl);
  if (SUCCEEDED(hr))
  {
   IPersistFile* pIPF;
   
   //这里是我们要创建快捷方式的原始文件地址
   pisl->SetPath( SourcePath );
   pisl->SetArguments( pParam ) ;
   hr = pisl->QueryInterface(IID_IPersistFile, (void**)&pIPF);
   if (SUCCEEDED(hr))
   {
    //这里是我们要创建快捷方式的目标地址
    pIPF->Save( wsz, FALSE);
    pIPF->Release();
   }
   else
   {
    DbgPrint( "call QueryInterface failt " ) ;
    return FALSE ;
   }
   pisl->Release();
  }
  else
  {
   DbgPrint( " call CoCreateInstance failt " ) ;
   return FALSE ;
  }
  CoUninitialize();
 }
 else
 {
  DbgPrint( "inint ConInitialize Failt " ) ;
  return FALSE ;
 }
 return TRUE ;
}
 
2、读取快捷方式文件的一些信息。
HWND hWnd ;
  HRESULT hres ;
  IShellLink  * PShLink ;
  WIN32_FIND_DATA wfd ;
  CString lpszLinkName;
  char   lpszPath[MAX_PATH] = {0};
  char   lpszDescription[MAX_PATH] = {0} ;
  lpszLinkName = f.GetFilePath() ;
  //DbgPrint( lpszLinkName ) ;
  hres = CoInitialize( NULL ) ;
  if ( SUCCEEDED(hres) )
  {
   hres = CoCreateInstance( CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)&PShLink ) ;
   
   if ( SUCCEEDED(hres) )
   {
    IPersistFile * ppf ;
    
    hres = PShLink->QueryInterface(  IID_IPersistFile, (LPVOID*)&ppf  );
    if ( SUCCEEDED(hres) )
    {
     WORD wsz[MAX_PATH] ;
     MultiByteToWideChar( CP_ACP, 0, lpszLinkName, -1, wsz, MAX_PATH ) ;
     hres = ppf->Load( wsz, STGM_READ ) ;
     if ( SUCCEEDED(hres) )
     {
      hres = PShLink->Resolve(  hWnd, SLR_ANY_MATCH|SLR_NO_UI ) ;
      if( SUCCEEDED(hres) )
      {
 
       //这里就可以用类提供的函数得到一些快捷方式的信息了。
       hres = PShLink->GetPath( lpszPath, MAX_PATH, &wfd, SLGP_SHORTPATH ) ;
       CString sFileNameTemp ;
       sFileNameTemp.Format( "%s", lpszPath ) ; //这里我只得到了源路径
      }
      else
       DbgPrint( "Resolve failt " ) ;
     }
     else
      DbgPrint( "load failt " ) ;
     ppf->Release( ) ;
    }
    else
     DbgPrint( "QueryInterface failt " ) ;
    PShLink->Release(  ) ;
   }
   else
    DbgPrint( "coCreateInStance failt " ) ;
  
   CoUninitialize() ;
  }
  else
  {
   DbgPrint( "call CoInitialize failt " ) ;
  }
 
这两个功能的实现都是从网上收集了些代码,自己修改了一下,现在对这两个类有了初步的认识,谁有更深入的理解,欢迎交流。
 
阅读(11659) | 评论(0) | 转发(0) |
0

上一篇:LDAP介绍(二)

下一篇:Why and How to learn C

给主人留下些什么吧!~~