typedef ULONG
(WINAPI* pfnSHChangeNotifyRegister)
(
 HWND hWnd,
 int  fSource,
 LONG fEvents,
 UINT wMsg,
 int  cEntries,
 SHChangeNotifyEntry* pfsne 
 );pfnSHChangeNotifyRegister m_pfnRegister;SHChangeNotifyEntry shEntry = {0};
shEntry.fRecursive = TRUE;
shEntry.pidl = 0;
m_ulNotifyId = 0;

//注册Shell监视函数
m_ulNotifyId = m_pfnRegister(GetSafeHwnd(),
        SHCNRF_InterruptLevel|SHCNRF_ShellLevel,
        SHCNE_ALLEVENTS,
        WM_USERDEF_FILECHANGED, //自定义消息
        1,
        &shEntry
);
//自定义消息函数
LRESULT CShellDlg::OnFileChanged(WPARAM wParam, LPARAM lParam)
{
UpdateData();
    CString  strOriginal = _T("");
SHNotifyInfo* pShellInfo = (SHNotifyInfo*)wParam;

strOriginal = GetPathFromPIDL(pShellInfo->dwItem1);
if(strOriginal.IsEmpty())
{
return NULL;
}

switch(lParam)
{
case SHCNE_CREATE:
{
m_strLog += strOriginal;
m_strLog += "\r\n";
}
break;
case SHCNE_DELETE:
m_strLog += strOriginal;
m_strLog += "\r\n";
break;
case SHCNE_RENAMEITEM:
{
m_strLog += strOriginal;
m_strLog += "\r\n";
}
break;
    }
UpdateData(FALSE);    return NULL; 
}
当复制或重命名文件的时候,如何得到源文件路径?pShellInfo->dwItem2一直是空的,何解啊?

