<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://nfex.ru/index.php?action=history&amp;feed=atom&amp;title=Csharp%2FCSharp_Tutorial%2FWindows%2FNative_Windows_Function</id>
		<title>Csharp/CSharp Tutorial/Windows/Native Windows Function - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://nfex.ru/index.php?action=history&amp;feed=atom&amp;title=Csharp%2FCSharp_Tutorial%2FWindows%2FNative_Windows_Function"/>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/Windows/Native_Windows_Function&amp;action=history"/>
		<updated>2026-04-29T23:11:05Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/Windows/Native_Windows_Function&amp;diff=6718&amp;oldid=prev</id>
		<title> в 15:31, 26 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/Windows/Native_Windows_Function&amp;diff=6718&amp;oldid=prev"/>
				<updated>2010-05-26T15:31:53Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 15:31, 26 мая 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
			</entry>

	<entry>
		<id>http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/Windows/Native_Windows_Function&amp;diff=6719&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/Windows/Native_Windows_Function&amp;diff=6719&amp;oldid=prev"/>
				<updated>2010-05-26T12:20:18Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==Calling a Function with a Structure Parameter==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;// Code from &lt;br /&gt;
// A Programmer&amp;quot;s Introduction to C# 2.0, Third Edition&lt;br /&gt;
// copyright 2000 Eric Gunnerson&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
struct Point&lt;br /&gt;
{&lt;br /&gt;
    public int x;&lt;br /&gt;
    public int y;&lt;br /&gt;
    &lt;br /&gt;
    public override string ToString()&lt;br /&gt;
    {&lt;br /&gt;
        return(String.Format(&amp;quot;({0}, {1})&amp;quot;, x, y));&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
struct Rect&lt;br /&gt;
{&lt;br /&gt;
    public int left;&lt;br /&gt;
    public int top;&lt;br /&gt;
    public int right;&lt;br /&gt;
    public int bottom;&lt;br /&gt;
    &lt;br /&gt;
    public override string ToString()&lt;br /&gt;
    {&lt;br /&gt;
        return(String.Format(&amp;quot;({0}, {1})\n    ({2}, {3})&amp;quot;, left, top, right, bottom));&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
struct WindowPlacement&lt;br /&gt;
{&lt;br /&gt;
    public uint length;&lt;br /&gt;
    public uint flags;&lt;br /&gt;
    public uint showCmd;&lt;br /&gt;
    public Point minPosition;&lt;br /&gt;
    public Point maxPosition;&lt;br /&gt;
    public Rect normalPosition;    &lt;br /&gt;
    &lt;br /&gt;
    public override string ToString()&lt;br /&gt;
    {&lt;br /&gt;
        return(String.Format(&amp;quot;min, max, normal:\n{0}\n{1}\n{2}&amp;quot;,&lt;br /&gt;
        minPosition, maxPosition, normalPosition));&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
class MainClass&lt;br /&gt;
{&lt;br /&gt;
    [DllImport(&amp;quot;user32&amp;quot;)]&lt;br /&gt;
    static extern IntPtr GetForegroundWindow();&lt;br /&gt;
    &lt;br /&gt;
    [DllImport(&amp;quot;user32&amp;quot;)]&lt;br /&gt;
    static extern bool GetWindowPlacement(IntPtr handle, ref WindowPlacement wp);&lt;br /&gt;
    &lt;br /&gt;
    public static void Main()&lt;br /&gt;
    {&lt;br /&gt;
        IntPtr window = GetForegroundWindow();&lt;br /&gt;
        &lt;br /&gt;
        WindowPlacement wp = new WindowPlacement();&lt;br /&gt;
        wp.length = (uint) Marshal.SizeOf(wp);&lt;br /&gt;
        &lt;br /&gt;
        bool result = GetWindowPlacement(window, ref wp);&lt;br /&gt;
        &lt;br /&gt;
        if (result)&lt;br /&gt;
        {&lt;br /&gt;
            Console.WriteLine(wp);&lt;br /&gt;
        }&lt;br /&gt;
    } &lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;min, max, normal:&lt;br /&gt;
(-32000, -32000)&lt;br /&gt;
(-60, -8)&lt;br /&gt;
(-1, -1)&lt;br /&gt;
    (1273, 728)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Calling Native DLL Functions==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
class Test&lt;br /&gt;
{&lt;br /&gt;
    [DllImport(&amp;quot;user32.dll&amp;quot;)]&lt;br /&gt;
    public static extern int MessageBox(IntPtr h, string m, string c, int type);&lt;br /&gt;
    public static void Main()&lt;br /&gt;
    {&lt;br /&gt;
        int retval = MessageBox(IntPtr.Zero, &amp;quot;Hello&amp;quot;, &amp;quot;Caption&amp;quot;, 0);&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Enumerate Display Monitors==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;/*&lt;br /&gt;
NET Development for Java Programmers&lt;br /&gt;
# By Paul Gibbons&lt;br /&gt;
# ISBN: 1-59059-038-4&lt;br /&gt;
# 432 pp.&lt;br /&gt;
# Published: Jul 2002&lt;br /&gt;
*/&lt;br /&gt;
 &lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
[ StructLayout( LayoutKind.Sequential ) ]&lt;br /&gt;
struct Rect&lt;br /&gt;
{&lt;br /&gt;
  public int left;&lt;br /&gt;
  public int top;&lt;br /&gt;
  public int right;&lt;br /&gt;
  public int bottom;&lt;br /&gt;
}&lt;br /&gt;
[ StructLayout( LayoutKind.Sequential ) ]&lt;br /&gt;
struct MonitorInfo&lt;br /&gt;
{&lt;br /&gt;
  public uint size;&lt;br /&gt;
  public Rect monitor;&lt;br /&gt;
  public Rect work;&lt;br /&gt;
  public uint flags;&lt;br /&gt;
}&lt;br /&gt;
class MainClass&lt;br /&gt;
{&lt;br /&gt;
  delegate bool MonitorEnumDelegate( IntPtr hMonitor,IntPtr hdcMonitor,ref Rect lprcMonitor, IntPtr dwData );&lt;br /&gt;
  [ DllImport( &amp;quot;user32.dll&amp;quot; ) ]&lt;br /&gt;
  static extern bool EnumDisplayMonitors( IntPtr hdc, IntPtr lprcClip, MonitorEnumDelegate lpfnEnum, IntPtr dwData );&lt;br /&gt;
  [ DllImport( &amp;quot;user32.dll&amp;quot; ) ]&lt;br /&gt;
  static extern bool GetMonitorInfo( IntPtr hmon, ref MonitorInfo mi );&lt;br /&gt;
  static bool MonitorEnum( IntPtr hMonitor, IntPtr hdcMonitor, ref Rect lprcMonitor,  IntPtr dwData )&lt;br /&gt;
  {&lt;br /&gt;
    MonitorInfo mi = new MonitorInfo();&lt;br /&gt;
    mi.size = (uint)Marshal.SizeOf( mi );&lt;br /&gt;
    bool success = GetMonitorInfo( hMonitor, ref mi );&lt;br /&gt;
    return true;&lt;br /&gt;
  }&lt;br /&gt;
  [STAThread]&lt;br /&gt;
  static void Main(string[] args)&lt;br /&gt;
  {&lt;br /&gt;
    MonitorEnumDelegate med = new MonitorEnumDelegate( MonitorEnum );&lt;br /&gt;
    EnumDisplayMonitors( IntPtr.Zero, IntPtr.Zero, med, IntPtr.Zero );&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Get Computer name (char * parameter)==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
class MainClass&lt;br /&gt;
{&lt;br /&gt;
  [ DllImport( &amp;quot;kernel32.dll&amp;quot; ) ]&lt;br /&gt;
  static extern unsafe bool GetComputerNameW( char* name, ref ulong size );&lt;br /&gt;
  [STAThread]&lt;br /&gt;
  static unsafe void Main(string[] args)&lt;br /&gt;
  {&lt;br /&gt;
    ulong size = 256;&lt;br /&gt;
    char* name = stackalloc char[ (int)size ];&lt;br /&gt;
    bool success = GetComputerNameW( name, ref size );&lt;br /&gt;
    for ( uint i = 0; i &amp;lt; size; i++, name++ )&lt;br /&gt;
    {&lt;br /&gt;
      System.Console.Write( *name );&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;nfex&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Get computer name (StringBuilder parameter)==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Text;&lt;br /&gt;
class MainClass{&lt;br /&gt;
  [ DllImport( &amp;quot;kernel32.dll&amp;quot; ) ]&lt;br /&gt;
  static extern bool GetComputerName( StringBuilder name, ref ulong size );&lt;br /&gt;
  [STAThread]&lt;br /&gt;
  static void Main(string[] args)&lt;br /&gt;
  {&lt;br /&gt;
    ulong size = 256;&lt;br /&gt;
    StringBuilder name = new StringBuilder( (int)size );&lt;br /&gt;
    bool success = GetComputerName( name, ref size );&lt;br /&gt;
    Console.WriteLine( name.ToString() );&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;nfex&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Get current Active Window==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
using System.Windows.Forms;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Text;&lt;br /&gt;
public class MainClass&lt;br /&gt;
    // Declare external functions.&lt;br /&gt;
    [DllImport(&amp;quot;user32.dll&amp;quot;)]&lt;br /&gt;
    private static extern IntPtr GetForegroundWindow();&lt;br /&gt;
    [DllImport(&amp;quot;user32.dll&amp;quot;)]&lt;br /&gt;
    private static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);&lt;br /&gt;
    public static void Main() {&lt;br /&gt;
        int chars = 256;&lt;br /&gt;
        StringBuilder buff = new StringBuilder(chars);&lt;br /&gt;
        // Obtain the handle of the active window.&lt;br /&gt;
        IntPtr handle = GetForegroundWindow();&lt;br /&gt;
        // Update the controls.&lt;br /&gt;
        if (GetWindowText(handle, buff, chars) &amp;gt; 0)&lt;br /&gt;
        {&lt;br /&gt;
            Console.WriteLine(buff.ToString());&lt;br /&gt;
            Console.WriteLine(handle.ToString());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Get free disk space==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
using System.IO;&lt;br /&gt;
using System.Reflection;&lt;br /&gt;
using System.Runtime.ConstrainedExecution;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using Microsoft.Win32.SafeHandles;&lt;br /&gt;
&lt;br /&gt;
public class MainClass&lt;br /&gt;
{&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, CharSet = CharSet.Auto)]&lt;br /&gt;
    static extern bool GetDiskFreeSpaceEx(string lpDirectoryName,&lt;br /&gt;
       out ulong lpFreeBytesAvailable,&lt;br /&gt;
       out ulong lpTotalNumberOfBytes,&lt;br /&gt;
       out ulong lpTotalNumberOfFreeBytes);&lt;br /&gt;
    public static void Main()&lt;br /&gt;
    {&lt;br /&gt;
        ulong freeBytesAvail;&lt;br /&gt;
        ulong totalNumOfBytes;&lt;br /&gt;
        ulong totalNumOfFreeBytes;&lt;br /&gt;
        if (!GetDiskFreeSpaceEx(&amp;quot;C:\\&amp;quot;, out freeBytesAvail, out totalNumOfBytes, out totalNumOfFreeBytes))&lt;br /&gt;
        {&lt;br /&gt;
            Console.Error.WriteLine(&amp;quot;Error occurred: {0}&amp;quot;,&lt;br /&gt;
                Marshal.GetExceptionForHR(Marshal.GetLastWin32Error()).Message);&lt;br /&gt;
        }&lt;br /&gt;
        else&lt;br /&gt;
        {&lt;br /&gt;
            Console.WriteLine(&amp;quot;Free disk space:&amp;quot;);&lt;br /&gt;
            Console.WriteLine(&amp;quot;    Available bytes : {0}&amp;quot;, freeBytesAvail);&lt;br /&gt;
            Console.WriteLine(&amp;quot;    Total # of bytes: {0}&amp;quot;, totalNumOfBytes);&lt;br /&gt;
            Console.WriteLine(&amp;quot;    Total free bytes: {0}&amp;quot;, totalNumOfFreeBytes);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;Free disk space:&lt;br /&gt;
    Available bytes : 33091416064&lt;br /&gt;
    Total # of bytes: 60003381248&lt;br /&gt;
    Total free bytes: 33091416064&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Get Monitor Information==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;/*        &lt;br /&gt;
Revised from &lt;br /&gt;
NET Development for Java Programmers&lt;br /&gt;
*/&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
[ StructLayout( LayoutKind.Explicit ) ]&lt;br /&gt;
struct Point&lt;br /&gt;
{&lt;br /&gt;
  [ FieldOffset( 0 ) ]&lt;br /&gt;
  public int x;&lt;br /&gt;
  [ FieldOffset( 4 ) ]&lt;br /&gt;
  public int y;&lt;br /&gt;
}&lt;br /&gt;
[ StructLayout( LayoutKind.Sequential ) ]&lt;br /&gt;
struct Rect&lt;br /&gt;
{&lt;br /&gt;
  public int left;&lt;br /&gt;
  public int top;&lt;br /&gt;
  public int right;&lt;br /&gt;
  public int bottom;&lt;br /&gt;
}&lt;br /&gt;
[ StructLayout( LayoutKind.Sequential ) ]&lt;br /&gt;
struct MonitorInfo&lt;br /&gt;
{&lt;br /&gt;
  public uint size;&lt;br /&gt;
  public Rect monitor;&lt;br /&gt;
  public Rect work;&lt;br /&gt;
  public uint flags;&lt;br /&gt;
}&lt;br /&gt;
class MainClass&lt;br /&gt;
{&lt;br /&gt;
  [ DllImport( &amp;quot;user32.dll&amp;quot; ) ]&lt;br /&gt;
  static extern IntPtr MonitorFromPoint( Point p, uint flags );&lt;br /&gt;
  [ DllImport( &amp;quot;user32.dll&amp;quot; ) ]&lt;br /&gt;
  static extern bool GetMonitorInfo( IntPtr hmon, ref MonitorInfo mi );&lt;br /&gt;
  [STAThread]&lt;br /&gt;
  static void Main(string[] args)&lt;br /&gt;
  {&lt;br /&gt;
    Point p = new Point();&lt;br /&gt;
    p.x = 1;&lt;br /&gt;
    p.y = 1;&lt;br /&gt;
    IntPtr hmon = MonitorFromPoint( p, 1 );&lt;br /&gt;
    MonitorInfo mi = new MonitorInfo();&lt;br /&gt;
    mi.size = (uint)Marshal.SizeOf( mi );&lt;br /&gt;
        bool success = GetMonitorInfo( hmon, ref mi );&lt;br /&gt;
    Console.WriteLine(mi.size);&lt;br /&gt;
    Console.WriteLine(mi.monitor);&lt;br /&gt;
    &lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;40&lt;br /&gt;
Rect&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==GetVersionEx by using kernel32.dll==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
[StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
public class OSVersionInfo&lt;br /&gt;
{&lt;br /&gt;
    public int dwOSVersionInfoSize;&lt;br /&gt;
    public int dwMajorVersion;&lt;br /&gt;
    public int dwMinorVersion;&lt;br /&gt;
    public int dwBuildNumber;&lt;br /&gt;
    public int dwPlatformId;&lt;br /&gt;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]&lt;br /&gt;
    public String szCSDVersion;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class MainClass&lt;br /&gt;
{&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    public static extern bool GetVersionEx([In, Out] OSVersionInfo osvi);&lt;br /&gt;
    static void Main(string[] args)&lt;br /&gt;
    {&lt;br /&gt;
        OSVersionInfo osvi = new OSVersionInfo();&lt;br /&gt;
        osvi.dwOSVersionInfoSize = Marshal.SizeOf(osvi);&lt;br /&gt;
        GetVersionEx(osvi);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Class size: &amp;quot; + osvi.dwOSVersionInfoSize);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Major Version: &amp;quot; + osvi.dwMajorVersion);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Minor Version: &amp;quot; + osvi.dwMinorVersion);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Build Number: &amp;quot; + osvi.dwBuildNumber);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Platform Id: &amp;quot; + osvi.dwPlatformId);&lt;br /&gt;
        Console.WriteLine(&amp;quot;CSD Version: &amp;quot; + osvi.szCSDVersion);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Platform: &amp;quot; + Environment.OSVersion.Platform);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Version: &amp;quot; + Environment.OSVersion.Version);&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;Class size: 148&lt;br /&gt;
Major Version: 5&lt;br /&gt;
Minor Version: 1&lt;br /&gt;
Build Number: 2600&lt;br /&gt;
Platform Id: 2&lt;br /&gt;
CSD Version: Service Pack 2&lt;br /&gt;
Platform: Win32NT&lt;br /&gt;
Version: 5.1.2600.131072&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Get Workstation information==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;/*&lt;br /&gt;
NET Development for Java Programmers&lt;br /&gt;
# By Paul Gibbons&lt;br /&gt;
# ISBN: 1-59059-038-4&lt;br /&gt;
# 432 pp.&lt;br /&gt;
# Published: Jul 2002&lt;br /&gt;
*/&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Text;&lt;br /&gt;
class NWGetInfo&lt;br /&gt;
{&lt;br /&gt;
  [ StructLayout( LayoutKind.Sequential ) ]&lt;br /&gt;
  struct WkstaInfo102&lt;br /&gt;
  {&lt;br /&gt;
    public uint platform_id;&lt;br /&gt;
    public IntPtr computername;&lt;br /&gt;
    public IntPtr langroup;&lt;br /&gt;
    public uint ver_major;&lt;br /&gt;
    public uint ver_minor;&lt;br /&gt;
    public IntPtr lanroot;&lt;br /&gt;
    public uint logged_on_users;&lt;br /&gt;
  }&lt;br /&gt;
  [ DllImport( &amp;quot;Netapi32.dll&amp;quot; ) ]&lt;br /&gt;
  static extern unsafe int NetWkstaGetInfo( IntPtr servername, int level, byte** bufptr );&lt;br /&gt;
  [ DllImport( &amp;quot;Netapi32.dll&amp;quot; ) ]&lt;br /&gt;
  static extern unsafe int NetApiBufferFree( byte* bufptr );&lt;br /&gt;
  [STAThread]&lt;br /&gt;
  static unsafe void Main(string[] args)&lt;br /&gt;
  {&lt;br /&gt;
    byte* bp = null;&lt;br /&gt;
    int rc = NetWkstaGetInfo( IntPtr.Zero, 102, &amp;amp;bp );&lt;br /&gt;
    WkstaInfo102* wip = (WkstaInfo102*)bp;&lt;br /&gt;
    Console.WriteLine( &amp;quot;System {0} has {1} users logged on&amp;quot;, Marshal.PtrToStringAuto( wip-&amp;gt;computername ), wip-&amp;gt;logged_on_users );&lt;br /&gt;
    rc = NetApiBufferFree( bp );&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;System nfex has 3 users logged on&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Lock work station==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
using System.Collections;&lt;br /&gt;
using System.Data;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
public class MainClass {&lt;br /&gt;
  [ DllImport( &amp;quot;user32.dll&amp;quot; ) ]&lt;br /&gt;
  private static extern bool LockWorkStation();&lt;br /&gt;
&lt;br /&gt;
  [STAThread]&lt;br /&gt;
  static void Main() &lt;br /&gt;
  {&lt;br /&gt;
    LockWorkStation();&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Reading INI file: Get Private Profile String==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Text;&lt;br /&gt;
class MainClass&lt;br /&gt;
{&lt;br /&gt;
    // Declare the unmanaged functions.&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;, EntryPoint = &amp;quot;GetPrivateProfileString&amp;quot;)]&lt;br /&gt;
    private static extern int GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName);&lt;br /&gt;
&lt;br /&gt;
    static void Main(string[] args)&lt;br /&gt;
    {&lt;br /&gt;
        string section = &amp;quot;SampleSection&amp;quot;&lt;br /&gt;
        string key = &amp;quot;Key1&amp;quot;;&lt;br /&gt;
        string filename = &amp;quot;\\initest.ini&amp;quot;;&lt;br /&gt;
        int chars = 256;&lt;br /&gt;
        StringBuilder buffer = new StringBuilder(chars);&lt;br /&gt;
        string sDefault = &amp;quot;&amp;quot;;&lt;br /&gt;
        if (GetPrivateProfileString(section, key, sDefault, buffer, chars, filename) != 0)&lt;br /&gt;
        {&lt;br /&gt;
            Console.WriteLine(&amp;quot;Value of Key1 in [SampleSection] is: &amp;quot; + buffer.ToString());&lt;br /&gt;
        }&lt;br /&gt;
        else&lt;br /&gt;
        {&lt;br /&gt;
            Console.WriteLine(&amp;quot;Value of Key1 in [SampleSection] is: &amp;quot; + null);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==The windows version information==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;// Code from &lt;br /&gt;
// A Programmer&amp;quot;s Introduction to C# 2.0, Third Edition&lt;br /&gt;
// copyright 2000 Eric Gunnerson&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
unsafe struct OSVERSIONINFO&lt;br /&gt;
{&lt;br /&gt;
     public uint dwOSVersionInfoSize;  &lt;br /&gt;
     public uint dwMajorVersion;  &lt;br /&gt;
     public uint dwMinorVersion;  &lt;br /&gt;
     public uint dwBuildNumber;&lt;br /&gt;
     public uint dwPlatformId; &lt;br /&gt;
     public fixed char szCSDVersion[128];&lt;br /&gt;
}&lt;br /&gt;
class Program{&lt;br /&gt;
     [DllImport(&amp;quot;Kernel32.dll&amp;quot;, CharSet = CharSet.Unicode)]&lt;br /&gt;
     static extern bool GetVersionEx(ref OSVERSIONINFO lpVersionInfo);&lt;br /&gt;
     unsafe static void Main(string[] args)&lt;br /&gt;
     {&lt;br /&gt;
          OSVERSIONINFO versionInfo = new OSVERSIONINFO();&lt;br /&gt;
          versionInfo.dwOSVersionInfoSize = (uint)sizeof(OSVERSIONINFO);&lt;br /&gt;
          bool res = GetVersionEx(ref versionInfo);&lt;br /&gt;
          Console.WriteLine(Marshal.PtrToStringUni(new IntPtr(versionInfo.szCSDVersion)));&lt;br /&gt;
     }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;S&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Use native windows function to read file==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;// Code from &lt;br /&gt;
// A Programmer&amp;quot;s Introduction to C# 2.0, Third Edition&lt;br /&gt;
// copyright 2000 Eric Gunnerson&lt;br /&gt;
&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Text;&lt;br /&gt;
class FileRead&lt;br /&gt;
{&lt;br /&gt;
    const uint GENERIC_READ = 0x80000000;&lt;br /&gt;
    const uint OPEN_EXISTING = 3;&lt;br /&gt;
    IntPtr handle;&lt;br /&gt;
    &lt;br /&gt;
    public FileRead(string filename)&lt;br /&gt;
    {&lt;br /&gt;
        // opens the existing file&lt;br /&gt;
        handle = CreateFile(    filename,&lt;br /&gt;
        GENERIC_READ,&lt;br /&gt;
        0, &lt;br /&gt;
        0,&lt;br /&gt;
        OPEN_EXISTING,&lt;br /&gt;
        0,&lt;br /&gt;
        0);&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    [DllImport(&amp;quot;kernel32&amp;quot;, SetLastError=true)]&lt;br /&gt;
    static extern IntPtr CreateFile(&lt;br /&gt;
    string filename,&lt;br /&gt;
    uint desiredAccess,&lt;br /&gt;
    uint shareMode,&lt;br /&gt;
    uint attributes,        // really SecurityAttributes pointer&lt;br /&gt;
    uint creationDisposition,&lt;br /&gt;
    uint flagsAndAttributes,&lt;br /&gt;
    uint templateFile);&lt;br /&gt;
    &lt;br /&gt;
    [DllImport(&amp;quot;kernel32&amp;quot;, SetLastError=true)]&lt;br /&gt;
    static extern unsafe bool ReadFile(&lt;br /&gt;
    IntPtr hFile,&lt;br /&gt;
    void* lpBuffer, &lt;br /&gt;
    int nBytesToRead,&lt;br /&gt;
    int* nBytesRead,&lt;br /&gt;
    int overlapped);&lt;br /&gt;
    &lt;br /&gt;
    public unsafe int Read(byte[] buffer, int count)&lt;br /&gt;
    {&lt;br /&gt;
        int n = 0;&lt;br /&gt;
        fixed (byte* p = buffer) &lt;br /&gt;
        {&lt;br /&gt;
            ReadFile(handle, p, count, &amp;amp;n, 0);&lt;br /&gt;
        }&lt;br /&gt;
        return n;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
class Test&lt;br /&gt;
{&lt;br /&gt;
    public static void Main(string[] args)&lt;br /&gt;
    {&lt;br /&gt;
        FileRead fr = new FileRead(&amp;quot;test.cs&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
        byte[] buffer = new byte[128];&lt;br /&gt;
        ASCIIEncoding e = new ASCIIEncoding();&lt;br /&gt;
        &lt;br /&gt;
        // loop through, read until done&lt;br /&gt;
        Console.WriteLine(&amp;quot;Contents&amp;quot;);&lt;br /&gt;
        while (fr.Read(buffer, 128) != 0)&lt;br /&gt;
        {&lt;br /&gt;
            Console.Write(&amp;quot;{0}&amp;quot;, e.GetString(buffer));&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;Contents&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Writing INI file: Write Private Profile String==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Text;&lt;br /&gt;
class MainClass&lt;br /&gt;
{&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;, EntryPoint = &amp;quot;WritePrivateProfileString&amp;quot;)]&lt;br /&gt;
    private static extern bool WritePrivateProfileString(string lpAppName, string lpKeyName, string lpString, string lpFileName);&lt;br /&gt;
    static void Main(string[] args)&lt;br /&gt;
    {&lt;br /&gt;
        // Write a new value.&lt;br /&gt;
        WritePrivateProfileString(&amp;quot;MySection&amp;quot;, &amp;quot;MyKey&amp;quot;, &amp;quot;New Value&amp;quot;, &amp;quot;\\initest.ini&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>