Написать программу для получения сведений о системе, используя функции Win32 API - C#
Формулировка задачи:
В институте такая вот задача далась, помогите её решить пожалуйста:
Написать программу для получения сведений о системе, используя функции
Win32 API типа: GetSystemDirectory (), GetWindowsDirectory (), GetComputerName (),
GetUserName (), GetVersionEx (), GetKeyboardType (). Для вывода результатов
использовать функции wsprintf (), WriteConsole ().
Решение задачи: «Написать программу для получения сведений о системе, используя функции Win32 API»
textual
Листинг программы
using System;
using System.Runtime.InteropServices;
using System.Text;
namespace n
{
struct OSVERSIONINFO
{
public uint dwOSVersionInfoSize;
public uint dwMajorVersion;
public uint dwMinorVersion;
public uint dwBuildNumber;
public uint dwPlatformId;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string szCSDVersion;
public Int16 wServicePackMajor;
public Int16 wServicePackMinor;
public Int16 wSuiteMask;
public Byte wProductType;
public Byte wReserved;
}
class Program
{
[DllImport("kernel32.dll")]
static extern uint GetSystemDirectory([Out] StringBuilder lpBuffer,
uint uSize);
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern uint GetWindowsDirectory(StringBuilder lpBuffer,
uint uSize);
[DllImport("user32.dll", CallingConvention = CallingConvention.Cdecl)]
static extern int wsprintf(
[Out] StringBuilder buffer,
string format,
string arg);
[DllImport("kernel32.dll")]
static extern bool WriteConsole(IntPtr hConsoleOutput, string lpBuffer,
uint nNumberOfCharsToWrite, out uint lpNumberOfCharsWritten,
IntPtr lpReserved);
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr GetStdHandle(int nStdHandle);
[DllImport("Kernel32", CharSet = CharSet.Auto)]
public static extern bool GetComputerName(StringBuilder buffer, ref uint size);
[DllImport("advapi32.dll", SetLastError = true)]
static extern bool GetUserName(StringBuilder sb, ref uint length);
[DllImport("user32.dll")]
static extern int GetKeyboardType(int nTypeFlag);
static uint Write(string s)
{
const int STD_OUTPUT_HANDLE = -11;
IntPtr iStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
uint cchWritten;
if (!WriteConsole(iStdOut,
s,
(uint)s.Length,
out cchWritten,
(IntPtr)0))
return 0;
return cchWritten;
}
[DllImport("kernel32")]
static extern bool GetVersionEx(ref OSVERSIONINFO osvi);
static void Main(string[] args)
{
StringBuilder buffer=new StringBuilder(128);
StringBuilder tmp=new StringBuilder(128);
GetSystemDirectory(tmp, 128);
wsprintf(buffer, "System Directory %s\n",tmp.ToString());
Write(buffer.ToString());
GetWindowsDirectory(tmp, 128);
wsprintf(buffer, "Windows Directory %s\n", tmp.ToString());
Write(buffer.ToString());
uint l = 128;
GetComputerName(tmp, ref l);
wsprintf(buffer, "Computer name %s\n", tmp.ToString());
Write(buffer.ToString());
GetUserName(tmp, ref l);
wsprintf(buffer, "User name %s\n", tmp.ToString());
Write(buffer.ToString());
OSVERSIONINFO o=new OSVERSIONINFO();
GetVersionEx(ref o);
wsprintf(buffer, "Platform ID %s\n", o.dwPlatformId.ToString());
Write(buffer.ToString());
int size= GetKeyboardType(2);
wsprintf(buffer, "The number of function keys on the keyboard:%s\n", size.ToString());
Write(buffer.ToString());
Console.Read();
}
}
}