Note that there are some explanatory texts on larger screens.

plurals
  1. POP/Invoke SHSetKnownFolderPath
    text
    copied!<p>EDIT: Scotty2012 and David Morton's answers don't work for me so I have put a bounty on this question. I think I need to change the type of the string to something else before passing it in.</p> <p>I'm not much cop at P/Invoke and I'm struggling with declaring and calling <a href="http://msdn.microsoft.com/en-us/library/bb762249(VS.85).aspx" rel="nofollow noreferrer">SHSetKnownFolderPath</a>. I'm using VB9 but if anyone puts answers in C# I should be able to translate.</p> <p>I have got SHGetKnowFolderPath working. Here is my code.</p> <p>In VB</p> <pre><code>Imports System.Runtime.InteropServices Public Class Form1 &lt;DllImport("shell32.dll")&gt; _ Private Shared Function SHGetKnownFolderPath(&lt;MarshalAs(UnmanagedType.LPStruct)&gt; ByVal rfid As Guid, ByVal dwFlags As UInteger, ByVal hToken As IntPtr, ByRef pszPath As IntPtr) As Integer End Function &lt;DllImport("shell32.dll")&gt; _ Private Shared Function SHSetKnownFolderPath(&lt;MarshalAs(UnmanagedType.LPStruct)&gt; ByVal rfid As Guid, ByVal dwFlags As UInteger, ByVal hToken As IntPtr, ByRef pszPath As IntPtr) As Integer End Function Public Shared ReadOnly Documents As New Guid("FDD39AD0-238F-46AF-ADB4-6C85480369C7") Private Sub ButtonSetDocumentsPath_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSetDocumentsPath.Click Dim pPath As IntPtr = Marshal.StringToCoTaskMemUni(TextBoxPath.Text) If SHSetKnownFolderPath(Documents, 0, IntPtr.Zero, pPath) = 0 Then MsgBox("Set Sucessfully") End If End Sub Private Sub ButtonGetDocumentsPath_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonGetDocumentsPath.Click Dim pPath As IntPtr If SHGetKnownFolderPath(Documents, 0, IntPtr.Zero, pPath) = 0 Then Dim s As String = Marshal.PtrToStringUni(pPath) Marshal.FreeCoTaskMem(pPath) TextBoxPath.Text = s End If End Sub End Class </code></pre> <p>Thanks!</p>
 

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