Created
December 19, 2016 22:28
-
-
Save Iristyle/7dec27fa1fc6cf9b34875461496b9866 to your computer and use it in GitHub Desktop.
Ruby SHGetFolderPath - different implementations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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