該例子演示了利用微軟
微軟力推
我們將要構造的示例方案的目的就是通過專用的PC卡自動采集來自於不同天氣情報采集點的天氣信息
我們的目標是讓用戶通過互聯網(Web)獲得指定采集點的天氣預報信息
PC卡的驅動程序接口
假設我們PC采集卡已安裝到專用PC上
我們並不打算深入研究如何開發PC卡的硬件驅動程序
我們准備有下面定義的結構來封裝采集到的數據
typedef struct {
unsigned long stationID;
unsigned long state; // for management purposes
unsigned long timeStamp;
double temperature; // celcius
double humidity; // percent
double airPressure; // millibar
} WEATHER_DATA
從PC卡讀取數據我們可以利用Win
用戶要求得到指定的采集點的天氣信息的過程如下
以下的頭定義文件為驅動程序和用戶程序共用
// weather_common
// Common definitions used by both user level module and
// kernel driver
//
#define WEATHER_TYPE
// The IOCTL function codes from
// are for customer use
#define IOCTL_GET_WEATHER_DATA
CTL_CODE( WEATHER_TYPE
// Define common used weather data structure
typedef struct {
unsigned long stationID;
unsigned long state;
unsigned long timeStamp;
double temperature;
double humidity;
double airPressure;
} WEATHER_DATA
接下來的代碼是用戶端的實現
// UserLevelModule
//
// Implementation of user level module
DWORD dwBytesReturned;
// Create structure and fill initial data
WEATHER_DATA dataStruct;
// specify station to request
dataStruct
// Open driver
HANDLE hDriver;
hDriver = CreateFile(
// Issue control call to driver passing prepared structure
DeviceIOControl(hDriver
(void*)&dataStruct
(void*)&dataStruct
&dwBytesReturned
// If succeeded the structure now contains
// the requested data in data
// Close driver
CloseHandle(hDriver);
接下來是驅動程序的部分代碼
// WeatherDrv
//
// Implementation of kernel driver
NTSTATUS
WeatherDispatch(
IN PDEVICE_OBJECT DeviceObject
IN PIRP Irp
)
{
pIrpStack = IoGetCurrentIrpStackLocation(pIrp);
// Dispatch based on major fcn code
switch (pIrpStack
{
case IRP_MJ_DEVICE_CONTROL:
// Dispatch on IOCTL
switch (pIrpStack
{
case IOCTL_GET_WEATHER_DATA:
pWeatherData = (PWEATHER_DATA)
pIrp
if(pWeatherData != NULL)
{
// Fill time stamp in data structure
KeQuerySystemTime(&sysTime);
RtlTimeToTimeFields(&sysTime
pWeatherData
+
+
// Emulate controller card work with
// sample data
pWeatherData
pWeatherData
pWeatherData
}
pIrp
sizeof(WEATHER_DATA);
break;
default:
break;
}
Status = STATUS_SUCCESS;
break;
// Complete request
}
用戶端代碼
驅動程序以及驅動程序和用戶程序的通訊協議定義好了以後
我們可以開發一個應用程序或者動態連接庫
// weatherDrvCtrl
//
// Declaration of GetWeatherData()
DWORD WINAPI GetWeatherData(WEATHER_DATA* pData);
// weatherDrvCtrl
//
// Definition of GetWeatherData()
DWORD WINAPI GetWeatherData(
WEATHER_DATA* pData)
{
HANDLE hDriver;
DWORD dwErr = ERROR_SUCCESS;
DWORD dwBytesReturned;
// Open driver
HANDLE hDriver;
hDriver = CreateFile(DRIVER_DEVICE_NAME
if(hDriver != INVALID_HANDLE_VALUE)
{
// Issue control call to driver
// passing prepared structure
dwErr = DeviceIOControl(hDriver
(void*)&dataStruct
(void*)&dataStruct
&dwBytesReturned
if(!dwErr)
{
// If succeeded the structure now contains
// requested data in data field
}
// Close driver
CloseHandle(hDriver);
}
else
{
// Handle device open error;
}
return dwErr;
}
附件的weatherDrvCtrl項目是用Visual Studio
到現在為止我們已經完成了驅動程序和用戶端調用的所有工作
網絡服務Web Service
下面我們來實現
[ StructLayout( LayoutKind
public struct WeatherData
{
public UInt
public UInt
public UInt
public Double temperature;
public Double humidity;
public Double airPressure;
}
[DllImport(
static extern unsafe uint GetWeatherData
(ref WeatherData data);
[WebMethod(Description=
public unsafe WeatherData
QueryWeatherData(uint stationID)
{
// Create and initialize data structure
WeatherData weatherData = new WeatherData();
weatherData
// Call user level wrapper for device driver
uint dwRet = GetWeatherData(ref weatherData);
// If call failed
return weatherData;
}
如果您按照上述過程正確操作
From:http://tw.wingwit.com/Article/program/net/201311/13454.html