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
[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
//定義記憶體的資訊結構
[structlayout(layoutkind.sequential)]
public
struct memory_info
//定義系統時間的資訊結構
[structlayout(layoutkind.sequential)]
public
struct systemtime_info
private
void
button1_click(object sender, eventargs e)
private
void
button2_click(object sender, eventargs e)
}}
c 中 的使用
對於習慣使用c進行開發的朋友們,在看到c 中出現的 符號,可能會犯迷糊,因為在c語言中這個符號表示了取位址符,但是在c 中它卻有著不同的用途,掌握c 的 符號,是提高 執行效率和增強 質量的乙個很好的辦法。在 c 學習提高篇 3 隱式指標的文章中我詳細介紹了在隱式指標 的用法,其實這兩個概念是統一的...
C 中 的使用
1.可空型別修飾符 引用型別可以使用空引用表示乙個不存在的值,而值型別通常不能表示為空。例如 string str null 是正確的,int i null 編譯器就會報錯。為了使值型別也可為空,就可以使用可空型別,即用可空型別修飾符 來表示,表現形式為 t?例如 int?表示可空的整形,datet...
C 中datetime的使用
c 中datetime的使用 大家在做報表或查詢的時候都會有給使用者預設一些可選的日期範圍 如本年度銷售額 本季度利潤 本月新增客戶 c 裡內建的 datetime 基本上都可以實現這些功能,巧用 datetime 會使你處理這些事來變輕鬆多了 今天 datetime.now.date.toshor...