C#中[]的使用

c#中的方括號可用於數組,索引,屬性,更重要的是可以用於外部Dll類庫的引用。

C#實現.NET組件與COM組件的互操作


[DllImport(“kernel32.dll”)]這叫引入kernel32.dll這個動態連接庫。
這個動態連接庫裏面包含了很多WindowsAPI函數,如果你想使用這面的函數,就需要這麼引入。舉個例子:
[DllImport(“kernel32.dll”)]
private static extern void 函數名(參數,[參數]);

extern 作用:標識這個變量或者函數定義在其他文件 ,提示編譯器遇到此變量的時,在其他模塊裏尋找,這裏是在提供的動態庫裏找

函數名就是一個屬於kernel32.dll裏的一個函數。完了你就可以用那個函數了。
.NET組件中使用目前存在的COM組件
對於.NET來講,使用COM組件就要簡單一些。..NET提供了大量的類庫來方便的實現同COM的相互操作,其中很重要的一個名稱空間(nameSpace)就是:System.Runtime.InteropServices。通過這個名稱空間的名字我們也可以從字面上看出,”互操作服務”。System.Runtime.InteropServices這個名稱空間提供了一系列的類來對COM對象進行操作。
例子: 內存,硬盤的利用率

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace Windows.Help
{
public partial class SystemInfo : Form
{
public SystemInfo()
{
InitializeComponent();
}
[DllImport("kernel32")]
public static extern void GetWindowsDirectory(StringBuilder WinDir, int count);
[DllImport("kernel32")]
public static extern void GetSystemDirectory(StringBuilder SysDir, int count);
[DllImport("kernel32")]
public static extern void GetSystemInfo(ref CPU_INFO cpuinfo);
[DllImport("kernel32")]
public static extern void GlobalMemoryStatus(ref MEMORY_INFO meminfo);
[DllImport("kernel32")]
public static extern void GetSystemTime(ref SYSTEMTIME_INFO stinfo);
//定義CPU的信息結構
[StructLayout(LayoutKind.Sequential)]
public struct CPU_INFO
{
public uint dwOemId;
public uint dwPageSize;
public uint lpMinimumApplicationAddress;
public uint lpMaximumApplicationAddress;
public uint dwActiveProcessorMask;
public uint dwNumberOfProcessors;
public uint dwProcessorType;
public uint dwAllocationGranularity;
public uint dwProcessorLevel;
public uint dwProcessorRevision;
}
//定義內存的信息結構
[StructLayout(LayoutKind.Sequential)]
public struct MEMORY_INFO
{
public uint dwLength;
public uint dwMemoryLoad;
public uint dwTotalPhys;
public uint dwAvailPhys;
public uint dwTotalPageFile;
public uint dwAvailPageFile;
public uint dwTotalVirtual;
public uint dwAvailVirtual;
}
//定義系統時間的信息結構
[StructLayout(LayoutKind.Sequential)]
public struct SYSTEMTIME_INFO
{
public ushort wYear;
public ushort wMonth;
public ushort wDayOfWeek;
public ushort wDay;
public ushort wHour;
public ushort wMinute;
public ushort wSecond;
public ushort wMilliseconds;
}
private void button1_Click(object sender, EventArgs e)
{
//調用GetWindowsDirectory和GetSystemDirectory函數分別取得Windows路徑和系統路徑
const int nChars = 128;
StringBuilder Buff = new StringBuilder(nChars);
GetWindowsDirectory(Buff, nChars);
WindowsDirectory.Text = "Windows路徑:" + Buff.ToString();
GetSystemDirectory(Buff, nChars);
SystemDirectory.Text = " 系統路徑:" + Buff.ToString();
//調用GetSystemInfo函數獲取CPU的相關信息
CPU_INFO CpuInfo;
CpuInfo = new CPU_INFO();
GetSystemInfo(ref CpuInfo);
NumberOfProcessors.Text = "本計算機中有" + CpuInfo.dwNumberOfProcessors.ToString() + "個CPU";
ProcessorType.Text = "CPU的類型爲" + CpuInfo.dwProcessorType.ToString();
ProcessorLevel.Text = "CPU等級爲" + CpuInfo.dwProcessorLevel.ToString();
OemId.Text = "CPU的OEM ID爲" + CpuInfo.dwOemId.ToString();
PageSize.Text = "CPU中的頁面大小爲" + CpuInfo.dwPageSize.ToString();
//調用GlobalMemoryStatus函數獲取內存的相關信息
MEMORY_INFO MemInfo;
MemInfo = new MEMORY_INFO();
GlobalMemoryStatus(ref MemInfo);
MemoryLoad.Text = MemInfo.dwMemoryLoad.ToString() + "%的內存正在使用";
TotalPhys.Text = "物理內存共有" + MemInfo.dwTotalPhys.ToString() + "字節";
AvailPhys.Text = "可使用的物理內存有" + MemInfo.dwAvailPhys.ToString() + "字節";
TotalPageFile.Text = "交換文件總大小爲" + MemInfo.dwTotalPageFile.ToString() + "字節";
AvailPageFile.Text = "尚可交換文件大小爲" + MemInfo.dwAvailPageFile.ToString() + "字節";
TotalVirtual.Text = "總虛擬內存有" + MemInfo.dwTotalVirtual.ToString() + "字節";
AvailVirtual.Text = "未用虛擬內存有" + MemInfo.dwAvailVirtual.ToString() + "字節";
//調用GetSystemTime函數獲取系統時間信息
SYSTEMTIME_INFO StInfo;
StInfo = new SYSTEMTIME_INFO();
GetSystemTime(ref StInfo);
Date.Text = StInfo.wYear.ToString() + "年" + StInfo.wMonth.ToString() + "月" + StInfo.wDay.ToString() + "日";
Time.Text = (StInfo.wHour + 8).ToString() + "點" + StInfo.wMinute.ToString() + "分" + StInfo.wSecond.ToString() + "秒";
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章