sword3i
驱动牛犊
驱动牛犊
  • 注册日期2003-07-25
  • 最后登录2013-01-07
  • 粉丝0
  • 关注0
  • 积分15分
  • 威望112点
  • 贡献值0点
  • 好评度9点
  • 原创分0分
  • 专家分0分
阅读:1413回复:2

那位大侠给我一个驱动程序与APP通讯的示例代码!

楼主#
更多 发布于:2003-07-26 17:55
本人对驱动与APP交换数据、通讯等不太清除,那位大侠能帮帮新人,有源码那更好........
随风奔跑自由是方向 追逐雷和闪电的力量 把浩瀚的海洋装进我胸膛 即使再小的帆也能远航 随风飞翔有梦作翅膀 敢爱敢做勇敢闯一闯 哪怕遇见再大的风险再大的浪 也会有默契的目光 努力蹭分! ..................
antspower
驱动中牛
驱动中牛
  • 注册日期2002-10-17
  • 最后登录2010-08-03
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值2点
  • 好评度0点
  • 原创分0分
  • 专家分0分
沙发#
发布于:2003-07-27 19:17
这是最easy的:
1.通讯
在NDIS IMD中用NdisMRegisterDevice,注册一个设备。然后可以通过
DeviceIoControl和应用层通讯
2。当你在APP调用API比如CreateFile ,等等时系统将你的请求转化为IRP转入KENEL中
///////////////////////////////////////////////////////////
ZT
w2k下的PassThru和用户态交互修改说明

