2011-10-25 24 views
0

我得到一个非常奇怪的(对我来说)崩溃,而手动操纵UNICODE_STRING崩溃而手动操纵UNICODE_STRING

UNICODE_STRING ustrName; 
UNICODE_STRING ustrPortName; 
UNICODE_STRING linkName; 
UCHAR m_COMPortName[6]; 

RtlInitUnicodeString(&ustrName, L"PortName"); 
status = WdfStringCreate(NULL, WDF_NO_OBJECT_ATTRIBUTES, &strPortName); 
if(NT_SUCCESS(status)) // String created 
{ status = WdfRegistryQueryString (hKey, &ustrName, strPortName); // strPortName is now "COM8" 
    if (NT_SUCCESS (status)) { 
     WdfStringGetUnicodeString(strPortName, &ustrPortName); 
     m_COMPortName[0] = (UCHAR)ustrPortName.Buffer[0]; 
     m_COMPortName[1] = (UCHAR)ustrPortName.Buffer[1]; 
     m_COMPortName[2] = (UCHAR)ustrPortName.Buffer[2]; 
     m_COMPortName[3] = (UCHAR)ustrPortName.Buffer[3]; 
     m_COMPortName[4] = (UCHAR)ustrPortName.Buffer[4]; 
     m_COMPortName[5] = 0; // Force a null-termination 
    } 

} 
WdfRegistryClose(hKey); 

RtlInitUnicodeString(&linkName, L"\\??\\COM123"); // Init with lets say COM123, Breakpoint here... 
linkName.Buffer[7] = (USHORT)m_COMPortName[3]; // First digit in the COM-port number // ** THIS LINE CRASH ** 
linkName.Buffer[8] = (USHORT)m_COMPortName[4]; // Second digit in the COM-port number // (if any else NULL) 
linkName.Buffer[9] = (USHORT)m_COMPortName[5]; // Third digit in the COM-port number // (if any else NULL) 

拆卸:

902de533 6840072e90  push offset mydriver! ?? ::FNODOBFM::'string' (902e0740) ** Breakpoint here (same as above...) ** 
902de538 8d45f8   lea  eax,[ebp-8] 
902de53b 50    push eax 
902de53c ff1528202e90 call dword ptr [mydriver!_imp__RtlInitUnicodeString (902e2028)] 
902de542 660fb60d23392e90 movzx cx,byte ptr [mydriver!m_COMPortName+0x3 (902e3923)] ** Start of the crashing line ** 
902de54a 8b55fc   mov  edx,dword ptr [ebp-4] ** Seems ok ** 
902de54d 66894a0e  mov  word ptr [edx+0Eh],cx ds:0023:902e074e=0031 ** CRASH!!! ** 
902de551 660fb60524392e90 movzx ax,byte ptr [mydriver!m_COMPortName+0x4 (902e3924)] 
902de559 8b4dfc   mov  ecx,dword ptr [ebp-4] 
902de55c 66894110  mov  word ptr [ecx+10h],ax 
902de560 660fb61525392e90 movzx dx,byte ptr [mydriver!m_COMPortName+0x5 (902e3925)] 
902de568 8b45fc   mov  eax,dword ptr [ebp-4] 
902de56b 66895012  mov  word ptr [eax+12h],dx 

两个linkNamem_COMPortName看起来是正确的观察。这是怎么回事?

另一种解决方案是以某种方式将unicode字符串L"\\??\\"与动态读取的unicode字符串L"COMx"连接起来。但我不知道该怎么做。我所知道的MultiByteToWideChar但我不是那么喜欢,因为它需要windows.h,当我包括文件到我的小KMDF驱动程序项目的编译器给我吨的错误使用它的...

所有的代码做为Windows Vista在WINDDK 7600.16385.1(KMDF)

+0

在#include 中发现此RtlUnicodeStringCat(&linkName,&ustrPortName),仍然崩溃... – user872661

回答

0

MSDNRtlUnicodeStringInit

设置UNICODE_STRING结构的 地址,源参数指定的缓冲部件。

linkName缓冲指向一个常数(L"\\??\\COM123"),所以当你试图修改它,它坠毁。