Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't retrieve registry value size #230

Open
hverstoep opened this issue Jan 19, 2024 · 2 comments
Open

Can't retrieve registry value size #230

hverstoep opened this issue Jan 19, 2024 · 2 comments

Comments

@hverstoep
Copy link

I can't retrieve registry value size. This is needed to be able to provide a buffer of the right size. See function here: https://learn.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regqueryvalueexa

Current Behavior

We can specify the buffer size (Int):

regQueryValueEx :: HKEY -> String -> LPBYTE -> Int -> IO RegValueType
regQueryValueEx key name value value_len =
  withForeignPtr key $ \ p_key ->
  withTString name $ \ c_name ->
  alloca $ \ p_ty ->
  with (fromIntegral value_len) $ \ p_value_len -> do
  failUnlessSuccess "RegQueryValueEx" $
    c_RegQueryValueEx p_key c_name nullPtr p_ty value p_value_len
  peek p_ty
foreign import WINDOWS_CCONV unsafe "windows.h RegQueryValueExW"
  c_RegQueryValueEx :: PKEY -> LPCTSTR -> Ptr DWORD -> Ptr DWORD -> LPBYTE -> Ptr DWORD -> IO ErrCode

But it isn't returned. I need to call the function once with 0 to get the required buffer size, the allocate the buffer, and call the function again. Perhaps something like:

regQueryValueEx' :: HKEY -> String -> LPBYTE -> Int -> IO (Int, RegValueType)
regQueryValueEx' key name value value_len =
  withForeignPtr key $ \ p_key ->
  withTString name $ \ c_name ->
  alloca $ \ p_ty ->
  with (fromIntegral value_len) $ \ p_value_len -> do
  failUnlessSuccess "RegQueryValueEx" $
    c_RegQueryValueEx p_key c_name nullPtr p_ty value p_value_len
  (,) <$> (fromIntegral <$> peek p_value_len) <*> peek p_ty
foreign import ccall unsafe "windows.h RegQueryValueExW"
  c_RegQueryValueEx :: PKEY -> LPCTSTR -> Ptr DWORD -> Ptr DWORD -> LPBYTE -> Ptr DWORD -> IO ErrCode

Your Environment

  • Version used: Win32-2.13.2.1
  • Operating System and version: Windows 10
@Mistuke
Copy link
Contributor

Mistuke commented Jan 21, 2024

Hi, thanks for the report. I'll take a look tomorrow evening.

@Mistuke
Copy link
Contributor

Mistuke commented Feb 11, 2024

Sorry for the delay, was caught up in some work.

Hmm maybe we should just expose the size parameter as a pointer? So maybe just

regQueryValueEx' :: HKEY -> String -> LPBYTE -> Ptr Int -> IO RegValueType

since you have to create value anyway, but maybe this is a less natural fit.

I think we should avoid the deference unless the results is ERROR_MORE_DATA and so leaving it up to the caller seems more natural.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants