GetVersionEx Implementation Problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sabin Finateanu

    GetVersionEx Implementation Problem

    Hi! I have this method that gets the OS suite and product type, but it
    is always returning null. Can anyone help me, please? Here is the method
    and other necessary code:

    [StructLayout(La youtKind.Sequen tial)]
    private struct OSVERSIONINFOEX
    {
    public int dwOSVersionInfo Size;
    public int dwMajorVersion;
    public int dwMinorVersion;
    public int dwBuildNumber;
    public int dwPlatformId;
    [MarshalAs(Unman agedType.ByValT Str, SizeConst = 128)]
    public string szCSDVersion;
    public int wServicePackMaj or;
    public int wServicePackMin or;
    public int wSuiteMask;
    public byte wProductType;
    public byte wReserved;
    }

    [DllImport("kern el32.dll", EntryPoint="Get VersionEx")]
    private static extern bool GetVersionEx(re f OSVERSIONINFOEX
    osVersionInfo);

    #region Private Constants
    private const Int32 VER_NT_WORKSTAT ION = 1;
    private const Int32 VER_NT_DOMAIN_C ONTROLLER = 2;
    private const Int32 VER_NT_SERVER = 3;
    private const Int32 VER_SUITE_SMALL BUSINESS = 1;
    private const Int32 VER_SUITE_ENTER PRISE = 2;
    private const Int32 VER_SUITE_TERMI NAL = 16;
    private const Int32 VER_SUITE_DATAC ENTER = 128;
    private const Int32 VER_SUITE_SINGL EUSERTS = 256;
    private const Int32 VER_SUITE_PERSO NAL = 512;
    private const Int32 VER_SUITE_BLADE = 1024;
    #endregion


    /// <summary>
    ///
    /// </summary>
    /// <returns></returns>
    public static string OSProductType()
    {
    OSVERSIONINFOEX osVersionInfo = new OSVERSIONINFOEX ();
    OperatingSystem osInfo = Environment.OSV ersion;

    osVersionInfo.d wOSVersionInfoS ize = Marshal.SizeOf( osVersionInfo);

    if(!GetVersionE x(ref osVersionInfo))
    {
    return "";
    }
    else
    {
    if(osInfo.Versi on.Major == 4)
    {
    if(osVersionInf o.wProductType == VER_NT_WORKSTAT ION)
    {
    // Windows NT 4.0 Workstation
    return " Workstation";
    }
    else if(osVersionInf o.wProductType == VER_NT_SERVER)
    {
    // Windows NT 4.0 Server
    return " Server";
    }
    else
    {
    return "";
    }
    }
    else if(osInfo.Versi on.Major == 5)
    {
    if(osVersionInf o.wProductType == VER_NT_WORKSTAT ION)
    {
    if((osVersionIn fo.wSuiteMask & VER_SUITE_PERSO NAL) ==
    VER_SUITE_PERSO NAL)
    {
    // Windows XP Home Edition
    return " Home Edition";
    }
    else
    {
    // Windows XP / Windows 2000 Professional
    return " Professional";
    }
    }
    else if(osVersionInf o.wProductType == VER_NT_SERVER)
    {
    if(osInfo.Versi on.Minor == 0)
    {
    if((osVersionIn fo.wSuiteMask & VER_SUITE_DATAC ENTER) ==
    VER_SUITE_DATAC ENTER)
    {
    // Windows 2000 Datacenter Server
    return " Datacenter Server";
    }
    else if((osVersionIn fo.wSuiteMask & VER_SUITE_ENTER PRISE) ==
    VER_SUITE_ENTER PRISE)
    {
    // Windows 2000 Advanced Server
    return " Advanced Server";
    }
    else
    {
    // Windows 2000 Server
    return " Server";
    }
    }
    else
    {
    if((osVersionIn fo.wSuiteMask & VER_SUITE_DATAC ENTER) ==
    VER_SUITE_DATAC ENTER)
    {
    // Windows Server 2003 Datacenter Edition
    return " Datacenter Edition";
    }
    else if((osVersionIn fo.wSuiteMask & VER_SUITE_ENTER PRISE) ==
    VER_SUITE_ENTER PRISE)
    {
    // Windows Server 2003 Enterprise Edition
    return " Enterprise Edition";
    }
    else if((osVersionIn fo.wSuiteMask & VER_SUITE_BLADE ) ==
    VER_SUITE_BLADE )
    {
    // Windows Server 2003 Web Edition
    return " Web Edition";
    }
    else
    {
    // Windows Server 2003 Standard Edition
    return " Standard Edition";
    }
    }
    }
    }
    }

    return "";
    }
  • Sabin Finateanu

    #2
    Re: GetVersionEx Implementation Problem

    I wanted to use the OS Version but that property doesn't give me any
    information on the OS suite or product type. That's why i did it this
    way. Nicholas Paldino [.NET/C# MVP] wrote:[color=blue]
    > Sabin,
    >
    > Why not use the static OSVersion property on the Environment class? It
    > should give you a good deal of what you want.
    >
    > If that doesn't give you want you want, have you tried looking for this
    > definition on http://www.pinvoke.net?
    >
    > Hope this helps.
    >
    >[/color]

    I wanted to use the OS Version but that property doesn't give me any
    information on the OS suite or product type. That's why i did it this way.

    Comment

    • Sabin Finateanu

      #3
      Re: GetVersionEx Implementation Problem

      Willy Denoyette [MVP] wrote:[color=blue]
      > "Sabin Finateanu" <fsabin@rdslink .ro> wrote in message
      > news:%23Z$$5zhj FHA.796@TK2MSFT NGP10.phx.gbl.. .
      >[color=green]
      >>Hi! I have this method that gets the OS suite and product type, but it is
      >>always returning null. Can anyone help me, please? Here is the method and
      >>other necessary code:
      >>
      >>[StructLayout(La youtKind.Sequen tial)]
      >>private struct OSVERSIONINFOEX
      >>{
      >>public int dwOSVersionInfo Size;
      >>public int dwMajorVersion;
      >>public int dwMinorVersion;
      >>public int dwBuildNumber;
      >>public int dwPlatformId;
      >>[MarshalAs(Unman agedType.ByValT Str, SizeConst = 128)]
      >>public string szCSDVersion;
      >>public int wServicePackMaj or;
      >>public int wServicePackMin or;
      >>public int wSuiteMask;
      >>public byte wProductType;
      >>public byte wReserved;
      >>}
      >>[/color]
      >
      > A firts look only...
      >
      > public short wServicePackMaj or;
      > public short wServicePackMin or;
      > public short wSuiteMask;
      >
      > Willy.
      >
      >
      >[/color]
      I've made some debugging and it seems that the GetVersionEx always
      returns false. Can anyone tell me whay?

      Comment

      • Willy Denoyette [MVP]

        #4
        Re: GetVersionEx Implementation Problem

        Did you made the changes I posted?

        [color=blue][color=green][color=darkred]
        >>>public int wServicePackMaj or;
        >>>public int wServicePackMin or;
        >>>public int wSuiteMask;[/color][/color][/color]

        Should be...

        public short wServicePackMaj or;
        public short wServicePackMin or;
        public short wSuiteMask;

        Willy.

        "Sabin Finateanu" <fsabin@rdslink .ro> wrote in message
        news:%23jN5gNij FHA.3608@TK2MSF TNGP12.phx.gbl. ..[color=blue]
        > Willy Denoyette [MVP] wrote:[color=green]
        >> "Sabin Finateanu" <fsabin@rdslink .ro> wrote in message
        >> news:%23Z$$5zhj FHA.796@TK2MSFT NGP10.phx.gbl.. .
        >>[color=darkred]
        >>>Hi! I have this method that gets the OS suite and product type, but it is
        >>>always returning null. Can anyone help me, please? Here is the method and
        >>>other necessary code:
        >>>
        >>>[StructLayout(La youtKind.Sequen tial)]
        >>>private struct OSVERSIONINFOEX
        >>>{
        >>>public int dwOSVersionInfo Size;
        >>>public int dwMajorVersion;
        >>>public int dwMinorVersion;
        >>>public int dwBuildNumber;
        >>>public int dwPlatformId;
        >>>[MarshalAs(Unman agedType.ByValT Str, SizeConst = 128)]
        >>>public string szCSDVersion;
        >>>public int wServicePackMaj or;
        >>>public int wServicePackMin or;
        >>>public int wSuiteMask;
        >>>public byte wProductType;
        >>>public byte wReserved;
        >>>}
        >>>[/color]
        >>
        >> A firts look only...
        >>
        >> public short wServicePackMaj or;
        >> public short wServicePackMin or;
        >> public short wSuiteMask;
        >>
        >> Willy.
        >>
        >>
        >>[/color]
        > I've made some debugging and it seems that the GetVersionEx always
        > returns false. Can anyone tell me whay?[/color]


        Comment

        • Sabin Finateanu

          #5
          Re: GetVersionEx Implementation Problem

          Willy Denoyette [MVP] wrote:[color=blue]
          > Did you made the changes I posted?
          >
          >
          >[color=green][color=darkred]
          >>>>public int wServicePackMaj or;
          >>>>public int wServicePackMin or;
          >>>>public int wSuiteMask;[/color][/color]
          >
          >
          > Should be...
          >
          > public short wServicePackMaj or;
          > public short wServicePackMin or;
          > public short wSuiteMask;
          >
          > Willy.
          >
          > "Sabin Finateanu" <fsabin@rdslink .ro> wrote in message
          > news:%23jN5gNij FHA.3608@TK2MSF TNGP12.phx.gbl. ..
          >[color=green]
          >>Willy Denoyette [MVP] wrote:
          >>[color=darkred]
          >>>"Sabin Finateanu" <fsabin@rdslink .ro> wrote in message
          >>>news:%23Z$$5 zhjFHA.796@TK2M SFTNGP10.phx.gb l...
          >>>
          >>>
          >>>>Hi! I have this method that gets the OS suite and product type, but it is
          >>>>always returning null. Can anyone help me, please? Here is the method and
          >>>>other necessary code:
          >>>>
          >>>>[StructLayout(La youtKind.Sequen tial)]
          >>>>private struct OSVERSIONINFOEX
          >>>>{
          >>>>public int dwOSVersionInfo Size;
          >>>>public int dwMajorVersion;
          >>>>public int dwMinorVersion;
          >>>>public int dwBuildNumber;
          >>>>public int dwPlatformId;
          >>>>[MarshalAs(Unman agedType.ByValT Str, SizeConst = 128)]
          >>>>public string szCSDVersion;
          >>>>public int wServicePackMaj or;
          >>>>public int wServicePackMin or;
          >>>>public int wSuiteMask;
          >>>>public byte wProductType;
          >>>>public byte wReserved;
          >>>>}
          >>>>
          >>>
          >>>A firts look only...
          >>>
          >>>public short wServicePackMaj or;
          >>>public short wServicePackMin or;
          >>>public short wSuiteMask;
          >>>
          >>>Willy.
          >>>
          >>>
          >>>[/color]
          >>
          >> I've made some debugging and it seems that the GetVersionEx always
          >>returns false. Can anyone tell me whay?[/color]
          >
          >
          >[/color]
          Now it works. I just realized that uint16 = short. I forgot that.
          Thank you!

          Comment

          • Nicholas Paldino [.NET/C# MVP]

            #6
            Re: GetVersionEx Implementation Problem

            Sabin,

            Why not use the static OSVersion property on the Environment class? It
            should give you a good deal of what you want.

            If that doesn't give you want you want, have you tried looking for this
            definition on http://www.pinvoke.net?

            Hope this helps.


            --
            - Nicholas Paldino [.NET/C# MVP]
            - mvp@spam.guard. caspershouse.co m

            "Sabin Finateanu" <fsabin@rdslink .ro> wrote in message
            news:%23Z$$5zhj FHA.796@TK2MSFT NGP10.phx.gbl.. .[color=blue]
            > Hi! I have this method that gets the OS suite and product type, but it is
            > always returning null. Can anyone help me, please? Here is the method and
            > other necessary code:
            >
            > [StructLayout(La youtKind.Sequen tial)]
            > private struct OSVERSIONINFOEX
            > {
            > public int dwOSVersionInfo Size;
            > public int dwMajorVersion;
            > public int dwMinorVersion;
            > public int dwBuildNumber;
            > public int dwPlatformId;
            > [MarshalAs(Unman agedType.ByValT Str, SizeConst = 128)]
            > public string szCSDVersion;
            > public int wServicePackMaj or;
            > public int wServicePackMin or;
            > public int wSuiteMask;
            > public byte wProductType;
            > public byte wReserved;
            > }
            >
            > [DllImport("kern el32.dll", EntryPoint="Get VersionEx")]
            > private static extern bool GetVersionEx(re f OSVERSIONINFOEX
            > osVersionInfo);
            >
            > #region Private Constants
            > private const Int32 VER_NT_WORKSTAT ION = 1;
            > private const Int32 VER_NT_DOMAIN_C ONTROLLER = 2;
            > private const Int32 VER_NT_SERVER = 3;
            > private const Int32 VER_SUITE_SMALL BUSINESS = 1;
            > private const Int32 VER_SUITE_ENTER PRISE = 2;
            > private const Int32 VER_SUITE_TERMI NAL = 16;
            > private const Int32 VER_SUITE_DATAC ENTER = 128;
            > private const Int32 VER_SUITE_SINGL EUSERTS = 256;
            > private const Int32 VER_SUITE_PERSO NAL = 512;
            > private const Int32 VER_SUITE_BLADE = 1024;
            > #endregion
            >
            >
            > /// <summary>
            > ///
            > /// </summary>
            > /// <returns></returns>
            > public static string OSProductType()
            > {
            > OSVERSIONINFOEX osVersionInfo = new OSVERSIONINFOEX ();
            > OperatingSystem osInfo = Environment.OSV ersion;
            >
            > osVersionInfo.d wOSVersionInfoS ize = Marshal.SizeOf( osVersionInfo);
            >
            > if(!GetVersionE x(ref osVersionInfo))
            > {
            > return "";
            > }
            > else
            > {
            > if(osInfo.Versi on.Major == 4)
            > {
            > if(osVersionInf o.wProductType == VER_NT_WORKSTAT ION)
            > {
            > // Windows NT 4.0 Workstation
            > return " Workstation";
            > }
            > else if(osVersionInf o.wProductType == VER_NT_SERVER)
            > {
            > // Windows NT 4.0 Server
            > return " Server";
            > }
            > else
            > {
            > return "";
            > }
            > }
            > else if(osInfo.Versi on.Major == 5)
            > {
            > if(osVersionInf o.wProductType == VER_NT_WORKSTAT ION)
            > {
            > if((osVersionIn fo.wSuiteMask & VER_SUITE_PERSO NAL) == VER_SUITE_PERSO NAL)
            > {
            > // Windows XP Home Edition
            > return " Home Edition";
            > }
            > else
            > {
            > // Windows XP / Windows 2000 Professional
            > return " Professional";
            > }
            > }
            > else if(osVersionInf o.wProductType == VER_NT_SERVER)
            > {
            > if(osInfo.Versi on.Minor == 0)
            > {
            > if((osVersionIn fo.wSuiteMask & VER_SUITE_DATAC ENTER) ==
            > VER_SUITE_DATAC ENTER)
            > {
            > // Windows 2000 Datacenter Server
            > return " Datacenter Server";
            > }
            > else if((osVersionIn fo.wSuiteMask & VER_SUITE_ENTER PRISE) ==
            > VER_SUITE_ENTER PRISE)
            > {
            > // Windows 2000 Advanced Server
            > return " Advanced Server";
            > }
            > else
            > {
            > // Windows 2000 Server
            > return " Server";
            > }
            > }
            > else
            > {
            > if((osVersionIn fo.wSuiteMask & VER_SUITE_DATAC ENTER) ==
            > VER_SUITE_DATAC ENTER)
            > {
            > // Windows Server 2003 Datacenter Edition
            > return " Datacenter Edition";
            > }
            > else if((osVersionIn fo.wSuiteMask & VER_SUITE_ENTER PRISE) ==
            > VER_SUITE_ENTER PRISE)
            > {
            > // Windows Server 2003 Enterprise Edition
            > return " Enterprise Edition";
            > }
            > else if((osVersionIn fo.wSuiteMask & VER_SUITE_BLADE ) == VER_SUITE_BLADE )
            > {
            > // Windows Server 2003 Web Edition
            > return " Web Edition";
            > }
            > else
            > {
            > // Windows Server 2003 Standard Edition
            > return " Standard Edition";
            > }
            > }
            > }
            > }
            > }
            >
            > return "";
            > }[/color]


            Comment

            • Willy Denoyette [MVP]

              #7
              Re: GetVersionEx Implementation Problem


              "Sabin Finateanu" <fsabin@rdslink .ro> wrote in message
              news:%23Z$$5zhj FHA.796@TK2MSFT NGP10.phx.gbl.. .[color=blue]
              > Hi! I have this method that gets the OS suite and product type, but it is
              > always returning null. Can anyone help me, please? Here is the method and
              > other necessary code:
              >
              > [StructLayout(La youtKind.Sequen tial)]
              > private struct OSVERSIONINFOEX
              > {
              > public int dwOSVersionInfo Size;
              > public int dwMajorVersion;
              > public int dwMinorVersion;
              > public int dwBuildNumber;
              > public int dwPlatformId;
              > [MarshalAs(Unman agedType.ByValT Str, SizeConst = 128)]
              > public string szCSDVersion;
              > public int wServicePackMaj or;
              > public int wServicePackMin or;
              > public int wSuiteMask;
              > public byte wProductType;
              > public byte wReserved;
              > }
              >[/color]
              A firts look only...

              public short wServicePackMaj or;
              public short wServicePackMin or;
              public short wSuiteMask;

              Willy.



              Comment

              • Sabin Finateanu

                #8
                Re: GetVersionEx Implementation Problem

                I wanted to use the OS Version but that property doesn't give me any
                information on the OS suite or product type. That's why i did it this
                way. Nicholas Paldino [.NET/C# MVP] wrote:[color=blue]
                > Sabin,
                >
                > Why not use the static OSVersion property on the Environment class? It
                > should give you a good deal of what you want.
                >
                > If that doesn't give you want you want, have you tried looking for this
                > definition on http://www.pinvoke.net?
                >
                > Hope this helps.
                >
                >[/color]

                I wanted to use the OS Version but that property doesn't give me any
                information on the OS suite or product type. That's why i did it this way.

                Comment

                • Sabin Finateanu

                  #9
                  Re: GetVersionEx Implementation Problem

                  Willy Denoyette [MVP] wrote:[color=blue]
                  > "Sabin Finateanu" <fsabin@rdslink .ro> wrote in message
                  > news:%23Z$$5zhj FHA.796@TK2MSFT NGP10.phx.gbl.. .
                  >[color=green]
                  >>Hi! I have this method that gets the OS suite and product type, but it is
                  >>always returning null. Can anyone help me, please? Here is the method and
                  >>other necessary code:
                  >>
                  >>[StructLayout(La youtKind.Sequen tial)]
                  >>private struct OSVERSIONINFOEX
                  >>{
                  >>public int dwOSVersionInfo Size;
                  >>public int dwMajorVersion;
                  >>public int dwMinorVersion;
                  >>public int dwBuildNumber;
                  >>public int dwPlatformId;
                  >>[MarshalAs(Unman agedType.ByValT Str, SizeConst = 128)]
                  >>public string szCSDVersion;
                  >>public int wServicePackMaj or;
                  >>public int wServicePackMin or;
                  >>public int wSuiteMask;
                  >>public byte wProductType;
                  >>public byte wReserved;
                  >>}
                  >>[/color]
                  >
                  > A firts look only...
                  >
                  > public short wServicePackMaj or;
                  > public short wServicePackMin or;
                  > public short wSuiteMask;
                  >
                  > Willy.
                  >
                  >
                  >[/color]
                  I've made some debugging and it seems that the GetVersionEx always
                  returns false. Can anyone tell me whay?

                  Comment

                  • Willy Denoyette [MVP]

                    #10
                    Re: GetVersionEx Implementation Problem

                    Did you made the changes I posted?

                    [color=blue][color=green][color=darkred]
                    >>>public int wServicePackMaj or;
                    >>>public int wServicePackMin or;
                    >>>public int wSuiteMask;[/color][/color][/color]

                    Should be...

                    public short wServicePackMaj or;
                    public short wServicePackMin or;
                    public short wSuiteMask;

                    Willy.

                    "Sabin Finateanu" <fsabin@rdslink .ro> wrote in message
                    news:%23jN5gNij FHA.3608@TK2MSF TNGP12.phx.gbl. ..[color=blue]
                    > Willy Denoyette [MVP] wrote:[color=green]
                    >> "Sabin Finateanu" <fsabin@rdslink .ro> wrote in message
                    >> news:%23Z$$5zhj FHA.796@TK2MSFT NGP10.phx.gbl.. .
                    >>[color=darkred]
                    >>>Hi! I have this method that gets the OS suite and product type, but it is
                    >>>always returning null. Can anyone help me, please? Here is the method and
                    >>>other necessary code:
                    >>>
                    >>>[StructLayout(La youtKind.Sequen tial)]
                    >>>private struct OSVERSIONINFOEX
                    >>>{
                    >>>public int dwOSVersionInfo Size;
                    >>>public int dwMajorVersion;
                    >>>public int dwMinorVersion;
                    >>>public int dwBuildNumber;
                    >>>public int dwPlatformId;
                    >>>[MarshalAs(Unman agedType.ByValT Str, SizeConst = 128)]
                    >>>public string szCSDVersion;
                    >>>public int wServicePackMaj or;
                    >>>public int wServicePackMin or;
                    >>>public int wSuiteMask;
                    >>>public byte wProductType;
                    >>>public byte wReserved;
                    >>>}
                    >>>[/color]
                    >>
                    >> A firts look only...
                    >>
                    >> public short wServicePackMaj or;
                    >> public short wServicePackMin or;
                    >> public short wSuiteMask;
                    >>
                    >> Willy.
                    >>
                    >>
                    >>[/color]
                    > I've made some debugging and it seems that the GetVersionEx always
                    > returns false. Can anyone tell me whay?[/color]


                    Comment

                    • Sabin Finateanu

                      #11
                      Re: GetVersionEx Implementation Problem

                      Willy Denoyette [MVP] wrote:[color=blue]
                      > Did you made the changes I posted?
                      >
                      >
                      >[color=green][color=darkred]
                      >>>>public int wServicePackMaj or;
                      >>>>public int wServicePackMin or;
                      >>>>public int wSuiteMask;[/color][/color]
                      >
                      >
                      > Should be...
                      >
                      > public short wServicePackMaj or;
                      > public short wServicePackMin or;
                      > public short wSuiteMask;
                      >
                      > Willy.
                      >
                      > "Sabin Finateanu" <fsabin@rdslink .ro> wrote in message
                      > news:%23jN5gNij FHA.3608@TK2MSF TNGP12.phx.gbl. ..
                      >[color=green]
                      >>Willy Denoyette [MVP] wrote:
                      >>[color=darkred]
                      >>>"Sabin Finateanu" <fsabin@rdslink .ro> wrote in message
                      >>>news:%23Z$$5 zhjFHA.796@TK2M SFTNGP10.phx.gb l...
                      >>>
                      >>>
                      >>>>Hi! I have this method that gets the OS suite and product type, but it is
                      >>>>always returning null. Can anyone help me, please? Here is the method and
                      >>>>other necessary code:
                      >>>>
                      >>>>[StructLayout(La youtKind.Sequen tial)]
                      >>>>private struct OSVERSIONINFOEX
                      >>>>{
                      >>>>public int dwOSVersionInfo Size;
                      >>>>public int dwMajorVersion;
                      >>>>public int dwMinorVersion;
                      >>>>public int dwBuildNumber;
                      >>>>public int dwPlatformId;
                      >>>>[MarshalAs(Unman agedType.ByValT Str, SizeConst = 128)]
                      >>>>public string szCSDVersion;
                      >>>>public int wServicePackMaj or;
                      >>>>public int wServicePackMin or;
                      >>>>public int wSuiteMask;
                      >>>>public byte wProductType;
                      >>>>public byte wReserved;
                      >>>>}
                      >>>>
                      >>>
                      >>>A firts look only...
                      >>>
                      >>>public short wServicePackMaj or;
                      >>>public short wServicePackMin or;
                      >>>public short wSuiteMask;
                      >>>
                      >>>Willy.
                      >>>
                      >>>
                      >>>[/color]
                      >>
                      >> I've made some debugging and it seems that the GetVersionEx always
                      >>returns false. Can anyone tell me whay?[/color]
                      >
                      >
                      >[/color]
                      Now it works. I just realized that uint16 = short. I forgot that.
                      Thank you!

                      Comment

                      Working...