一.参考资料为winXP DDK下的PassThru。如果熟悉的话就不要浪费时间看我的修改过程(太简陋了,怕大家笑话),很容易搞定的。
二.内核态的修改过程:
1.在Passthru.c中增加如下的变量定义
#define LINKNAME_STRING L\"\\\\DosDevices\\\\Passthru\"
#define NTDEVICE_STRING L\"\\\\Device\\\\Passthru\"
NDIS_HANDLE NdisDeviceHandle = NULL;
PDEVICE_OBJECT ControlDeviceObject = NULL;
enum _DEVICE_STATE
{
PS_DEVICE_STATE_READY = 0, // ready for create/delete
PS_DEVICE_STATE_CREATING, // create operation in progress
PS_DEVICE_STATE_DELETING // delete operation in progress
} ControlDeviceState = PS_DEVICE_STATE_READY;
///////////////////////////////////////////////////////////////////////////////////////
2.在Passthru.c增加如下函数
NDIS_STATUS
PtRegisterDevice(
VOID
)
/*++

Routine Description:

Register an ioctl interface - a device object to be used for this
purpose is created by NDIS when we call NdisMRegisterDevice.

This routine is called whenever a new miniport instance is
initialized. However, we only create one global device object,
when the first miniport instance is initialized. This routine
handles potential race conditions with PtDeregisterDevice via
the ControlDeviceState and MiniportCount variables.

NOTE: do not call this from DriverEntry; it will prevent the driver
from being unloaded (e.g. on uninstall).

Arguments:

None

Return Value:

NDIS_STATUS_SUCCESS if we successfully register a device object.

--*/
{
NDIS_STATUS Status = NDIS_STATUS_SUCCESS;
UNICODE_STRING DeviceName;
UNICODE_STRING DeviceLinkUnicodeString;
PDRIVER_DISPATCH DispatchTable[IRP_MJ_MAXIMUM_FUNCTION];
UINT i;

DBGPRINT((\"==>PtRegisterDevice\\n\"));

NdisAcquireSpinLock(&GlobalLock);

++MiniportCount;

if (1 == MiniportCount)
{
ASSERT(ControlDeviceState != PS_DEVICE_STATE_CREATING);

//
// Another thread could be running PtDeregisterDevice on
// behalf of another miniport instance. If so, wait for
// it to exit.
//
while (ControlDeviceState != PS_DEVICE_STATE_READY)
{
NdisReleaseSpinLock(&GlobalLock);
NdisMSleep(1);
NdisAcquireSpinLock(&GlobalLock);
}

ControlDeviceState = PS_DEVICE_STATE_CREATING;

NdisReleaseSpinLock(&GlobalLock);

for (i = 0; i < IRP_MJ_MAXIMUM_FUNCTION; i++)
{
DispatchTable = PtDispatch;
}

NdisInitUnicodeString(&DeviceName, NTDEVICE_STRING);
NdisInitUnicodeString(&DeviceLinkUnicodeString, LINKNAME_STRING);

//
// Create a device object and register our dispatch handlers
//

Status = NdisMRegisterDevice(
NdisWrapperHandle,
&DeviceName,
&DeviceLinkUnicodeString,
&DispatchTable[0],
&ControlDeviceObject,
&NdisDeviceHandle
);

NdisAcquireSpinLock(&GlobalLock);

ControlDeviceState = PS_DEVICE_STATE_READY;
}

NdisReleaseSpinLock(&GlobalLock);

//DBGPRINT((\"<==PtRegisterDevice: %x\\n\", Status));
DBGPRINT(\"<==PtRegisterDevice\");

return (Status);
}
NTSTATUS
PtDispatch(
IN PDEVICE_OBJECT DeviceObject,
IN PIRP Irp
)
/*++
Routine Description:

Process IRPs sent to this device.

Arguments:

DeviceObject - pointer to a device object
Irp - pointer to an I/O Request Packet

Return Value:

NTSTATUS - STATUS_SUCCESS always - change this when adding
real code to handle ioctls.

--*/
{
PIO_STACK_LOCATION irpStack;
NTSTATUS status = STATUS_SUCCESS;

DBGPRINT((\"==>Pt Dispatch\\n\"));
irpStack = IoGetCurrentIrpStackLocation(Irp);

switch (irpStack->MajorFunction)
{
case IRP_MJ_CREATE:
break;
case IRP_MJ_CLOSE:
break;
case IRP_MJ_DEVICE_CONTROL:
//
// Add code here to handle ioctl commands sent to passthru.
//
break;
default:
break;
}

Irp->IoStatus.Status = status;
IoCompleteRequest(Irp, IO_NO_INCREMENT);

DBGPRINT((\"<== Pt Dispatch\\n\"));

return status;

}
NDIS_STATUS
PtDeregisterDevice(
VOID
)
/*++

Routine Description:

Deregister the ioctl interface. This is called whenever a miniport
instance is halted. When the last miniport instance is halted, we
request NDIS to delete the device object

Arguments:

NdisDeviceHandle - Handle returned by NdisMRegisterDevice

Return Value:

NDIS_STATUS_SUCCESS if everything worked ok

--*/
{
NDIS_STATUS Status = NDIS_STATUS_SUCCESS;

DBGPRINT((\"==>PassthruDeregisterDevice\\n\"));

NdisAcquireSpinLock(&GlobalLock);

ASSERT(MiniportCount > 0);

--MiniportCount;

if (0 == MiniportCount)
{
//
// All miniport instances have been halted. Deregister
// the control device.
//

ASSERT(ControlDeviceState == PS_DEVICE_STATE_READY);

//
// Block PtRegisterDevice() while we release the control
// device lock and deregister the device.
//
ControlDeviceState = PS_DEVICE_STATE_DELETING;

NdisReleaseSpinLock(&GlobalLock);

if (NdisDeviceHandle != NULL)
{
Status = NdisMDeregisterDevice(NdisDeviceHandle);
NdisDeviceHandle = NULL;
}

NdisAcquireSpinLock(&GlobalLock);
ControlDeviceState = PS_DEVICE_STATE_READY;
}

NdisReleaseSpinLock(&GlobalLock);

//DBGPRINT((\"<== PassthruDeregisterDevice: %x\\n\", Status));
DBGPRINT(\"<== PassthruDeregisterDevice\");
return Status;

}
VOID
PtUnloadProtocol(
VOID
)
{
NDIS_STATUS Status;

if (ProtHandle != NULL)
{
NdisDeregisterProtocol(&Status, ProtHandle);
ProtHandle = NULL;
}

DBGPRINT((\"PtUnloadProtocol: done!\\n\"));
}

///////////////////////////////////////////////////////////////////////////////////////
3.在passthru.h文件中增加上述函数的声明
NTSTATUS
PtDispatch(
IN PDEVICE_OBJECT DeviceObject,
IN PIRP Irp
);

NDIS_STATUS
PtRegisterDevice(
VOID
);

NDIS_STATUS
PtDeregisterDevice(
VOID
);

VOID
PtUnloadProtocol(
VOID
);
/////////////////////////////////////////////////////////////////////////////////////
4.在precomp.h文件的第一行增加如下定义
#define NDIS_WDM 1
/////////////////////////////////////////////////////////////////////////////////////
5.把passthru.c文件中的函数DriverEntry中的PChars.UnloadHandler = NULL;改为
PChars.UnloadHandler = PtUnloadProtocol;
/////////////////////////////////////////////////////////////////////////////////////
6.在miniport.h文件中的函数MPInitialize的代码:Status = NDIS_STATUS_SUCCESS;前增加
//
// Create an ioctl interface
//
(VOID)PtRegisterDevice();
/////////////////////////////////////////////////////////////////////////////////////
7.在miniport.h文件中的函数MPHalt的代码:if (pAdapt->BindingHandle != NULL)前增加
NdisReleaseSpinLock(&GlobalLock);

//
// Delete the ioctl interface that was created when the miniport
// was created.
//
(VOID)PtDeregisterDevice();
/////////////////////////////////////////////////////////////////////////////////////
8.把protocol.c文件中的函数PtUnload的内容改为如下:
DBGPRINT((\"PtUnload: entered\\n\"));
PtUnloadProtocol();
DBGPRINT((\"PtUnload: done!\\n\"));
/////////////////////////////////////////////////////////////////////////////////////
9.把passthru.c文件中的函数DriverEntry的变量定义的后面加上如下代码:
NdisAllocateSpinLock(&GlobalLock);
/////////////////////////////////////////////////////////////////////////////////////
10.修改完毕后编译装载并重起机器就可以了.



三.用户态的代码很简单如下:
#include \"stdafx.h\"
#include <windows.h>
#include <stdio.h>
int main(int argc, char* argv[])
{
HANDLE hdevice = CreateFile(\"\\\\\\\\.\\\\Passthru\", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if (hdevice == INVALID_HANDLE_VALUE)
{
printf(\"Unable to open PassThru device - error %d\\n\", GetLastError());
return 1;
}
CloseHandle(hdevice);
return 0;
}

运行用户态程序,用DebugView就可以看到内核态和用户态的交互情况。

祝你好运!
呵呵,发现在做好事了,拿着这篇文章帖了无数次了。
//骗分似乎还不错:D:D:D:D:D


 :D
放弃瘟草,现吃李草
sword3i
驱动牛犊
驱动牛犊
  • 注册日期2003-07-25
  • 最后登录2013-01-07
  • 粉丝0
  • 关注0
  • 积分15分
  • 威望112点
  • 贡献值0点
  • 好评度9点
  • 原创分0分
  • 专家分0分
板凳#
发布于:2003-07-28 09:18
不哆嗦了,给分
随风奔跑自由是方向 追逐雷和闪电的力量 把浩瀚的海洋装进我胸膛 即使再小的帆也能远航 随风飞翔有梦作翅膀 敢爱敢做勇敢闯一闯 哪怕遇见再大的风险再大的浪 也会有默契的目光 努力蹭分! ..................
游客

返回顶部