阅读:1801回复:9
为什么我的共享内存不可用?(50分)
我在Dispatch里面创建共享内存,
PVOID CreateAndMapMemory() { PVOID buffer; PMDL mdl; PVOID userVAToReturn; // // Allocate a 4K buffer to share with the application // buffer = ExAllocatePoolWithTag(NonPagedPool, 1024, 'Ksir'); if(!buffer) { return(NULL); } // // Allocate and initalize an MDL that describes the buffer // mdl = IoAllocateMdl(buffer, 1024, FALSE, FALSE, NULL); if(!mdl) { ExFreePool(buffer); return(NULL); } // // Finish building the MDL -- Fill in the "page portion" // MmBuildMdlForNonPagedPool(mdl); #if NT_40 // // Map the buffer into user space // // NOTE: This function bug checks if out of PTEs // userVAToReturn = MmMapLockedPages(mdl, UserMode); #else // // The preferred V5 way to map the buffer into user space // userVAToReturn = MmMapLockedPagesSpecifyCache(mdl, // MDL UserMode, // Mode MmCached, // Caching NULL, // Address FALSE, // Bugcheck? NormalPagePriority); // Priority // // If we get NULL back, the request didn't work. // I'm thinkin' that's better than a bug check anyday. // if(!userVAToReturn) { IoFreeMdl(mdl); ExFreePool(buffer); return(NULL); } #endif // // Store away both the mapped VA and the MDL address, so that // later we can call MmUnmapLockedPages(StoredPointer, StoredMdl) // // StoredPointer = userVAToReturn; // StoredMdl = mdl; //DbgPrint("UserVA = 0x%0x\n", userVAToReturn); return(userVAToReturn); } 然后 *((PVOID*)pIrp->AssociatedIrp.SystemBuffer)=pShareMemory; pIrp->IoStatus.Information = sizeof(PVOID); 返回共享内存指针 但是改指针不能够在NdisReceive里面用,一用到就兰屏,例如用NdisZeroMemory, 是不是在NdisReceive里面也要MmMapLockedPages一次? |
|
|
沙发#
发布于:2004-11-07 20:36
BaseAddress
If AccessMode = UserMode, this parameter specifies the starting user address to map the MDL to, or NULL to allow the system to choose the starting address 为什么要在NdisReceive(指ProtocolReceive?)用返回的userVAToReturn?user space address呀,不bsod倒怪了。不是得到kernle的地址(buffer)了吗,直接用,还是NonPaged,多好 |
|
板凳#
发布于:2004-11-07 21:27
*((PVOID*)pIrp->AssociatedIrp.SystemBuffer)=pShareMemory;
修改一下 *((PVOID*)pIrp->AssociatedIrp.SystemBuffer)相当于PVOID *,也就是指针的指针。修改为 memcpy(pIrp->AssociatedIrp.SystemBuffer , &pShareMemory , sizeof(PVOID) );应该可以 |
|
|
地板#
发布于:2004-11-08 09:05
BaseAddress 感觉有道理应该按着这种方法查一查 |
|
|
地下室#
发布于:2004-11-08 10:06
谢谢楼上几位大哥
找出问题所在了,是线程的问题 |
|
|
5楼#
发布于:2004-11-08 10:09
给分
|
|
|
6楼#
发布于:2004-11-09 23:23
晕了,现在又有问题了,返回的指针为NULL的
不知道哪里有问题 PVOID pShareMemory=NULL; DWORD dwDev=0; SetLastError(0); if(!DeviceIoControl(m_hDevice, IO_GETLOG_CONTROL, &m_hLogSemaphore, sizeof(HANDLE), &pShareMemory, sizeof(PVOID), &dwDev, NULL )) { DWORD dwRet=GetLastError(); return false; } if(pShareMemory==NULL) ::MessageBox (NULL,"共享内存没有分配成功","提示",MB_OK); case IO_GETLOG_CONTROL: { HANDLE hSemaphore; hSemaphore=*((HANDLE*)(pIrp->AssociatedIrp.SystemBuffer)); ObReferenceObjectByHandle(hSemaphore, GENERIC_ALL, NULL, KernelMode, &pSemaphore, NULL); pUserVirtualAddress = MmMapLockedPages(pMdl, UserMode); *((PVOID *)(pIrp->AssociatedIrp.SystemBuffer))=pUserVirtualAddress; pIrp->IoStatus.Information = sizeof(PVOID); break; } 各位大哥,帮帮忙啊 |
|
|
7楼#
发布于:2004-11-10 09:25
上次我不是说了将
*((PVOID *)(pIrp->AssociatedIrp.SystemBuffer))=pUserVirtualAddress; 修改为 *(ULONG *)(pIrp->AssociatedIrp.SystemBuffer))=(ULONG)pUserVirtualAddress ;这样应该可以的。 |
|
|
8楼#
发布于:2004-11-10 11:19
谢谢楼上的大哥,但是还是不行,我这样改了
HANDLE hSemaphore; hSemaphore=*((HANDLE*)(pIrp->AssociatedIrp.SystemBuffer)); ObReferenceObjectByHandle(hSemaphore, GENERIC_ALL, NULL, KernelMode, &pSemaphore, NULL); pUserVirtualAddress = MmMapLockedPages(pMdl, UserMode); *((ULONG *)(pIrp->AssociatedIrp.SystemBuffer))=(ULONG)pUserVirtualAddress; pIrp->IoStatus.Information = sizeof(ULONG); PVOID pShareMemory=NULL; DWORD dwDev=0; App里面 SetLastError(0); if(!DeviceIoControl(m_hDevice, IO_GETLOG_CONTROL, &m_hLogSemaphore, sizeof(HANDLE), &pShareMemory, sizeof(ULONG), &dwDev, NULL )) { DWORD dwRet=GetLastError(); return false; } if(pShareMemory==NULL) ::MessageBox (NULL,"共享内存没有分配成功","提示",MB_OK); |
|
|
9楼#
发布于:2005-01-07 10:11
在我创建的系统线程中可否调用MmUnmapLockedPages()函数来释放我先前分配的一块共享内存。
|
|