Solved

C# problem using the GetVersionExA API.

Posted on 2004-09-28
14
1,047 Views
Last Modified: 2008-01-09
Hi.
I'm trying to determine if the operating system is a domain controller using the GetVersionExA function and OSVERSIONINFOEX struct
I tried the exact same code in VB.NET and it worked just fine, but it doesn't in C#.
Any ideas?

Here is the code I have:
[StructLayout(LayoutKind.Sequential)]
public struct OSVERSIONINFOEX
{
      [MarshalAs(UnmanagedType.U4)]
      public int dwOSVersionInfoSize;
      [MarshalAs(UnmanagedType.U4)]
      public int dwMajorVersion;
      [MarshalAs(UnmanagedType.U4)]
      public int dwMinorVersion;
      [MarshalAs(UnmanagedType.U4)]
      public int dwBuildNumber;
      [MarshalAs(UnmanagedType.U4)]
      public int dwPlatformId;
      [MarshalAs(UnmanagedType.LPTStr, SizeConst=128)]
      public string szCSDVersion;
      [MarshalAs(UnmanagedType.U2)]
      public short wServicePackMajor;
      [MarshalAs(UnmanagedType.U2)]
      public short wServicePackMinor;
      [MarshalAs(UnmanagedType.U2)]
      public short wSuiteMask;
      public byte wProductType;
      public byte wReserved;
}

[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool GetVersionExA([MarshalAs(UnmanagedType.Struct)] ref OSVERSIONINFOEX lpVersionInfo);

public bool IsDomainController()
{
bool isDC = false;
OSVERSIONINFOEX os = new OSVERSIONINFOEX();
os.dwOSVersionInfoSize = Marshal.SizeOf(os);
if (GetVersionExA(ref os))
{
      isDC = (os.wProductType == 2);
}
else
{
      Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
}

return isDC;
}

Any help would be greatly appreciated!
0
Comment
Question by:Balder1978
  • 7
  • 7
14 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 12173937
Try something like this instead:

string osVersion = Environment.OSVersion.Platform.ToString();

Bob
0
 
LVL 2

Author Comment

by:Balder1978
ID: 12180802
I don't need the OS version, I need to know if it's a Domain Controller or not.
But thanks for the input!
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 12181098
Do you still have the VB.NET code?

Bob
0
 
LVL 2

Author Comment

by:Balder1978
ID: 12181126
sure:

<StructLayout(LayoutKind.Sequential)> _
Public Structure OSVERSIONINFOEX
        <MarshalAs(UnmanagedType.U4)> _
        Public dwOSVersionInfoSize As Integer
        <MarshalAs(UnmanagedType.U4)> _
        Public dwMajorVersion As Integer
        <MarshalAs(UnmanagedType.U4)> _
        Public dwMinorVersion As Integer
        <MarshalAs(UnmanagedType.U4)> _
        Public dwBuildNumber As Integer
        <MarshalAs(UnmanagedType.U4)> _
        Public dwPlatformId As Integer
        <MarshalAs(UnmanagedType.ByValArray, SizeConst:=128)> _
        Public szCSDVersion() As Char
        <MarshalAs(UnmanagedType.U2)> _
        Public wServicePackMajor As Short
        <MarshalAs(UnmanagedType.U2)> _
        Public wServicePackMinor As Short
        <MarshalAs(UnmanagedType.U2)> _
        Public wSuiteMask As Short
        Public wProductType As Byte
        Public wReserved As Byte
End Structure

Private Declare Function GetVersionEx Lib "kernel32.dll" Alias "GetVersionExA" (<MarshalAs(UnmanagedType.Struct)> ByRef ByReflpVersionInfo As OSVERSIONINFOEX) As Boolean

Usage:
Dim os As OSVERSIONINFOEX = New OSVERSIONINFOEX
os.dwOSVersionInfoSize = Marshal.SizeOf(os)
GetVersionEx(os)
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 12181180
In VB.NET, the os.dwOSVersionInfoSize = 156, and in C#, it is 32.  Investigating...

Bob
0
 
LVL 2

Author Comment

by:Balder1978
ID: 12181203
Exactly, and if I set it manually to 156 (which is too dirty programming), I get the OS version numbers, but not the wProductType wich is what I'm looking for.
Thanks!
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 12181246
The OSVERSIONINFOEX.szCSDVersion appears to be the problem.

In the VB.NET IL code:

szCSDVersion public marshal (fixed array[128]) char[]

In the C# IL code:

szCSDVersion public marshal (lptstr) string

There is clearly a size difference here.

Bob
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 96

Accepted Solution

by:
Bob Learned earned 125 total points
ID: 12181278
Change to this:

      [MarshalAs(UnmanagedType.ByValArray , SizeConst=128)]
      public char[] szCSDVersion;

Bob
0
 
LVL 2

Author Comment

by:Balder1978
ID: 12181308
that didn't work, but this did:

[MarshalAs(UnmanagedType.ByValTStr, SizeConst=128)]
public string szCSDVersion;

The size is now 156, but it still doesn't work...
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 12181348
It worked for me--return a false.  What .NET version do you have?  I have 2003.

Bob
0
 
LVL 2

Author Comment

by:Balder1978
ID: 12181920
I have 2003 with .NET 1.1 SP1 too... Strange...
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 12181938
What happened when you used this?

[MarshalAs(UnmanagedType.ByValArray , SizeConst=128)]
      public char[] szCSDVersion;


Bob
0
 
LVL 2

Author Comment

by:Balder1978
ID: 12181952
So sorry, didn't change public char[] szCSDVersion;
I'll test it and get back to u ASAP.
0
 
LVL 2

Author Comment

by:Balder1978
ID: 12182176
Thanks alot, man. Works great!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
LINQ question 7 22
Trouble with References... 5 25
C# Error - Add Failed 12 52
What .NET website keeps me current? 9 32
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now