2

give the code below, lastuser string returns null, however, if I use regedit to look at this key it has data associated with it. Is LoggedOnSAMuser a restricted key?

public static string lastlogon()
    {
        string lastuser;
        RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
        RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI",false);
        if (registryKey != null)
        {
            lastuser = (string) registryKey.GetValue("LastLoggedOnSAMUser");
        }
        else lastuser = "Unknown User";
        return (lastuser);
    }
user3308131
  • 159
  • 1
  • 5
  • 16
  • I can't find that key. I only see `LastLoggedOnSAMUser`. But that may be due to a configuration difference between your machine and mine. – CodesInChaos Oct 10 '14 at 12:32
  • 2
    Please specify your target CPU and your OS bitness. Probably you are a victim of [registry redirector](http://msdn.microsoft.com/en-us/library/windows/desktop/aa384232(v=vs.85).aspx) – Steve Oct 10 '14 at 12:34
  • OS = Windows 7 Professional, SP1 64-bit. – user3308131 Oct 10 '14 at 12:36
  • 1
    I also want to note that you should look for a proper API which returns what you want. Directly accessing internal windows registry keys is usually not a good idea. For example [How to get logged-in user's full name in windows?](http://stackoverflow.com/questions/3438634/how-to-get-logged-in-users-full-name-in-windows) mentions `GetUserName` and `GetUserNameEx`. – CodesInChaos Oct 10 '14 at 12:36
  • I found this: http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28090635.html Maybe it helps you to find a solution when LoggedOnSAMUser is not present. – blfuentes Oct 10 '14 at 12:39
  • There's an explanation: [http://stackoverflow.com/questions/13728491/opensubkey-returns-null-for-a-registry-key-that-i-can-see-in-regedit-exe](http://stackoverflow.com/questions/13728491/opensubkey-returns-null-for-a-registry-key-that-i-can-see-in-regedit-exe) – Fratyx Oct 10 '14 at 12:40
  • I've tried running it as administrator and have tried the registry view at both 32 and 64. I'm now wondering if there's another key I can get the last logged on user information from. – user3308131 Oct 10 '14 at 12:52
  • Are you sure you want the last logged in user and not the currently logged in user (i.e. the one your application is running in)? – CodesInChaos Oct 10 '14 at 13:13

3 Answers3

6

2 possible issues:

  1. You are trying to read the LoggedOnSAMUser key, quite a chance you meant LastLoggedOnSAMUser.
  2. You might be trying to read a 64-bit registry entry from a 32-bit application. If possible, change your platform target to x64 and retry. If not possible, you might have to use the registry API directly. Hopefully a nudge in the right directon: link
decPL
  • 5,384
  • 1
  • 26
  • 36
  • Changing to a x64 platform did the trick. Not sure why I couldn't read the 64 bit registry key from a 32 bit application. – user3308131 Oct 10 '14 at 13:05
  • Run `reg import file.reg /reg:32` to import registry keys as 32-bit entries. Then your 32-bit application can read them. – dibery Sep 17 '22 at 16:07
0

Almost certainly you have a 32 bit process on a 64 bit machine and so are subject to registry redirection. Your 32 bit process, by default, reads from the 32 bit view of the registry. But you want to read from the 64 bit view.

Solve the problem by requesting that you read from the 64 bit view of the registry, by way of the RegistryView enumeration.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • I have tried both the 32 & 64 bit base views (RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);) without any change in returned value of NULL. – user3308131 Oct 10 '14 at 13:03
  • You are doing something wrong. You don't need to switch to x64. `RegistryView` is known to work. – David Heffernan Oct 10 '14 at 13:19
0

This seems to work on Windows 7

    RegistryKey thisKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
    RegistryKey thisSubkey = thisKey.OpenSubKey(@"SOFTWARE\\fred", false);
    _url = (string)thisSubkey.GetValue("_url", "*");
    _port = (string)thisSubkey.GetValue("_port", 0);
Billy Willoughby
  • 806
  • 11
  • 15