//定义ansi字符串
VOID NTAPI
RtlInitAnsiString(                                                                                                                                  

 IN OUT PANSI_STRING DestinationString,
 IN PCSTR SourceString)
{
 SIZE_T DestSize;

 if(SourceString)
 {
  DestSize = strlen(SourceString);
  DestinationString->Length = (USHORT)DestSize;
  DestinationString->MaximumLength = (USHORT)DestSize + sizeof(CHAR);
 }
 else
 {
  DestinationString->Length = 0;
  DestinationString->MaximumLength = 0;
 }

 DestinationString->Buffer = (PCHAR)SourceString;
}

 

//定义unicode字符串
/*
 * @implemented
 *
 * NOTES
 *  If source is NULL the length of source is assumed to be 0.
 */
VOID NTAPI
RtlInitUnicodeString(                                                                                                                                      
 IN OUT PUNICODE_STRING DestinationString,
 IN PCWSTR SourceString)
{
 SIZE_T DestSize;

 if(SourceString)
 {
  DestSize = strlenW(SourceString) * sizeof(WCHAR);
  DestinationString->Length = (USHORT)DestSize;
  DestinationString->MaximumLength = (USHORT)DestSize + sizeof(WCHAR);
 }
 else
 {
  DestinationString->Length = 0;
  DestinationString->MaximumLength = 0;
 }

 DestinationString->Buffer = (PWCHAR)SourceString;
}

 

//将Ansi字符串转换成unicode字符串

NTSTATUS NTAPI
RtlAnsiStringToUnicodeString(                                                                                 
 IN OUT PUNICODE_STRING UniDest,
 IN PANSI_STRING AnsiSource,
 IN BOOLEAN AllocateDestinationString)
{
 ULONG Length;
 PUCHAR WideString;
 USHORT i;

 Length = AnsiSource->Length * sizeof(WCHAR);
 if (Length > MAXUSHORT) return STATUS_INVALID_PARAMETER_2;
 UniDest->Length = (USHORT)Length;

 if (AllocateDestinationString)
 {
  UniDest->MaximumLength = (USHORT)Length + sizeof(WCHAR);
  UniDest->Buffer = (PWSTR) malloc(UniDest->MaximumLength);
  if (!UniDest->Buffer)
   return STATUS_NO_MEMORY;
 }
 else if (UniDest->Length >= UniDest->MaximumLength)
 {
  return STATUS_BUFFER_OVERFLOW;
 }

 WideString = (PUCHAR)UniDest->Buffer;
 for (i = 0; i <= AnsiSource->Length; i++)
 {
  WideString[2 * i + 0] = AnsiSource->Buffer[i];
  WideString[2 * i + 1] = 0;
 }
 return STATUS_SUCCESS;
}

 

//将unicode字符串转成大写
WCHAR NTAPI
RtlUpcaseUnicodeChar(                                                                                    
 IN WCHAR Source)
{
 USHORT Offset;

 if (Source < 'a')
  return Source;

 if (Source <= 'z')
  return (Source - ('a' - 'A'));

 Offset = 0;

 return Source + (SHORT)Offset;
}

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