chylml
驱动牛犊
驱动牛犊
  • 注册日期2004-06-29
  • 最后登录2004-12-18
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
阅读:1535回复:3

内存共享--救命阿!

楼主#
更多 发布于:2004-07-01 16:44
我用DriverWorks开发驱动,想在内核和应用之间共享内存,先在内核分配内存块,供DMA缓存数据,应用程序取得该内存的首指针,再利用这个指针读出该块的数据。现在初步的实验是回传指针,生成框架之后,我只改了kernel的READ()和app的doRead()函数,kernel里的READ()函数如下:
NTSTATUS Pointer2Device::Read(KIrp I)
{
t << "Entering Pointer2Device::Read, " << I << EOL;

KMemory Mem(I.Mdl()); //应用程序的MDL;

PULONG pBuffer = (PULONG) Mem.MapToSystemSpace();


ULONG BufferSize = 128;

PVOID pImageMemory = ExAllocatePoolWithTag(NonPagedPool,
                                   BufferSize,
                                   'MpaM');

    if(!pImageMemory)
{
        t << "status = STATUS_UNSUCCESSFUL" << EOL;
    }

KMemory ImageMdl = KMemory(pImageMemory,BufferSize);

PUCHAR pp = (PUCHAR)ImageMdl.VirtualAddress();

t << "ImageMdl.VirtualAddress = " << (ULONG)pp << EOL;

for(int i = 0; i < 16; i++) //写数0、2、4、6...
*pp++  = 2 * i;

if(NT_SUCCESS(ImageMdl.LockPages(IoReadAccess,UserMode))) //锁定
{
t << "lockpages sucessed" << EOL;
}
else
{
t << "lockpages unsucessed" << EOL;
}

ImageMdl.SetPageArray();

PVOID m_UserSpace = (PVOID)ImageMdl.MapToUserSpace();

t << "ImageMdl.PointerOfUserSpace = " << (ULONG)m_UserSpace << EOL;

*pBuffer = (ULONG)(m_UserSpace);

I.Information() = 4;

return I.PnpComplete(this, STATUS_SUCCESS);
}


应用程序doRead()如下:
void doRead(int n)
{
unsigned long *buf;
ULONG nRead;
ULONG i;

buf = (unsigned long *)malloc(n);
if (buf == NULL)
{
printf("Failed to allocate buffer for read");
Exit(1);
}

// Read data from driver
printf("Reading from device - ");
ReadFile(hDevice, buf, n, &nRead, NULL);
printf("%d bytes read from device (%d requested).n", nRead, n);

// Print what was read

printf("the pointer form kernel is :n");

ULONG pToKernelBuffer;

pToKernelBuffer = *buf;
printf("pToKernelBuffer = %xn",pToKernelBuffer);

for(i = 0; i < 16; i++)
{
printf("%u ",*(unsigned char *)(pToKernelBuffer++));
}  

printf("n");

free(buf);
}
doRead()能够读到m_UserSpace = (PVOID)ImageMdl.MapToUserSpace()的指针,也能够把写入缓冲区的0、2、4、6、、、读出来。但DriverMonitor锁定缓冲页时显示lockpages unsucessed,那我没有锁定缓冲区就MapToUserSpace是不是有错误呢.而最严重的是如果我把生成MDL的程序放到其他例程,例如把PVOID pImageMemory = ExAllocatePoolWithTag(NonPagedPool,
                                   BufferSize,
                                   'MpaM');
KMemory ImageMdl = KMemory(pImageMemory,BufferSize)放到onstartdivece ()时就会篮平(该在那里生成呢?onstartdevice()还是driverentry()),我肯定是要在其他地方生存MDL,所以很郁闷,请指点下这关于内核分配内存块、回传指针的编程,谢谢!告诉我怎么样给分,多多都给!

另外如果READ()函数这样生成MDL时显示能够生成MDL,但是执行MapToUserSpace会篮平。
pImageMemory = ExAllocatePoolWithTag(NonPagedPool,
                                   128,
                                   'MpaM');
 
    if(!pImageMemory)
{
        status = STATUS_UNSUCCESSFUL;
return status;
    }

int MdlSize = KMemory::RequiredMdlStorage(pImageMemory, 128);

p = (PMDL) new (NonPagedPool) char[MdlSize];
if ( p == NULL)
{
ExFreePool(pImageMemory);
pImageMemory = NULL;
status = STATUS_UNSUCCESSFUL;
return status;
}

ImageMdl = KMemory(pImageMemory,128,p);

if ((PMDL)ImageMdl == NULL)
{
ExFreePool(pImageMemory);
delete p;
pImageMemory = NULL;
status = STATUS_UNSUCCESSFUL;
return status;
}
t << "ImageMdl = KMemory(pImageMemory,128,p) successn";

PUCHAR pp = (PUCHAR)ImageMdl.VirtualAddress();

t << "ImageMdl.VirtualAddress = " << (ULONG)pp << EOL;

for(int i = 0; i < 16; i++) //写数0、2、4、6...
*pp++  = 2 * i;

if(NT_SUCCESS(ImageMdl.LockPages(IoReadAccess,UserMode))) //锁定
{
t << "lockpages sucessed" << EOL;
}
else
{
t << "lockpages unsucessed" << EOL;
}

ImageMdl.SetPageArray();

PVOID m_UserSpace = (PVOID)ImageMdl.MapToUserSpace();

t << "ImageMdl.PointerOfUserSpace = " << (ULONG)m_UserSpace << EOL;

*pBuffer = (ULONG)(m_UserSpace);

I.Information() = 4;

return I.PnpComplete(this, STATUS_SUCCESS);
}

谢谢!


[编辑 -  7/1/04 by  chylml]
CYoung
驱动中牛
驱动中牛
  • 注册日期2003-06-10
  • 最后登录2005-01-04
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
沙发#
发布于:2004-07-01 16:57
ImageMdl.LockPages(IoReadAccess,UserMode)?把内核的内存锁到应用层?
答得好的话,请兄弟们加点分,那是对我的肯定
chylml
驱动牛犊
驱动牛犊
  • 注册日期2004-06-29
  • 最后登录2004-12-18
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
板凳#
发布于:2004-07-01 18:22
to楼上:是啊,我是想锁定,再回传指针给app啊。你的意思是怎么样啊。
AllenZh
驱动老牛
驱动老牛
  • 注册日期2001-08-19
  • 最后登录2015-11-27
  • 粉丝19
  • 关注10
  • 积分1316分
  • 威望2387点
  • 贡献值7点
  • 好评度321点
  • 原创分0分
  • 专家分0分
地板#
发布于:2004-07-02 09:52
这样的问题我至少回答了3次,在论坛中搜索一下
1,承接Windows下驱动/应用开发 2,本人原创虚拟鼠标/键盘,触摸屏,虚拟显卡,Mirror驱动,XP无盘的SCSI虚拟磁盘驱动等 3,windows下有尝技术服务(包括BUG调试,员工培训等) 欢迎深圳和海外企业联系.msn:mfczmh@sina.com
游客

返回顶部