通過API實現C 對硬體的控制(二)

2021-05-25 08:09:02 字數 4222 閱讀 9539

原文:

下面是該類的**:

using system;

using system.collections.generic;

using system.text;

using system.runtime.interopservices;

namespace hardware

// wm_devicechange message

[structlayout(layoutkind.sequential)]

public class dev_broadcast_deviceinte***ce

///

/// 裝置資訊資料

///

[structlayout(layoutkind.sequential)]

public class sp_devinfo_data

;///

/// 屬性變更引數

///

[structlayout(layoutkind.sequential)]

public class sp_propchange_params

;///

/// 裝置安裝

///

[structlayout(layoutkind.sequential)]

public class sp_devinstall_params

;[structlayout(layoutkind.sequential)]

public class sp_classinstall_header;}

}在完成了該類後,我們還需要寫乙個對硬體操作與獲取的操作類hardwareclass。

下面為它的完整**:

using system;

using system.collections.generic;

using system.text;

using system.runtime.interopservices;

namespace hardware

externs.sp_devinfo_data deviceinfodata;

deviceinfodata = new externs.sp_devinfo_data();

deviceinfodata.cbsize = 28;

deviceinfodata.devinst = 0;

deviceinfodata.classguid = system.guid.empty;

deviceinfodata.reserved = 0;

uint32 i;

stringbuilder devicename = new stringbuilder("");

devicename.capacity = externs.max_dev_len;

for (i = 0; externs.setupdienumdeviceinfo(hdevinfo, i, deviceinfodata); i++)

hwlist.add(devicename.tostring());

}externs.setupdidestroydeviceinfolist(hdevinfo);

}catch (exception ex)

return hwlist.toarray();}}

#endregion

#region 公共事件

///

/// 清理非託管資源

///

///

public void dispose(intptr callback)

catch

}///

/// 設定指定裝置的狀態

///

/// 裝置名稱

/// 是否啟用

///

public bool setstate(string match, bool benable)

externs.sp_devinfo_data deviceinfodata;

deviceinfodata = new externs.sp_devinfo_data();

deviceinfodata.cbsize = 28;

deviceinfodata.devinst = 0;

deviceinfodata.classguid = system.guid.empty;

deviceinfodata.reserved = 0;

uint32 i;

stringbuilder devicename = new stringbuilder("");

devicename.capacity = externs.max_dev_len;

for (i = 0; externs.setupdienumdeviceinfo(hdevinfo, i, deviceinfodata); i++)

bool bmatch = true;

foreach (string search in match)

}if (bmatch)

}externs.setupdidestroydeviceinfolist(hdevinfo);

}catch (exception ex)

return true;

}///

/// 允許乙個視窗或者服務接收所有硬體的通知

/// 注:目前還沒有找到乙個比較好的方法來處理如果通知服務。

///

///

///

///

public bool allownotifications(intptr callback, bool usewindowhandle)

catch (exception ex)

}#endregion

#region 私有事件

///

/// 開啟或者關閉指定的裝置驅動

/// 注意:該方法目前沒有檢查是否需要重新啟動計算機。^.^

///

///

///

///

///

private bool openclose(intptr hdevinfo, externs.sp_devinfo_data devinfodata, bool benable)

sp_propchange_params1.classinstallheader.cbsize = marshal.sizeof(typeof(externs.sp_classinstall_header));

sp_propchange_params1.classinstallheader.installfunction = externs.dif_propertychange;

sp_propchange_params1.statechange = externs.dics_enable;

sp_propchange_params1.scope = externs.dics_flag_configspecific;

sp_propchange_params1.hwprofile = 0;

}else

szofpcp = marshal.sizeof(sp_propchange_params1);

ptrtopcp = marshal.allochglobal(szofpcp);

marshal.structuretoptr(sp_propchange_params1, ptrtopcp, true);

szdevinfodata = marshal.sizeof(devinfodata);

ptrtodevinfodata = marshal.allochglobal(szdevinfodata);

marshal.structuretoptr(devinfodata, ptrtodevinfodata, true);

bool rslt1 = externs.setupdisetclassinstallparams(hdevinfo, ptrtodevinfodata, ptrtopcp, marshal.sizeof(typeof(externs.sp_propchange_params)));

bool rstl2 = externs.setupdicallclassinstaller(externs.dif_propertychange, hdevinfo, ptrtodevinfodata);

if ((!rslt1) || (!rstl2))

else

}catch (exception ex)

}#endregion}}

通過API實現C 對硬體的控制(一)

最近在做專案的過程中,涉及到對硬體啟用及禁用的功能 在指定時間段內才能啟用硬體 因為c 自身對硬體的操作基本為零,為此,搜尋了一下msdn中與裝置相關的api函式。還果然就有。下面一一枚舉出各函式的原型及相關的說明文件。並將其轉為c 實現。原型 hdevnotify winapi registerd...

通過win32api讓c 控制Windows工作列

c 中宣告如下 using system.runtime.interopservices dllimport user32.dll entrypoint findwindowa public static extern intptr findwindowa string lp1,string lp2...

VB對低層硬體的訪問控制

vb沒有提供直接訪問低層硬體的控制項和方法,一度給對訪問硬體感興趣的程式設計者帶來不便。目前我們可從網上搜尋到支援低層硬體訪問的dll和activex控制項,通過它們可讀寫儲存器單元 埠,甚至控制硬體中斷。下面通過兩個利用dll和activex控制項示例介紹vb對低層硬體的訪問控制。一 利用dll讀...