The task is to determine the last time entries for the registry key. Since the standard RegistryKey class does not provide this function, I have to use the WinAPI function "RegQueryInfoKey". To get the key descriptor, I open it with "RegOpenKeyEx".
This is a WinAPI function prototype (taken from MSDN):
LONG WINAPI RegOpenKeyEx( __in HKEY hKey, __in LPCTSTR lpSubKey, DWORD ulOptions, __in REGSAM samDesired, __out PHKEY phkResult );
I use the following expression:
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)] public static extern int RegOpenKeyEx(UIntPtr hkey, string lpSubKey, uint samDesired, ref UIntPtr phkResult);
Then I call it like this:
UIntPtr hKey = UIntPtr.Zero; string myKeyName = "blablabla"; UIntPtr HKEY_USERS = (UIntPtr)0x80000003; uint KEY_READ = 0x20019; RegOpenKeyEx(HKEY_USERS, myKeyName, KEY_READ, ref hKey);
At this point, I get an "Access Violation" exception. What am I doing wrong? I think something is wrong with passing parameters, but how to do it right?
Thanks.
source share