Referring again to shell32, C # Visual Studio

Hmmm. Ok after re-looking at PInvoke, I'm sure I didn't quite understand: - / (just asked this question )

Let me illustrate the code I need to process. It works when I use "Add Link → COM → Microsoft Shell Controls and Automatation" ... but, unfortunately, it puts the link in my project, which looks like this: "C: \ Users \ Tim \ Documents \ Visual Studio 2008 \ Projects \ Wing \ FileWing \ OBJ \ Debug \ Interop.Shell32.dll "

I dig in the basket and look for the item that I want to restore. Is there any way NOT to fight through PInvoke to do this? Or get a link to the system32 / shell32.dll file that allows me to use this code at runtime?

private void recoverRecyclerBinEntry(string fileName, int size) { try { Shell Shl = new Shell(); Folder Recycler = Shl.NameSpace(10); // scans through all the recyclers entries till the one to recover has been found for (int i = 0; i < Recycler.Items().Count; i++) { FolderItem FI = Recycler.Items().Item(i); string FileName = Recycler.GetDetailsOf(FI, 0); if (Path.GetExtension(FileName) == "") FileName += Path.GetExtension(FI.Path); //Necessary for systems with hidden file extensions. string FilePath = Recycler.GetDetailsOf(FI, 1); string combinedPath = Path.Combine(FilePath, FileName); if (size == FI.Size && fileName == combinedPath) { Debug.Write("Match found. Restoring " + combinedPath + "..."); Undelete(FI); Debug.WriteLine("done."); } else { Debug.WriteLine("No match"); } } } catch (Exception ex) { Debug.WriteLine(ex.Message); Debug.WriteLine(ex.StackTrace); } } private bool Undelete(FolderItem Item) { try { foreach (FolderItemVerb FIVerb in Item.Verbs()) { if ( (FIVerb.Name.ToUpper().Contains("WIEDERHERSTELLEN")) || (FIVerb.Name.ToUpper().Contains("ESTORE")) || (FIVerb.Name.ToUpper().Contains("NDELETE")) ) { FIVerb.DoIt(); return true; } } //execute the first one: Item.Verbs().Item(0).DoIt(); return true; } catch (Exception) { Debug.WriteLine("ERROR undeleting"); return false; } } 
+6
reference c # visual-studio-2008 shell32
Jan 25 '10 at 16:21
source share
1 answer

Now you are mixing 2 different concepts: PInvoke and COM Interop.

PInvoke allows you to access native C functions from managed code. It works by defining a marshal-compatible signature of its own method in managed code and labeling it with the DllImport attribute. It requires and cannot have a link to the metadata for the native DLL. The DLL is detected at runtime using the usual loading rules for the Win32 DLL.

COM Interop allows you to access COM-compatible objects from managed code. This is done by obtaining a marshal compatible managed definition of the COM interface, and then getting the referee to the object in one of several ways. Obtaining a managed definition is often done by adding a metadata reference to the PIA (primary interop assembly) for the COM component. Prior to C # 4.0, this link could not be deleted without much work and should be deployed with your application.

In this particular example, you are using COM interop, not PInvoke.

+7
Jan 25 '10 at 16:29
source share



All Articles