2

I'm writing code that can update files (how uncommon and exciting !). When a file has been updated I need Windows Explorer to call the overlay extensions again, so that I always have the correct overlays over files. I figured I would do this with some p/invoke trickery (looking how TortoiseSVN is doing it), but I always get error 14007 and cant' find why. This is the code :

IntPtr ptr = Marshal.StringToHGlobalUni(FullName);
Shell.SHChangeNotify(SHCNE_UPDATEITEM, SHCNF_PATH | SHCNF_FLUSH, ptr, IntPtr.Zero);
Win32Exception w = new Win32Exception(Marshal.GetLastWin32Error());
LOG.ErrorFormat("Error {0} calling SHChangeNotify : {1}", w.NativeErrorCode, w.Message);
Marshal.FreeHGlobal(ptr)

Fullname is the path of the file that's been updated. I get the following error :

Error 14007 calling SHChangeNotify : The requested lookup key was not found in any active activation context

What am I doing wrong here ?

FYI :

[DllImport("Shell32.dll", CharSet=CharSet.Auto, SetLastError=true)]
public static extern void SHChangeNotify(HChangeNotifyEventID wEventId, HChangeNotifyFlags uFlags, IntPtr dwItem1, IntPtr dwItem2);

SHCNE_UPDATEITEM = 0x00002000
SHCNF_PATH = SHCNF_PATHW = 0x0005
SHCNF_FLUSH = 0x1000
Sébastien Nussbaumer
  • 6,202
  • 5
  • 40
  • 58

1 Answers1

6

The documentation for SHChangeNotify does not mention that the Win32 "last error" code is set on failure. Any value returned by GetLastError is meaningless.

Raymond Chen
  • 44,448
  • 11
  • 96
  • 135
  • Thanks ! I'm a big fan of your blog, it's an honour to have an answer from you :) It looks as though I had a wrong declaration of SHChangeNotify. Since the call wouldn't have any effect on an XP machine I figured that calling GetLastError would give me a hint to why the overlays aren't updated. I you have any idea that would be great ! Thanks again – Sébastien Nussbaumer Nov 08 '11 at 14:22
  • Ok, got it, I gave an incomplete path to SHChangeNotify (I was missing the drive letter ... which can come in quite handy here) ... argh ! – Sébastien Nussbaumer Nov 08 '11 at 14:39