I was looking for work online for two days. Yesterday, a netizen asked a question, saying SendMessage failed in .NET. I looked at his code was written with VB.NET. So I changed a small test program for a small test program for C #;
[DLLIMPORT ("User32.dll")] Private Static Extern Long SendMessagew (int HWnd, int WMSG, int WPARAM, INT LPAR);
System.Diagnostics.process [] p; p = system.diagnostics.Process.getProcesByname ("notepad"); int i = p [0] .handle.Toint32 (); sendMessagew (i, 16, 0, 0);
(Because SendMessage is divided by the sendMessagea, the sendMessagew is sent, because the SENDMESSAGEW is used inside the NT, and the sendMessageA call is first transferred to sendMessagew, so I use SendMessagew.) This program The function is to find a notepad program and then send him a shutdown message. I have tried, and it really failed. I started me to doubt the problem of .NET, because a netizen said that the successful code called success in VB is fails in VB.NET. So I used ILDASM to disassemble the program. The IL code is as follows. . 10rypoint .custom instance void [mscorlib] system.stathreadAttribute ::. ctor () = (01 00 00) // code size 40 (0x28) .maxstack 4 .locals init ([0] class [system] system.diagnostics .Process [] p, [1] int32 i, [2] Native Int cs $ 00000002 $ 00000000) IL_0000: LDSTR "NOTEPAD" IL_0005: Call class [system] system.diagnostics.process [] [system] system.diagnostics.Process: : GetProcessesByName (string) IL_000a: stloc.0 IL_000b: ldloc.0 IL_000c: ldc.i4.0 IL_000d: ldelem.ref IL_000e: callvirt instance native int [System] System.Diagnostics.Process :: get_Handle () IL_0013: stloc. 2 IL_0014: LDLOCA.S CS $ 00000002 $ 00000000 / / This is the part of the SendMessagew call to P [0] .Handle.Toint32 () Put parameter 1 IL_0016: Call Instance Int32 [mscorlib] System.intptr :: TOINT32 () IL_001B: STLOC.1 IL_001C: LDLOC.1 // Put 16, parameter 2 IL_001D: ldc.i4.s 16 // Add 0, parameter 3 IL_001F: LDC.i4.0 // Put 0, parameter 4 IL_0020: LDC .i4.0 il_0021: Call Int64 Twin.class1 :: SendMessagew (int32, INT32, INT32, INT32) IL_0026: POP IL_0027: RET can't see any problem, I thought it was an operation that a value was packaged. At first glance, all parameters are standard INT32 types, this is a value. I didn't think of this, but it is still a question of .NET. So the program is set, set a breakpoint at SendMessageW (I, 16, 0, 0) to see the exchange code.