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

int main( void )
{
	char path[MAX_PATH+1];
	LPITEMIDLIST folder = NULL;
	HRESULT hr;
	DWORD error;
	BOOL b;
	LPMALLOC shm;

	// get pointer to 'free' function for output of SHGetSpecialFolderLocation
	hr = SHGetMalloc( &shm );
	if (hr != S_OK) {
		error = HRESULT_CODE(hr);
		fprintf( stderr, "SHGetMalloc failed with error %u\n", error );
		return 1;
	}

	// get id
	hr = SHGetSpecialFolderLocation( NULL, CSIDL_PERSONAL, &folder );
	if (hr != S_OK) {
		error = HRESULT_CODE(hr);
		fprintf( stderr, "SHGetSpecialFolderLocation failed with error %d\n", error );
		return 1;
	}

	// extract path from id
	b = SHGetPathFromIDList( folder, path );
	shm->lpVtbl->Free( shm, folder );
	if (!b) {
		fprintf( stderr, "SHGetPathFromIDList failed\n" );
		return 1;
	}

	// print
	printf( "path: '%s'\n", path );
	return 0;
}
