Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To read the values from the registry in a useful format quite some code is necessary to convert between Haskell and C types. And that the values in question are usually of type <code>REG_EXPAND_SZ</code> also doesn't help. So it's not pretty, but this works for me:</p> <pre><code>{-# LANGUAGE ForeignFunctionInterface #-} import System.Win32.Types import System.Win32.Registry import Foreign.Ptr (castPtr) import Foreign.Marshal.Alloc (allocaBytes) import Foreign.C.String (peekCWString, withCWString) import Control.Exception (bracket, throwIO) -- // parse a string from a registry value of certain type parseRegString :: RegValueType -&gt; LPBYTE -&gt; IO String parseRegString ty mem | ty == rEG_SZ = peekCWString (castPtr mem) | ty == rEG_EXPAND_SZ = peekCWString (castPtr mem) &gt;&gt;= expandEnvironmentStrings | otherwise = ioError (userError "Invalid registry value type") -- // FFI import of the ExpandEnvironmentStrings function needed -- // to make use of the registry values expandEnvironmentStrings :: String -&gt; IO String expandEnvironmentStrings toexpand = withCWString toexpand $ \input -&gt; allocaBytes 512 $ \output -&gt; do c_ExpandEnvironmentStrings input output 256 peekCWString output foreign import stdcall unsafe "windows.h ExpandEnvironmentStringsW" c_ExpandEnvironmentStrings :: LPCTSTR -&gt; LPTSTR -&gt; DWORD -&gt; IO DWORD -- // open the registry key userShellFolders :: IO HKEY userShellFolders = regOpenKeyEx hKEY_CURRENT_USER "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders" kEY_QUERY_VALUE -- // read the actual value localAppData :: IO String localAppData = bracket userShellFolders regCloseKey $ \usfkey -&gt; allocaBytes 512 $ \mem -&gt; do ty &lt;- regQueryValueEx usfkey "Local AppData" mem 512 parseRegString ty mem main = localAppData &gt;&gt;= print </code></pre> <p>I'm not sure if all the error cases are handled correctly (like if the passed buffer was to small), so you might want to check the windows docs to see what happens in these cases. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload