Balder1978
asked on
C# problem using the GetVersionExA API.
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.S equential) ]
public struct OSVERSIONINFOEX
{
[MarshalAs(UnmanagedType.U 4)]
public int dwOSVersionInfoSize;
[MarshalAs(UnmanagedType.U 4)]
public int dwMajorVersion;
[MarshalAs(UnmanagedType.U 4)]
public int dwMinorVersion;
[MarshalAs(UnmanagedType.U 4)]
public int dwBuildNumber;
[MarshalAs(UnmanagedType.U 4)]
public int dwPlatformId;
[MarshalAs(UnmanagedType.L PTStr, SizeConst=128)]
public string szCSDVersion;
[MarshalAs(UnmanagedType.U 2)]
public short wServicePackMajor;
[MarshalAs(UnmanagedType.U 2)]
public short wServicePackMinor;
[MarshalAs(UnmanagedType.U 2)]
public short wSuiteMask;
public byte wProductType;
public byte wReserved;
}
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool GetVersionExA([MarshalAs(U nmanagedTy pe.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.ThrowExceptionForH R(Marshal. GetHRForLa stWin32Err or());
}
return isDC;
}
Any help would be greatly appreciated!
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.S
public struct OSVERSIONINFOEX
{
[MarshalAs(UnmanagedType.U
public int dwOSVersionInfoSize;
[MarshalAs(UnmanagedType.U
public int dwMajorVersion;
[MarshalAs(UnmanagedType.U
public int dwMinorVersion;
[MarshalAs(UnmanagedType.U
public int dwBuildNumber;
[MarshalAs(UnmanagedType.U
public int dwPlatformId;
[MarshalAs(UnmanagedType.L
public string szCSDVersion;
[MarshalAs(UnmanagedType.U
public short wServicePackMajor;
[MarshalAs(UnmanagedType.U
public short wServicePackMinor;
[MarshalAs(UnmanagedType.U
public short wSuiteMask;
public byte wProductType;
public byte wReserved;
}
[DllImport("kernel32.dll",
public static extern bool GetVersionExA([MarshalAs(U
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.ThrowExceptionForH
}
return isDC;
}
Any help would be greatly appreciated!
ASKER
I don't need the OS version, I need to know if it's a Domain Controller or not.
But thanks for the input!
But thanks for the input!
Do you still have the VB.NET code?
Bob
Bob
ASKER
sure:
<StructLayout(LayoutKind.S equential) > _
Public Structure OSVERSIONINFOEX
<MarshalAs(UnmanagedType.U 4)> _
Public dwOSVersionInfoSize As Integer
<MarshalAs(UnmanagedType.U 4)> _
Public dwMajorVersion As Integer
<MarshalAs(UnmanagedType.U 4)> _
Public dwMinorVersion As Integer
<MarshalAs(UnmanagedType.U 4)> _
Public dwBuildNumber As Integer
<MarshalAs(UnmanagedType.U 4)> _
Public dwPlatformId As Integer
<MarshalAs(UnmanagedType.B yValArray, SizeConst:=128)> _
Public szCSDVersion() As Char
<MarshalAs(UnmanagedType.U 2)> _
Public wServicePackMajor As Short
<MarshalAs(UnmanagedType.U 2)> _
Public wServicePackMinor As Short
<MarshalAs(UnmanagedType.U 2)> _
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)
<StructLayout(LayoutKind.S
Public Structure OSVERSIONINFOEX
<MarshalAs(UnmanagedType.U
Public dwOSVersionInfoSize As Integer
<MarshalAs(UnmanagedType.U
Public dwMajorVersion As Integer
<MarshalAs(UnmanagedType.U
Public dwMinorVersion As Integer
<MarshalAs(UnmanagedType.U
Public dwBuildNumber As Integer
<MarshalAs(UnmanagedType.U
Public dwPlatformId As Integer
<MarshalAs(UnmanagedType.B
Public szCSDVersion() As Char
<MarshalAs(UnmanagedType.U
Public wServicePackMajor As Short
<MarshalAs(UnmanagedType.U
Public wServicePackMinor As Short
<MarshalAs(UnmanagedType.U
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.
Usage:
Dim os As OSVERSIONINFOEX = New OSVERSIONINFOEX
os.dwOSVersionInfoSize = Marshal.SizeOf(os)
GetVersionEx(os)
In VB.NET, the os.dwOSVersionInfoSize = 156, and in C#, it is 32. Investigating...
Bob
Bob
ASKER
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!
Thanks!
The OSVERSIONINFOEX.szCSDVersi on 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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
that didn't work, but this did:
[MarshalAs(UnmanagedType.B yValTStr, SizeConst=128)]
public string szCSDVersion;
The size is now 156, but it still doesn't work...
[MarshalAs(UnmanagedType.B
public string szCSDVersion;
The size is now 156, but it still doesn't work...
It worked for me--return a false. What .NET version do you have? I have 2003.
Bob
Bob
ASKER
I have 2003 with .NET 1.1 SP1 too... Strange...
What happened when you used this?
[MarshalAs(UnmanagedType.B yValArray , SizeConst=128)]
public char[] szCSDVersion;
Bob
[MarshalAs(UnmanagedType.B
public char[] szCSDVersion;
Bob
ASKER
So sorry, didn't change public char[] szCSDVersion;
I'll test it and get back to u ASAP.
I'll test it and get back to u ASAP.
ASKER
Thanks alot, man. Works great!
string osVersion = Environment.OSVersion.Plat
Bob