阅读:2447回复:2
ring0与ring3共享内存的问题
我参照网上的做法,驱动分配的地址是传给应用层了,但是应用层不能访问,我已经在vc里看到指针的内容了,但是取不出来,一取就是access voilation。版主大人快来救命啊!大牛们帮帮忙啊!还有一点我得说一下,就是7点多突然成功过一次,但就这一次,成功了以后我关了虚拟机,玩了会斗地主,再开就不行了,为什么啊?
Ring0: PMDL share = NULL; PVOID addr = NULL; PVOID MappedAddr = NULL; BOOLEAN InitMmArea() { if(!addr) { addr = MyMalloc(MmLength); if(!addr) return FALSE; memset(addr, '\0', MmLength); } if(!share) share = IoAllocateMdl(addr, MmLength, FALSE, FALSE, NULL); if(!share) return FALSE; MmBuildMdlForNonPagedPool(share); if(!MappedAddr) DbgPrint("Map mdl failure.\n"); PsCreateSystemThread(&h_thread, GENERIC_ALL, NULL, NULL, NULL, MmThread, NULL); ObReferenceObjectByHandle(h_thread, THREAD_ALL_ACCESS, NULL, KernelMode, &ref_handle, NULL); RtlCopyMemory(addr, "12345", 6); DbgPrint("Init shared mm success\n"); return TRUE; } NTSTATUS GetShareMm(PIRP irp) { if(irp->AssociatedIrp.SystemBuffer) { MappedAddr = MmMapLockedPagesSpecifyCache(share, UserMode, MmNonCached, NULL, FALSE, NormalPagePriority); *((PVOID *)irp->AssociatedIrp.SystemBuffer) = MappedAddr; irp->IoStatus.Information = MmLength; } return STATUS_SUCCESS; } Ring3: void TestShareMm() { HANDLE handle = CreateFile(_T("\\\\.\\Passthru"), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); printf("Begin open device...\n"); if(handle == INVALID_HANDLE_VALUE) { printf("Open failure\n"); return; } else printf("Opened\n"); h_event = CreateEvent(NULL, TRUE, TRUE, NULL); ULONG real = 0 if(0 == DeviceIoControl(handle, IOCTL_QUERY_SHARE_MM, NULL, 0, &mm_share, sizeof(PVOID), &real, NULL)) { printf("Query share mm failed.\n"); CloseHandle(handle); CloseHandle(h_event); printf("%d\n", GetLastError()); return; } char str[7] = ""; if(mm_share) { memcpy(str, mm_share, 7); //(char *)mm_share的值我都看到了,但取不出来 printf("mm context: %s\n", str); } else { printf("Share mm is still NULL.\n"); } } |
|
沙发#
发布于:2010-03-29 16:26
验证过mm_share和MappedAddr是相同的吗
|
|
板凳#
发布于:2010-03-29 21:02
回 1楼(space061) 的帖子
已经行了,是printf的问题,不知道为什么,memcpy能考出来,但是printf就出问题,就是在那句deviceiocontrol之后随便一句printf都access voilence,之前就对,我后来改在mfc里试试,就能显示,真是怪异。 |
|