Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Try this code out. Sorry for the length, but it's all needed to properly PInvoke this particular function. It's a simple console application that includes a definition for both functions and an example usage of SHGetKnownFolderPath. </p> <p>I went ahead and included the definitions for KNOWN_FOLDER_FLAG as well as a few definitions for the folder ID's. All of the folder Id's are actually just GUIDs. All of the possible ID's can be found at %ProgramFiles%\Windows SDK\v6.0A\Include\KnownFolders.h and added in the same manner that I added in the sample.</p> <p>I included several wrapper functions that hide all of the evil marashal'ing details for calling the particular functions.</p> <p>If there is any particular folder id you'd like or explanation please add a comment and I'll update the sample. </p> <p><strong>EDIT</strong> Corrected a mistake in the Marshalling of SHSetKnownFolderPath. I did not add a MarshalAs tag to the String value and it was defaulting to an ANSI string. The API required unicode. The SHSetFolderFunction now works (confirmed with RecentFolder)</p> <pre><code>Imports System.Runtime.InteropServices Module NativeMethods Public Enum KNOWN_FOLDER_FLAG '''KF_FLAG_CREATE -&gt; 0x00008000 KF_FLAG_CREATE = 32768 '''KF_FLAG_DONT_VERIFY -&gt; 0x00004000 KF_FLAG_DONT_VERIFY = 16384 '''KF_FLAG_DONT_UNEXPAND -&gt; 0x00002000 KF_FLAG_DONT_UNEXPAND = 8192 '''KF_FLAG_NO_ALIAS -&gt; 0x00001000 KF_FLAG_NO_ALIAS = 4096 '''KF_FLAG_INIT -&gt; 0x00000800 KF_FLAG_INIT = 2048 '''KF_FLAG_DEFAULT_PATH -&gt; 0x00000400 KF_FLAG_DEFAULT_PATH = 1024 '''KF_FLAG_NOT_PARENT_RELATIVE -&gt; 0x00000200 KF_FLAG_NOT_PARENT_RELATIVE = 512 '''KF_FLAG_SIMPLE_IDLIST -&gt; 0x00000100 KF_FLAG_SIMPLE_IDLIST = 256 '''KF_FLAG_ALIAS_ONLY -&gt; 0x80000000 KF_FLAG_ALIAS_ONLY = &amp;H80000000 End Enum Public ComputerFolder As Guid = New Guid("0AC0837C-BBF8-452A-850D-79D08E667CA7") Public DesktopFolder As Guid = New Guid("B4BFCC3A-DB2C-424C-B029-7FE99A87C641") Public DocumentsFolder As Guid = New Guid("FDD39AD0-238F-46AF-ADB4-6C85480369C7") &lt;DllImport("shell32.dll")&gt; _ Public Function SHGetKnownFolderPath( _ ByRef folderId As Guid, _ ByVal flags As UInteger, _ ByVal token As IntPtr, _ &lt;Out()&gt; ByRef pathPtr As IntPtr) As Integer End Function &lt;DllImport("shell32.dll")&gt; _ Public Function SHSetKnownFolderPath( _ ByRef folderId As Guid, _ ByVal flags As UInteger, _ ByVal token As IntPtr, _ &lt;[In](), MarshalAs(UnmanagedType.LPWStr)&gt; ByVal path As String) As Integer End Function Public Function SHGetKnownFolderPathWrapper(ByVal folderId As Guid) As String Return SHGetKnownFolderPathWrapper(folderId, 0) End Function Public Function SHGetKnownFolderPathWrapper( _ ByVal folderId As Guid, _ ByVal flags As KNOWN_FOLDER_FLAG) As String Dim ptr = IntPtr.Zero Dim path = String.Empty Try Dim ret = SHGetKnownFolderPath(folderId, CUInt(flags), IntPtr.Zero, ptr) If ret &lt;&gt; 0 Then Throw Marshal.GetExceptionForHR(ret) End If path = Marshal.PtrToStringUni(ptr) Finally Marshal.FreeCoTaskMem(ptr) End Try Return path End Function Public Sub SHSetKnownFolderPathWrapper( _ ByVal folderId As Guid, _ ByVal path As String) SHSetKnownFolderPathWrapper(folderId, 0, path) End Sub Public Sub SHSetKnownFolderPathWrapper( _ ByVal folderId As Guid, _ ByVal flags As KNOWN_FOLDER_FLAG, _ ByVal path As String) Dim ret = SHSetKnownFolderPath(folderId, CUInt(flags), IntPtr.Zero, path) If ret &lt;&gt; 0 Then Throw Marshal.GetExceptionForHR(ret) End If End Sub End Module Module Module1 Sub Main() Dim path = SHGetKnownFolderPathWrapper(NativeMethods.DesktopFolder) Console.WriteLine(path) End Sub End Module </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload