Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Can't write to registry

Brooks_Van_Horn
New Contributor I
727 Views

My setup program created three keys in the Win 10 registry and I can read them but when I try to change the value of one of them I get an error 5 (lack of access priviledge). My code looks something like:

Subroutine ChkReg()

use ifwinty
use user32
use advapi32

implicit none

Integer(4):: iret, iLen
Integer(4):: lret
Integer(4):: hKey = 0
character(255):: buff

    buff = "                             "C
    iLen = sizeof(buff) - 1
    iret = RegOpenKeyEx (HKEY_CLASSES_ROOT,     &
                         "PDC"C,                                                       &
                         0,                                                                    &
                         KEY_EXECUTE,                                           &  ! only key that works
                         LOC(hKey) )

    if (iret == ERROR_SUCCESS) Then
       buff = "                             "C
       lret = RegQueryValueEx( hKey, "Data1"C, NULL, NULL, &
                               LOC(buff), iLen )
       if (iret /= ERROR_SUCCESS) Then
          buff = "                             "C
          lret = RegQueryValueEx( hKey, "Data1"C, NULL, NULL, &
                               LOC(buff), iLen )
          iret = MessageBox(NULL, buff, "Data1 Register PDC2", IDOK)
       else
          iret = MessageBox(NULL, buff, "Data1 Register PDC1", IDOK)
       end if

       Buff = "123456789"C
       iLen = 10
       iret = RegSetValueEX ( hKey, "Data1"C, 0, REG_SZ, LOC(Buff), iLen)

       iret = RegCloseKey(hKey)

    End If

End Subroutine ChkReg

All works well except the RegSetValueEX which returns an error code of 5. If I try a key of KEY_ALL_ACCESS then I get an error code 5 in RegOpenKeyEx. Any suggestions will be much appreciated.

Thanks,

Brooks

0 Kudos
1 Solution
andrew_4619
Honored Contributor II
727 Views

ilen will return the number of good bytes in buff if the return status is OK

 

 

View solution in original post

0 Kudos
8 Replies
Brooks_Van_Horn
New Contributor I
727 Views

If I change where my setup program puts the entries to HKEY_CURRENT_USER then I can read and write but isn't this hkcu key cleared when I logoff?

0 Kudos
IanH
Honored Contributor II
727 Views

If you want to set a value, then the handle to the key needs to have been opened with KEY_WRITE access (and perhaps others).

Try replacing `KEY_EXECUTE` on line 19 with `IOR(KEY_READ, KEY_WRITE)`.

KEY_ALL_ACCESS probably requests "too much" access, beyond the level permitted for a user that is not a "real" administrator.

(I would expect MessageBox to require null terminated strings.)

0 Kudos
Brooks_Van_Horn
New Contributor I
727 Views

I changed Messagebox to ""C strings. But I have another problem. I am reading Ok but I sometimes get junk after the data that I want. How do I stop that?

Brooks

0 Kudos
andrew_4619
Honored Contributor II
728 Views

ilen will return the number of good bytes in buff if the return status is OK

 

 

0 Kudos
Brooks_Van_Horn
New Contributor I
727 Views

Andrew,

Thanks for that. I didn't check the ret code. I have adapted a Hash sort routine from Numerical Recipies and it works really well. I decided to extend the code to sort arrays of strings without shuffling them around by using an integer index array. The sort seems to work except for the last element in the string and it doesn't want to get sorted correctly. Would you mind looking at it and see where my logic fails. I'll zip both the working version and the non-working array of strings one. This has stumpted me for several days.

Thank you very much.

Brooks

0 Kudos
Brooks_Van_Horn
New Contributor I
727 Views

It still doesn't work.See:

       iLen = 20
       Buff = ""C
       iret = RegQueryValueEx( hKey, "Data4"C, NULL, NULL, LOC(Buff), iLen )
       if (iret == ERROR_SUCCESS) Then
           myBuff = Buff(1:iLen-1)
           write(Buff,100) 'iLen = ', iLen, myBuff, iret
100        format (a,i2,5x,a,i5)
           iret = MessageBox(NULL, Buff // "x1"C, "Data4 Query"C, MB_OK)
       else
           iret = RegQueryValueEx( hKey, "Data4"C, NULL, NULL, LOC(MySN2), iLen )
           myBuff = Buff(1:iLen-1)
           write(Buff,100) 'iLen = ', iLen, myBuff, iret
           iret = MessageBox(NULL, Buff // "x2"C, "Data4 Query"C, MB_OK)
       end if

If I type Buff it appears as 1000 followed by some garbage.

Brooks

I repeated the RegQueryValueEx because the CVF Guide to Creating Windows Applications said it was wise.

0 Kudos
Brooks_Van_Horn
New Contributor I
727 Views

of course these are preceded by:

    buff = "                             "C
    iLen = sizeof(buff) - 1
    iret = RegOpenKeyEx (HKEY_CURRENT_USER,     &
                         "PDC"C,                &
                         0,                     &
                         KEY_ALL_ACCESS,        &
                         LOC(hKey) )

    if (iret == ERROR_SUCCESS) Then

 

0 Kudos
Brooks_Van_Horn
New Contributor I
727 Views

Andrew,

When I put a LOC() around iLen, everything worked correctly.  Thanks all.

Brooks

0 Kudos
Reply