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 7559525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:39:38+00:00 2026-05-30T12:39:38+00:00

I’m trying to write a function to get the Windows-equivalent of HOME. My C

  • 0

I’m trying to write a function to get the Windows-equivalent of HOME. My C skills are rusty, so don’t mind that my example code does not compile. I’m trying to use SHGetKnownFolderPath on Windows Vista and newer, and SHGetFolderPath on Server 2003 and older. Since I expect to encounter users running Windows XP (as it is still the number one deployed version of Windows), I am going to avoid having a reference to SHGetKnownFolderPath in the symbol table (as this would lead to a binary that won’t even load on XP). I know to LoadLibrary() shell32 and to GetProcAddress() from there, but my skills on doing function pointers are, well, crap, to say the least.

When I write features, and they are difficult to handle, I isolate them into an separate example file. The broken example I have so far is:

#include <windows.h>
#include <stdio.h>

// Pointerizing this Vista-and-later call for XP/2000 compat, etc.
typedef HRESULT (WINAPI* lpSHGetKnownFolderPath)(
    REFKNOWNFOLDERID rfid,
    DWORD dwFlags,
    HANDLE hToken,
    PWSTR *ppszPath
) lpSHGetKnownFolderPath;

int main(int argc, char *argv[])
{
    // SHGet(Known)FolderPath() method.
    HMODULE hndl_shell32;
    lpSHGetKnownFolderPath pSHGetKnownFolderPath;
    hndl_shell32 = LoadLibrary("shell32");
    pSHGetKnownFolderPath = GetProcAddress(hndl_shell32, "SHGetKnownFolderPathW");
    if(pSHGetKnownFolderPath != NULL) {

    } else {

    }

}

My question is this: Knowing that I’m doing this wrong, how would I go about doing this right? And an explanation as to how to do it right in the future would be appreciated. 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. Editorial Team
    Editorial Team
    2026-05-30T12:39:39+00:00Added an answer on May 30, 2026 at 12:39 pm

    Here is a small application that shows how to use LoadLibrary() and GetProcAddress() with advice provided in comments:

    #include <windows.h>
    #include <stdio.h>
    #include <shlobj.h>
    
    /* The name of the function pointer type is
        'lpSHGetKnownFolderPath', no need for
        additional token after ')'. */
    typedef HRESULT (WINAPI* lpSHGetKnownFolderPath)(
        REFKNOWNFOLDERID rfid,
        DWORD dwFlags,
        HANDLE hToken,
        PWSTR *ppszPath
    );
    
    int main()
    {
        HMODULE hndl_shell32;
        lpSHGetKnownFolderPath pSHGetKnownFolderPath;
    
        /* Always check the return value of LoadLibrary. */
        hndl_shell32 = LoadLibrary("shell32");
        if (NULL != hndl_shell32)
        {
            /* There is no 'SHGetKnownFolderPathW()'.
               You need to cast return value of 'GetProcAddress()'. */
            pSHGetKnownFolderPath = (lpSHGetKnownFolderPath)
                GetProcAddress(hndl_shell32, "SHGetKnownFolderPath");
    
            if(pSHGetKnownFolderPath != NULL)
            {
                PWSTR user_dir = 0;
                if (SUCCEEDED(pSHGetKnownFolderPath(
                                FOLDERID_Profile,
                                0,
                                NULL,
                                &user_dir)))
                {
                    /* Use 'user_dir' - remember to:
    
                         CoTaskMemFree(user_dir);
    
                       when no longer required.
                     */
                }
            }
            else
            {
                fprintf(stderr, "Failed to locate function: %d\n",
                    GetLastError());
            }
    
            /* Always match LoadLibrary with FreeLibrary.
               If FreeLibrary() results in the shell32.dll
               being unloaded 'pSHGetKnownFolderPath' is
               no longer valid.
             */
            FreeLibrary(hndl_shell32);
        }
        else
        {
            fprintf(stderr, "Failed to load shell32.dll: %d\n", GetLastError());
        }
    
        return 0;
    }
    

    This was compiled on Windows XP.

    Output on Windows XP:

    Failed to locate function: 127

    where 127 means The specified procedure could not be found.

    Output on Windows Vista:

    C:\Users\admin

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace

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.