커널 모드 string safe function 사용하기 시스템 프로그래밍

 

Using Safe String Functions

The following table summarizes the safe string functions that are available to kernel-mode drivers, and it indicates the C/C++ language runtime library functions that they replace. If a function's name contains Cb, the function treats strings as byte-counted. If a function's name contains Cch, the function treats strings as character-counted.

FunctionsPurposeReplaces
RtlStringCbCat
RtlStringCbCatEx
RtlStringCchCat
RtlStringCchCatEx
Concatenate two strings.strcat
wcscat
RtlStringCbCatN
RtlStringCbCatNEx
RtlStringCchCatN
RtlStringCchCatNEx
Concatenate two byte-counted strings, while limiting the size of the appended string.strncat
wcsncat
RtlStringCbCopy
RtlStringCbCopyEx
RtlStringCchCopy
RtlStringCchCopyEx
Copy a string into a buffer.strcpy
wcscpy
RtlStringCbCopyN
RtlStringCbCopyNEx
RtlStringCchCopyN
RtlStringCchCopyNEx
Copy a byte-counted string into a buffer, while limiting the size of the copied string.strncpy
wcsncpy
RtlStringCbLength
RtlStringCchLength
Determine the length of a supplied string.strlen
wcslen
RtlStringCbPrintf
RtlStringCbPrintfEx
RtlStringCchPrintf
RtlStringCchPrintfEx
Create a formatted text string that is based on a format string and a set of additional function arguments. sprintf
swprintf
_snprintf
_snwprintf
RtlStringCbVPrintf
RtlStringCbVPrintfEx
RtlStringCchVPrintf
RtlStringCchVPrintfEx
Create a formatted text string that is based on a format string and one additional function argument. vsprintf
vswprintf
_vsnprintf
_vsnwprintf

 

 

Importing Kernel-Mode Safe String Functions

The kernel-mode safe string functions are available:

  • As inline code that is contained in ntstrsafe.h.
  • In a library to which your code can be linked.

Use the inline versions of the kernel-mode safe string functions if your code will execute only on Windows XP and later versions of the operating system. If your code must execute on any Windows system earlier than Windows XP, you must use the library versions of the safe string functions.

To use the inline versions of the kernel-mode safe string functions

Include the header file, as shown.

#include <ntstrsafe.h> 

To use the library versions of the kernel-mode safe string functions

  1. Define NTSTRSAFE_LIB before including the new header file, as shown.
    #define NTSTRSAFE_LIB #include <ntstrsafe.h>
  1. In your project's sources file, add a TARGETLIBS entry for $(DDK_LIB_PATH) tstrsafe.lib.

Whenever you include ntstrsafe.h in a source file, the C/C++ language runtime library functions that ntstrsafe.h replaces become unavailable. If you call one of these unavailable functions, the compiler supplies an error message that tells you to use the safe string functions.

Similarly, if a header file supplies code that references one or more of the replaced functions, and if that header file is included after ntstrsafe.h, those functions will cause the compiler's error message to be displayed. Therefore, the include statement for ntstrsafe.h should follow all other headers' include statements.

If you want to be able to use both the C/C++ language runtime library functions and the safe string functions, include the following line in your code before including the ntstrsafe.h header file.

#define NTSTRSAFE_NO_DEPRECATE 

You can make available only the byte-counted or only the character-counted safe string functions.

To allow only byte-counted functions

Include the following line in your code before including the ntstrsafe.h header file.

#define NTSTRSAFE_NO_CCH_FUNCTIONS

To allow only character-counted functions

Include the following line in your code before including the ntstrsafe.h header file.

#define NTSTRSAFE_NO_CB_FUNCTIONS 

You can define either NTSTRSAFE_NO_CB_FUNCTIONS or NTSTRSAFE_NO_CCH_FUNCTIONS, but not both.

The maximum number of characters that any ANSI or Unicode string can contain is STRSAFE_MAX_CCH. This constant is defined in ntstrsafe.h.

If a string will be placed into a UNICODE_STRING structure, the string length must not exceed 65,535 characters.


덧글

  • 미친감자 2006/09/01 18:41 # 답글

    이 친구들은 IRQL 상관없이 쓸수 있는건가요?
  • somma 2006/09/01 19:25 # 답글

    넵! 아무때나 호출할수 있습니다. :-)
  • kuaaan 2017/01/24 10:58 # 삭제 답글

    somma// 저 함수들 대부분은 PASSIVE에서만 사용 가능하지 않나요?? 예를 들면 RtlStringCbPrintf 같은거... msdn에는 그렇게 나와있지만 실제로 써보니 별일 없더라... 하는 경험치를 말씀하신 건가요?? ^^;;
댓글 입력 영역