tags:

views:

1311

answers:

6

Some programs read the company name that you entered when Windows was installed and display it in the program. How is this done? Are they simply reading the name from the registry?

A: 

Check the API SystemParametersInfo and a constant named SPI_GETOEMINFO

int details = SystemParametersInfo(SPI_GETOEMINFO, OEMInfo.Capacity, OEMInfo, 0);
        if (details != 0)
        {
            MessageBox.Show(OEMInfo.ToString());
        }

That will return the companyname for the OEM. I dont think you have to enter company name when installing windows, only computer name ( I can be wrong here)

You can see all the constants and examples here:
http://pinvoke.net/default.aspx/Enums.SystemMetric

Stefan
I assume you are pinvoking the SystemParametersInfo from user32.dll. Where are you getting the constants from?
epotter
@epotter: http://pinvoke.net/default.aspx/Enums.SystemMetric
Stefan
+9  A: 

If you want the registered company name as entered in the registry, you can get it from:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\RegisteredOrganization

Using the Registry class you can do something along these lines:

string org = (string)Microsoft.Win32.Registry.GetValue(@"HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion", "RegisteredOrganization", "");
Xiaofu
I'd forgotten the @, thanks to GeneQ for pointing that out.
Xiaofu
need to make the HKEY_... a verbatim string literal using @ or escape the backslashes
Russ Cam
A few seconds too slow, Russ. ;) People seem very eagle-eyed on here, which is nice to see.
Xiaofu
+4  A: 

Windows stores the registered company name in the registry at :

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\RegisteredOrganization

Import the following :

using Microsoft.Win32;

Read the value of the registry key required, like so:

RegistryKey hklm = Registry.LocalMachine;
hklm = hklm.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion");
Object obp = hklm.GetValue("RegisteredOrganization");`
Console.WriteLine("RegisteredOrganization :{0}",obp);`

Or a one liner, as suggested by Xiaofu.

However, the correct way is to use a double backslash. This is because the backslash is the C# escape character - that is, you can insert a newline character using \n, or tab using \t therefore to let C# know that we want a plain backslash and not some escaped character, we have to use two backslashes (\) or use @ in front of the string like (@"\somestring") :

string org = (string)Microsoft.Win32.Registry.GetValue("HKEY_LOCAL_MACHINE\\\Software\\Microsoft\\Windows NT\\CurrentVersion", "RegisteredOrganization", "");

Note: The RegisteredOrganization key is not guaranteed to contain a value, as it may not have been filled in during the OS installation. So always use a try/catch block or check the returned value.

GeneQ
A: 

I couldn't find a method or property in .NET that gets you the information, but I found a technote that tells what registry key contains the info:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\RegisteredOrganization

http://support.microsoft.com/kb/310441

Ken Pespisa
+3  A: 

You can read this Using WMI, it looks to me like you're after the Win32_OperatingSystem class and the Organization element of that class holds the company name.

The code below is a console app that shows the registered user and organization. To get it to run you'll need to add a reference to System.Management.dll to the project. There should only be one management object so the foreach is probably redundant, not quite sure what best practice for that would be:

using System;
using System.Management;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            ManagementClass c = new ManagementClass("Win32_OperatingSystem");

            foreach (ManagementObject o in c.GetInstances())
            {
                Console.WriteLine("Registered User: {0}, Organization: {1}", o["RegisteredUser"], o["Organization"]);
            }
            Console.WriteLine("Finis!");
            Console.ReadKey();
        }
    }
}
Murph
A: 

I don't like to have text-y registry calls in my business code, and I'm not a fan of utility classes, so I wrote an extension method that gets the company name from the registry.

using Microsoft.Win32;

namespace Extensions
{
    public static class MyExtensions
    {
        public static string CompanyName(this RegistryKey key)
        {
            // this string goes in my resources file usually
            return (string)key.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion").GetValue("RegisteredOrganization");
        }
    }
}

Then I can easily get that value:

RegistryKey key = Registry.LocalMachine;
return key.CompanyName();

It's nothing fancy, just a prettier way of dealing with oft-retrieved registry values.

Robert S.