Windows消息

Figure 1Message Ranges in Windows

范围 描述
0 - (WM_USER-1) 系统使用的保留消息.如:WM_CREATE 和 WM_SCROLL.
WM_USER - 0x7FFF 私有窗口类使用的整型消息.如:DM_GETDEFID (对话框), TB_ENABLEBUTTON (工具栏),和 PBM_SETRANGE (进度条).
WM_APP - 0xBFFF 应用程序使用的消息.
0xC000 - 0xFFFF 应用程序使用的字符串消息(RegisterWindowMessage).
> 0xFFFF Windows 保留在将来使用.

Figure 2StrLit.cpp
////////////////////////////////////////////////////////////////
// MSDN Magazine -- July 2004
// If this code works, it was written by Paul DiLascia.
// If not, I don't know who wrote it.
// Compiles with Visual Studio .NET 2003 on Windows XP. Tab size=3.

// To see the difference between using C++ string literals (TCHAR) and
// .NET (managed) string literals, compile this program and then 
// disassemble the .exe with ILDASM. You can also compile with /FAs to 
// view the assembly code.
//
//     cl /clr /FAs strlit.cpp
//
#include "tchar.h"
#using <mscorlib.dll>

using namespace System;

int _tmain()
{
   // First two TCHAR strings are the same.
   String* c1 = new String(_T("This is a TCHAR string"));
   Console::WriteLine(c1);

   String* c2 = new String(_T("This is a TCHAR string"));
   Console::WriteLine(c1);

   // First two String literals are the same, third is different.
   String* s1 = new String(S"This is a String literal");
   Console::WriteLine(s1);

   String* s2 = new String(S"This is a String literal");
   Console::WriteLine(s2);

   String* s3 = new String(S"This is a different String literal");
   Console::WriteLine(s3);

   // What happens when you pass to managed functions?
   Console::WriteLine(_T("Goodbye,"));
   Console::WriteLine(S"World");

   return 0;
}

Figure 4StrLit Disassembled
//////////////////
// .asm file for strlit.cpp, edited to show highlights.
// Produced with cl /FAs (generate assembly with source).
//
.method public static int32 
  modopt([mscorlib]System.Runtime.CompilerServices.CallConvCdecl) 
  main() cil managed
{
  .vtentry 1 : 1
  // Code size 111 (0x6f)
  .maxstack  1
  .locals init ([0] string c1,
           [1] string s3,
           [2] string s2,
           [3] string s1,
           [4] string c2)
           ...
  // String* c1 = new String(_T("This is a TCHAR string"));
  IL_000b:  ldsflda    valuetype $ArrayType$0x11197cc2 
      modopt([Microsoft.VisualC]Microsoft.VisualC.IsConstModifier) 
      '?A0x1f1e2151.unnamed-global-0'
  IL_0010:  newobj instance void [mscorlib]System.String::.ctor(int8*)
  IL_0015:  stloc.0
  ...
  // String* s1 = new String(S"This is a String literal");
  IL_002e:  ldstr      "This is a String literal"
  IL_0033:  stloc.3
  ...
  // Console::WriteLine(_T("Goodbye,"));
  IL_0052:  ldsflda    valuetype $ArrayType$0x51890b12 
      modopt([Microsoft.VisualC]Microsoft.VisualC.IsConstModifier)
      '?A0x1f1e2151.unnamed-global-2'
  IL_0057:  newobj  instance void [mscorlib]System.String::.ctor(int8*)
  IL_005c:  call       void [mscorlib]System.Console::WriteLine(string)

  // Console::WriteLine(S"World");
  IL_0061:  ldstr      "World"
  IL_0066:  call       void [mscorlib]System.Console::WriteLine(string)
  ...
// end of method 'Global Functions'::main
}

Figure 5?SPAN class="clsCap">StrLit.asm
; Edited to show only highlights桺aul DiLascia
; Listing generated by Microsoft Compiler Version 13.10.3077 

; Generated by Visual C++ for Common Language Runtime
.file "StrLit.cpp"
   .rdata
$SG1883:
   .ascii   "This is a TCHAR string\000"
$SG2266:
   .ascii   "This is a TCHAR string\000"
.global  
   .bss
.local   $StringLiteralTok$70000001$,4
.local   $StringLiteralTok$70000001$,4
   ...
   .text
.global  ?main@@$$HYAHXZ ; main
   ...
; String* c1 = new String(_T("This is a TCHAR string"));
   ldsflda     $SG1883
   newobj      ??0String@System@@$$FQ$AAM@PAC@Z
   stloc.0           ; _c1$
   ...
; String* c2 = new String(_T("This is a TCHAR string"));
   ldsflda     $SG2266
   newobj      ??0String@System@@$$FQ$AAM@PAC@Z
   ...
; String* s1 = new String(S"This is a String literal");
   ldstr    $StringLiteralTok$70000001$
   stloc.3           ; _s1$
   ...
; String* s2 = new String(S"This is a String literal");
   ldstr    $StringLiteralTok$70000001$
   stloc.2           ; _s2$
   ...
   ret      
 .end ?main@@$$HYAHXZ ; main
text  ENDS

Figure 7Managed and Unmanaged Code
////////////////////////////////////////////////////////////////
// MSDN Magazine ?July 2004
// If this code works, it was written by Paul DiLascia.
// If not, I don't know who wrote it.
// Compiles with Visual Studio .NET 2003 on Windows XP. Tab size=3.

// To see what #pragma unmanaged does, compile this file with
//
//   cl /FAs /clr mixed.cpp
//
// and look at the .asm generated. You can see that unmanaged functions
// generate native machine code whereas managed ones generate MSIL.
// By default, when you use /clr, all functions are managed.
//
#include <stdio.h>
#include <vcclr.h>
typedef wchar_t WCHAR;
#using <mscorlib.dll>
using namespace System;

#pragma unmanaged
// This function will be compiled to native machine language
void PrintFive1(const WCHAR* msg)
{
   for (int i=0; i<5; i++) {
      wprintf(msg);
   }
}
#pragma managed

// This function and main will be compiled to MSIL
void PrintFive2(const WCHAR* msg)
{
   for (int i=0; i<5; i++) {
      wprintf(msg);
   }
}

int main()
{
   String* s2 = new String(S"Hello, world\n");
   const WCHAR __pin* pstr = PtrToStringChars(s2);
   PrintFive1(pstr);
   PrintFive2(pstr);
   return 0;
}

posted on 2005-12-29 09:39  Peter.zhou  阅读(336)  评论(0)    收藏  举报

导航