Description





Description

This enumeration is used by the DllImportAttribute to indicate the required modifications to the System.String arguments of an imported function.

[Note: See the DllImportAttribute class overview for an example that uses the CharSet enumeration.]

Example

using System;

using System.Runtime.InteropServices;



/// <summary>

/// Sample demonstrating the use of the CharSet enumeration. 

/// Use this enumeration to specify how strings should be marshalled to 

/// unmanaged code for methods decorated with DllImportAttribute and types 

/// decorated with StructLayoutAttribute.

/// </summary>

internal class CharSetSample

{



    private static void Main()

    {

        OSVERSIONINFO ovi = new OSVERSIONINFO();



        // Given that CharSet.Auto was used for OSVERSIONINFO, we can work out 

        // how strings are being marshalled by getting the unmanaged size of 

        // OSVERSIONINFO.

        ovi.dwOSVersionInfoSize = (uint)Marshal.SizeOf(typeof(OSVERSIONINFO));



        switch (ovi.dwOSVersionInfoSize)

        {



            case 148:

                Console.WriteLine(

                    "OSVERSIONINFO strings will be marshalled as ANSI.");

                break;



            case 276:

                Console.WriteLine(

                    "OSVERSIONINFO strings will be marshalled as Unicode.");

                break;



            default:

                Console.WriteLine(

                    "Unknown marshalling for OSVERSIONINFO strings.");

                break;



        }



        if (GetVersionEx(ref ovi))

        {

            Console.Write(

                "Detected OS version: {0}.{1}.{2}", 

                ovi.dwMajorVersion, ovi.dwMinorVersion, ovi.dwBuildNumber);



            if (ovi.szCSDVersion.Length > 0)

            {

                Console.Write(" ({0})", ovi.szCSDVersion);

            }



            Console.WriteLine();

        }

        else

        {

            Console.WriteLine("GetVersionEx failed.");

        }



        Console.WriteLine();

        Console.WriteLine();

        Console.WriteLine("Press Enter to continue");

        Console.ReadLine();

    }



    // The OSVERSIONINFO type to use with GetVersionEx. Note that CharSet.Auto 

    // is used so the type of string marshalling used is based on the OS.

    [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]

    private struct OSVERSIONINFO

    {

        public uint dwOSVersionInfoSize;

        public uint dwMajorVersion;

        public uint dwMinorVersion;

        public uint dwBuildNumber;

        public uint dwPlatformId;

        // Note that szCSDVersion is actually limited to SizeConst - 1 chars.

        [MarshalAs(UnmanagedType.ByValTStr, SizeConst=128)]

        public string szCSDVersion;

    }



    // Win32 method to retrieve information about the OS. .NET code would 

    // typically use Environment.OSVersion, although it's worth noting that 

    // GetVersionEx can retrieve the service pack string as well. Note that 

    // CharSet.Auto is used so the type of string marshalling used is based on 

    // the OS (Unicode on WinNT family and ANSI on Win9x family).

    [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]

    private static extern bool GetVersionEx(

        ref OSVERSIONINFO lpVersionInformation);



}


The output is


OSVERSIONINFO strings will be marshalled as Unicode.

Detected OS version: 5.1.2600 (Service Pack 2)





Press Enter to continue



     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows