none
RegOpenKeyExA is not able to read registry key ,It should return 0 but it always return Erro file not found. Platform is window 10. RRS feed

  • Question

  •  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim txt As String
            Dim res As Integer
            Dim rkey As Integer
            Const KEY_WOW64_64KEY As Integer = &H100
            txt = "\\SOFTWARE\\Policies\\Microsoft\\Windows\\windows search\\preventindexinguncachedexchangefolders"
            'res = RegOpenKeyExA(&H80000002, txt, 0, KEY_WOW64_64KEY, rkey)
            res = RegOpenKeyExA(&H80000002, txt, 0, 1, rkey)
            If (res = 0) Then
                MessageBox.Show("The calculations are complete", "My Application",
                MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk)
            Else
                MessageBox.Show("The calculations are not complete", "My Application",
                MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk)
            End If
        End Sub
    End Class
    • Moved by Sabah ShariqMVP Tuesday, June 6, 2017 12:19 PM Moved From Visual C#
    Tuesday, June 6, 2017 5:30 AM

All replies

  • Perhaps your implementation of RegOpenKeyExA is not correct.

    Perhaps that key does not exist.

    Without more information, this is just a guessing game. Pinvoke.net does not show that function, only this one:RegOpenKeyEx (advapi32)

    Declare Auto Function RegOpenKeyEx Lib "advapi32.dll"  (
       ByVal hKey As IntPtr,
       ByVal lpSubKey As String,
       ByVal ulOptions As Integer,
       ByVal samDesired As Integer,
       ByRef phkResult As Integer
    ) As Integer
    

    Tuesday, June 6, 2017 12:52 PM
  • I haven't this key on my Windows 10 , but this works with another key for testing :

    Dim hKey As IntPtr = IntPtr.Zero
    Dim nResult As Integer = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\Policies\Microsoft\Windows\Control Panel\Desktop", 0, KEY_READ Or KEY_WOW64_64KEY, hKey)
    If (nResult = 0) Then
        Dim nDataSize As Integer = Marshal.SizeOf(nDataSize)
        Dim nData As Integer = 0
        nResult = RegQueryValueEx(hKey, "LowLevelHooksTimeout", IntPtr.Zero, 0, nData, nDataSize)
        RegCloseKey(hKey)
    End If	

    Declarations:

    Public Const HKEY_LOCAL_MACHINE = &H80000002
    
    Public Const ERROR_MORE_DATA As Integer = &HEA
    Public Const ERROR_SUCCESS As Integer = 0
    Public Const READ_CONTROL As Integer = &H20000
    Public Const SYNCHRONIZE As Integer = &H100000
    Public Const STANDARD_RIGHTS_READ As Integer = READ_CONTROL
    Public Const STANDARD_RIGHTS_WRITE As Integer = READ_CONTROL
    Public Const KEY_QUERY_VALUE As Integer = &H1
    Public Const KEY_SET_VALUE As Integer = &H2
    Public Const KEY_CREATE_SUB_KEY As Integer = &H4
    Public Const KEY_ENUMERATE_SUB_KEYS As Integer = &H8
    Public Const KEY_NOTIFY As Integer = &H10
    Public Const KEY_CREATE_LINK As Integer = &H20
    Public Const KEY_READ As Integer = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
    Public Const KEY_WRITE As Integer = ((STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY) And (Not SYNCHRONIZE))
    Public Const REG_NONE As Integer = 0
    Public Const REG_SZ As Integer = 1
    Public Const REG_EXPAND_SZ As Integer = 2
    Public Const KEY_WOW64_32KEY As Integer = (&H200)
    Public Const KEY_WOW64_64KEY As Integer = (&H100)
    
    <DllImport("Advapi32", CharSet:=CharSet.Unicode)>
    Public Shared Function RegOpenKeyEx(hKey As IntPtr, lpSubKey As [String], ulOptions As Integer, samDesired As Integer, ByRef hkResult As IntPtr) As Integer
    End Function
    
    <DllImport("Advapi32", SetLastError:=True, CharSet:=CharSet.Unicode)>
    Public Shared Function RegQueryValueEx(<[In]()> ByVal hKey As IntPtr, <[In]()> ByVal lpValueName As String, ByVal lpReserved As UInteger, <Out()> ByRef lpType As Integer, <Out()> ByRef lpData As Integer, <[In](), Out()> ByRef lpcbData As Integer) As Integer
    End Function
    
    <DllImport("Advapi32", SetLastError:=False)>
    Public Shared Function RegCloseKey(handle As IntPtr) As Integer
    End Function	

    Tuesday, June 6, 2017 2:33 PM
  • First, why do this with unmanaged code instead of using the managed registry objects?

    Second, why do this with a VB application?  You are modifying the search behavior of Windows in conjunction with Exchange... that should be done through Group Policy on a Domain Controller or through Exchange management - either way it should be done in Active Directory somehow if you want to avoid all security concerns and ensure it works on any applicable system.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Wednesday, June 7, 2017 7:31 PM
    Moderator
  •  I was also wondering why it was not being done with the existing registry classes/methods in the Microsoft.Win32 Namespace ,  such as the Registry Class and RegistryKey Class.   8)

    If you say it can`t be done then i`ll try it

    Wednesday, June 7, 2017 8:08 PM