Skip to content

Instantly share code, notes, and snippets.

@Iristyle
Created October 23, 2017 23:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Iristyle/db78afd90bf59c28d3cc1741241bde56 to your computer and use it in GitHub Desktop.
Save Iristyle/db78afd90bf59c28d3cc1741241bde56 to your computer and use it in GitHub Desktop.
Fiddle vs Win32API for SHGetFolderPath
require 'fiddle/import'
require 'fiddle/types'
module Win32FiddleDirectories
extend Fiddle::Importer
include Fiddle::Win32Types # adds HWND, HANDLE, DWORD type aliases
# calling this appears to hose everything up!
# dlload "shell32", "kernel32"
typealias 'LPWSTR', 'wchar_t*'
typealias 'LONG', 'long'
typealias 'HRESULT','LONG'
# https://msdn.microsoft.com/en-us/library/windows/desktop/aa383751(v=vs.85).aspx
# HRESULT SHGetFolderPath(
# _In_ HWND hwndOwner,
# _In_ int nFolder,
# _In_ HANDLE hToken,
# _In_ DWORD dwFlags,
# _Out_ LPTSTR pszPath
# );
extern 'HRESULT SHGetFolderPath(HWND, int, HANDLE, DWORD, LPWSTR)'
PROGRAM_FILES = 0x0026
COMMON_APPDATA = 0x0023
def self.get_program_files
buffer = 0.chr * 1024
SHGetFolderPath(0, PROGRAM_FILES, 0, 0, buffer)
buffer.strip
end
def self.get_common_appdata
buffer = 0.chr * 1024
SHGetFolderPath(0, COMMON_APPDATA, 0, 0, buffer)
buffer.strip
end
end
module Win32Directories
require 'Win32API'
PROGRAM_FILES = 0x0026
COMMON_APPDATA = 0x0023
# https://msdn.microsoft.com/en-us/library/windows/desktop/aa383751(v=vs.85).aspx
# HRESULT SHGetFolderPath(
# _In_ HWND hwndOwner,
# _In_ int nFolder,
# _In_ HANDLE hToken,
# _In_ DWORD dwFlags,
# _Out_ LPTSTR pszPath
# );
# L - 32-bit unsigned
# P - point to a struct / null-terminated string
# HWND, int (32-bit integer), HANDLE, DWORD, LPWSTR / HRESULT return value
SHGetFolderPath = Win32API.new('shell32', 'SHGetFolderPath', ['P','L','P','L','P'], 'L')
def self.get_program_files
buffer = 0.chr * 1024
SHGetFolderPath.call(0, PROGRAM_FILES, 0, 0, buffer)
buffer.strip
end
def self.get_common_appdata
buffer = 0.chr * 1024
SHGetFolderPath.call(0, COMMON_APPDATA, 0, 0, buffer)
buffer.strip
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment