tags:

views:

392

answers:

4

I need the reference system32/shell32.dll as I use some shell functions to read out the recycling bin. I tried "Add Reference --> COM --> Microsoft Shell Controls and Automatation" and "Add Reference --> Browse ---> [going to the system32/shell32.dll directly]. Both adds the shell32 reference to my references. But when I look at the properties, I see the path of the reference looks like this: "C:\Users\Tim\Documents\Visual Studio 2008\Projects\Wing\FileWing\obj\Debug\Interop.Shell32.dll" ...

I'll not deploy this \obj\Debug\ path to my installer. So how can I reference the end-users shell32.dll directly? Is there a way? Why does VS2008 create this strange path? Can I change this path so it doesn't sit in this strange subfolder?

+5  A: 

I believe you are looking for P/Invoke (Platform Invoke)

Once you get the method for including and using the DLLs down, you can visit pinvoke.net to get specific code snippets for using certain methods.

Justin Niessner
Yep, you don't add a reference to shell32.dll (at least through the normal Add References), you use P/Invoke instead. There's a whole website dedicated to it even, http://www.pinvoke.net/
Chris Haas
Thank you, I guess this is it. Thanks a lot!
Akku
Okay, I think I'm seeking for something - easier. Wrote anopther comment below, please revisit that ... I'm pretty helpless.
Akku
A: 

After you add the dll reference using VS 2008, you can open the properties for the .dll.

Make sure Copy Local is set to True.

If that doesn't work another solution is to add the .dll as an item to you project, and make is as content, and tell it to copy to the output directory.

David Basarab
+2  A: 

Are you just using DllImport to access functionality in shell32/kernel32? If so, you don't need to add a reference.

For example:

[DllImport("KERNEL32.DLL", EntryPoint="MoveFileW",  SetLastError=true,
CharSet=CharSet.Unicode, ExactSpelling=true,
CallingConvention=CallingConvention.StdCall)]
public static extern bool MoveFile(String src, String dst);

Here's a tutorial on using platform invoke and here's an MSDN article.

Jason
A: 

Hmmm. Okay after revisiting PInvoke, I'm sure that I don't quite get it :-/

Let me illustrate the code I need to handle. I'm digging though the recycling bin and seek for a item that I want to recover. Is there any way NOT fighting though the PInvoke to get this done?

    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;
        }
    }
Akku