RtlInitUnicode의 내부 코드
조경민 오후 10:00 2001-11-05
bro@shinbiro.com
출처 : Google comp.os.ms-windows.programmer.nt.kernel-mode
=============================================================

Search Result 29
From: Slava Monich (monich@ftp.com)
Subject: Re: Simple STRING question..
Newsgroups: comp.os.ms-windows.programmer.nt.kernel-mode
View: Complete Thread (3 articles) | Original Format
Date: 1998/06/27


Jesper Holm wrote:
>
> Hi all,
>
> Being very new to NT driver programming I have a simple question
> regarding how to handle data of the type STRING (and for that matter
> UNICODE_STRING). Consider the following code fragment:
>
> STRING MyStr;
> CHAR    MyStrBuf[80];
>
> MyStr.Buffer = MyStrBuf;
> MyStr.MaximumLength = 80;
> MyStr.Length = 0;
>
> RtlInitString(&MyStr,"Hello world");
>

These RtlInit*String functions are simpler than you think.
RtlInitUnicodeString for instance takes just 18 instructions
on Intel x86 (disassembled by SoftIce):

_RtlInitUnicodeString
0008:80130FD8  PUSH    EDI
0008:80130FD9  MOV     EDI,[ESP+0C]    ; source string
0008:80130FDD  MOV     EDX,[ESP+08]    ; destination string
0008:80130FE1  MOV     DWORD PTR [EDX],00000000
0008:80130FE7  MOV     [EDX+04],EDI
0008:80130FEA  OR      EDI,EDI
0008:80130FEC  JZ      80131003
0008:80130FEE  OR      ECX,-01
0008:80130FF1  XOR     EAX,EAX
0008:80130FF3  REPNZ SCASW
0008:80130FF6  NOT     ECX
0008:80130FF8  SHL     ECX,1
0008:80130FFA  MOV     [EDX+02],CX
0008:80130FFE  DEC     ECX
0008:80130FFF  DEC     ECX
0008:80131000  MOV     [EDX],CX
0008:80131003  POP     EDI
0008:80131004  RET     0008

Before it was compiled, the code above might look like this:

RtlInitUnicodeString(
    PUNICODE_STRING Destination,
    PCWSTR Source )
{
    Destination->Length = 0;
    Destination->MaximumLength = 0;
    Destination->Buffer = Source;
    if ( Source )
    {
        USHORT Length = 0;
        while (*Source++) Length += sizeof(WCHAR);
        Destination->Length = Length;
        Destination->MaximumLength = Length + sizeof(WCHAR);
    }
}

As you can see, you don't have to initialize the destination
string before calling these functions - anything you put
in there gets overwritten anyways.

Hope this helps,
-Slava

'KB > MFC/Win32' 카테고리의 다른 글

DLL, LIB 잘 배포하기  (0) 2004.03.19
def 파일에 대해서  (0) 2004.03.19
[ddk] NT커널 모드 드라이버 in C++  (0) 2004.03.19
NT 커널 드라이버 Visual C++에서 컴파일하기  (0) 2004.03.19
[ddk] NT Kernel Driver  (0) 2004.03.19

+ Recent posts