解决方案 »

  1.   

    重命名的时候,dwItem1是源文件,dwItem2是目标文件。
    改变通知事件中不包含复制文件,复制文件时只有SHCNE_CREATE事件。
      

  2.   

    重命名的时候,dwItem1是源文件,dwItem2是目标文件。 
    改变通知事件中不包含复制文件,复制文件时只有SHCNE_CREATE事件。
      

  3.   

    重命名的时候,dwItem1是源文件,dwItem2是目标文件。
    -------------------------------------------------
    按我测试的结果来看,只能得到dwItem1,而且dwItem1是重命名后的文件名。dwItem2一直是空的。是不是我哪里用错了?
      

  4.   

    To cnzdgs:
    我用VC6做了个很简单的demo,能否麻烦你给我看一下?留个邮箱,我发给你,谢谢!
      

  5.   

    从你的代码中看不出错误,按你的说法感觉是SHCNE_RENAMEITEM没定义对,应该是:
    #define SHCNE_RENAMEITEM          0x00000001L
    如果你的VC+SDK是2000年以后的版本,shlobj.h中有相关定义。
      

  6.   

    你先按我上面说的检查一下,如果找不出问题再发给我,[email protected]。我一会有事,下午才能看。
      

  7.   

    先把事件类型定义贴给你参考一下。
    #define SHCNE_RENAMEITEM          0x00000001L
    #define SHCNE_CREATE              0x00000002L
    #define SHCNE_DELETE              0x00000004L
    #define SHCNE_MKDIR               0x00000008L
    #define SHCNE_RMDIR               0x00000010L
    #define SHCNE_MEDIAINSERTED       0x00000020L
    #define SHCNE_MEDIAREMOVED        0x00000040L
    #define SHCNE_DRIVEREMOVED        0x00000080L
    #define SHCNE_DRIVEADD            0x00000100L
    #define SHCNE_NETSHARE            0x00000200L
    #define SHCNE_NETUNSHARE          0x00000400L
    #define SHCNE_ATTRIBUTES          0x00000800L
    #define SHCNE_UPDATEDIR           0x00001000L
    #define SHCNE_UPDATEITEM          0x00002000L
    #define SHCNE_SERVERDISCONNECT    0x00004000L
    #define SHCNE_UPDATEIMAGE         0x00008000L
    #define SHCNE_DRIVEADDGUI         0x00010000L
    #define SHCNE_RENAMEFOLDER        0x00020000L
    #define SHCNE_FREESPACE           0x00040000L
      

  8.   

    谢谢cnzdgs。
    我没有自定义事件类型,把上面的考过去后,重命名是可以得到dwItem1和dwItem2了。
    请问如果是复制,能不能用Shell的相关函数得到源文件名,在delphi中,通过TForm的SHEentName可以得到复制的源。在VC6中可不可以实现。
    另:麻烦对10L中的各个事件类型注释一下,感谢。下班前结贴,大家多帮忙。
      

  9.   

    ChangeNotify中没有复制文件这个事件,不清楚要怎么得到复制的源。以下是MSDN中对各种事件的解释:
    SHCNE_ALLEVENTS
    All events have occurred. 
    SHCNE_ASSOCCHANGED
    A file type association has changed. SHCNF_IDLIST must be specified in the uFlags parameter. dwItem1 and dwItem2 are not used and must be NULL. 
    SHCNE_ATTRIBUTES
    The attributes of an item or folder have changed. SHCNF_IDLIST or SHCNF_PATH must be specified in uFlags. dwItem1 contains the item or folder that has changed. dwItem2 is not used and should be NULL.
    SHCNE_CREATE
    A nonfolder item has been created. SHCNF_IDLIST or SHCNF_PATH must be specified in uFlags. dwItem1 contains the item that was created. dwItem2 is not used and should be NULL.
    SHCNE_DELETE
    A nonfolder item has been deleted. SHCNF_IDLIST or SHCNF_PATH must be specified in uFlags. dwItem1 contains the item that was deleted. dwItem2 is not used and should be NULL. 
    SHCNE_DRIVEADD
    A drive has been added. SHCNF_IDLIST or SHCNF_PATH must be specified in uFlags. dwItem1 contains the root of the drive that was added. dwItem2 is not used and should be NULL. 
    SHCNE_DRIVEADDGUI
    A drive has been added and the Shell should create a new window for the drive. SHCNF_IDLIST or SHCNF_PATH must be specified in uFlags. dwItem1 contains the root of the drive that was added. dwItem2 is not used and should be NULL. 
    SHCNE_DRIVEREMOVED
    A drive has been removed. SHCNF_IDLIST or SHCNF_PATH must be specified in uFlags. dwItem1 contains the root of the drive that was removed. dwItem2 is not used and should be NULL. 
    SHCNE_EXTENDED_EVENT
    Not currently used. 
    SHCNE_FREESPACE
    The amount of free space on a drive has changed. SHCNF_IDLIST or SHCNF_PATH must be specified in uFlags. dwItem1 contains the root of the drive on which the free space changed. dwItem2 is not used and should be NULL. 
    SHCNE_MEDIAINSERTED
    Storage media has been inserted into a drive. SHCNF_IDLIST or SHCNF_PATH must be specified in uFlags. dwItem1 contains the root of the drive that contains the new media. dwItem2 is not used and should be NULL. 
    SHCNE_MEDIAREMOVED
    Storage media has been removed from a drive. SHCNF_IDLIST or SHCNF_PATH must be specified in uFlags. dwItem1 contains the root of the drive from which the media was removed. dwItem2 is not used and should be NULL. 
    SHCNE_MKDIR
    A folder has been created. SHCNF_IDLIST or SHCNF_PATH must be specified in uFlags. dwItem1 contains the folder that was created. dwItem2 is not used and should be NULL. 
    SHCNE_NETSHARE
    A folder on the local computer is being shared via the network. SHCNF_IDLIST or SHCNF_PATH must be specified in uFlags. dwItem1 contains the folder that is being shared. dwItem2 is not used and should be NULL. 
    SHCNE_NETUNSHARE
    A folder on the local computer is no longer being shared via the network. SHCNF_IDLIST or SHCNF_PATH must be specified in uFlags. dwItem1 contains the folder that is no longer being shared. dwItem2 is not used and should be NULL. 
    SHCNE_RENAMEFOLDER
    The name of a folder has changed. SHCNF_IDLIST or SHCNF_PATH must be specified in uFlags. dwItem1 contains the previous pointer to an item identifier list (PIDL) or name of the folder. dwItem2 contains the new PIDL or name of the folder. 
    SHCNE_RENAMEITEM
    The name of a nonfolder item has changed. SHCNF_IDLIST or SHCNF_PATH must be specified in uFlags. dwItem1 contains the previous PIDL or name of the item. dwItem2 contains the new PIDL or name of the item. 
    SHCNE_RMDIR
    A folder has been removed. SHCNF_IDLIST or SHCNF_PATH must be specified in uFlags. dwItem1 contains the folder that was removed. dwItem2 is not used and should be NULL. 
    SHCNE_SERVERDISCONNECT
    The computer has disconnected from a server. SHCNF_IDLIST or SHCNF_PATH must be specified in uFlags. dwItem1 contains the server from which the computer was disconnected. dwItem2 is not used and should be NULL. 
    SHCNE_UPDATEDIR
    The contents of an existing folder have changed, but the folder still exists and has not been renamed. SHCNF_IDLIST or SHCNF_PATH must be specified in uFlags. dwItem1 contains the folder that has changed. dwItem2 is not used and should be NULL. If a folder has been created, deleted, or renamed, use SHCNE_MKDIR, SHCNE_RMDIR, or SHCNE_RENAMEFOLDER, respectively, instead. 
    SHCNE_UPDATEIMAGE
    An image in the system image list has changed. SHCNF_DWORD must be specified in uFlags. 
    Windows NT/2000/XP: dwItem2 contains the index in the system image list that has changed. dwItem1 is not used and should be NULL.Windows 95/98: dwItem1 contains the index in the system image list that has changed. dwItem2 is not used and should be NULL.SHCNE_UPDATEITEM
    An existing nonfolder item has changed, but the item still exists and has not been renamed. SHCNF_IDLIST or SHCNF_PATH must be specified in uFlags. dwItem1 contains the item that has changed. dwItem2 is not used and should be NULL. If a nonfolder item has been created, deleted, or renamed, use SHCNE_CREATE, SHCNE_DELETE, or SHCNE_RENAMEITEM, respectively, instead. 
    SHCNE_DISKEVENTS
    Specifies a combination of all of the disk event identifiers. 
    SHCNE_GLOBALEVENTS
    Specifies a combination of all of the global event identifiers. 
    SHCNE_INTERRUPT
    The specified event occurred as a result of a system interrupt. As this value modifies other event values, it cannot be used alone.