Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 123623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T04:18:57+00:00 2026-05-11T04:18:57+00:00

EDIT: Scotty2012 and David Morton’s answers don’t work for me so I have put

  • 0

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.

I’m not much cop at P/Invoke and I’m struggling with declaring and calling SHSetKnownFolderPath. I’m using VB9 but if anyone puts answers in C# I should be able to translate.

I have got SHGetKnowFolderPath working. Here is my code.

In VB

Imports System.Runtime.InteropServices  Public Class Form1     <DllImport('shell32.dll')> _     Private Shared Function SHGetKnownFolderPath(<MarshalAs(UnmanagedType.LPStruct)> ByVal rfid As Guid, ByVal dwFlags As UInteger, ByVal hToken As IntPtr, ByRef pszPath As IntPtr) As Integer     End Function      <DllImport('shell32.dll')> _     Private Shared Function SHSetKnownFolderPath(<MarshalAs(UnmanagedType.LPStruct)> 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 

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T04:18:58+00:00Added an answer on May 11, 2026 at 4:18 am

    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.

    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.

    I included several wrapper functions that hide all of the evil marashal’ing details for calling the particular functions.

    If there is any particular folder id you’d like or explanation please add a comment and I’ll update the sample.

    EDIT 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)

    Imports System.Runtime.InteropServices    Module NativeMethods      Public Enum KNOWN_FOLDER_FLAG          '''KF_FLAG_CREATE -> 0x00008000         KF_FLAG_CREATE = 32768          '''KF_FLAG_DONT_VERIFY -> 0x00004000         KF_FLAG_DONT_VERIFY = 16384          '''KF_FLAG_DONT_UNEXPAND -> 0x00002000         KF_FLAG_DONT_UNEXPAND = 8192          '''KF_FLAG_NO_ALIAS -> 0x00001000         KF_FLAG_NO_ALIAS = 4096          '''KF_FLAG_INIT -> 0x00000800         KF_FLAG_INIT = 2048          '''KF_FLAG_DEFAULT_PATH -> 0x00000400         KF_FLAG_DEFAULT_PATH = 1024          '''KF_FLAG_NOT_PARENT_RELATIVE -> 0x00000200         KF_FLAG_NOT_PARENT_RELATIVE = 512          '''KF_FLAG_SIMPLE_IDLIST -> 0x00000100         KF_FLAG_SIMPLE_IDLIST = 256          '''KF_FLAG_ALIAS_ONLY -> 0x80000000         KF_FLAG_ALIAS_ONLY = &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')       <DllImport('shell32.dll')> _     Public Function SHGetKnownFolderPath( _         ByRef folderId As Guid, _         ByVal flags As UInteger, _         ByVal token As IntPtr, _         <Out()> ByRef pathPtr As IntPtr) As Integer      End Function      <DllImport('shell32.dll')> _     Public Function SHSetKnownFolderPath( _         ByRef folderId As Guid, _         ByVal flags As UInteger, _         ByVal token As IntPtr, _         <[In](), MarshalAs(UnmanagedType.LPWStr)> 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 <> 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 <> 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 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Edit: The below question was answered by this . I have a new updated
EDIT: There's now a doc page on this so this question is irrelevant, also
Edit: I'm looking for solution for this question now also with other programming languages.
EDIT : I've gotten the famous question badge with this question, so I figured
Edit: Seems numerous people think this is a dumb idea, so I would appreciate
EDIT: I have reworded the title question slightly, and adjusted the text to respond
EDIT: This question was exceptionally dumb and made me look like a script kiddie,
EDIT: looking for this: http://diminishing.org/extending-formtastic-with-a-sprinkle-of-jquery (If this works I'll answer my own question) I've
Edit: This question was written in 2008, which was like 3 internet ages ago.
EDIT: This question is more about language engineering than C++ itself. I used C++

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.