The ultimate weapon in solving. NET development problems

Source: Internet
Author: User
Tags mscorlib
Solve | The problem these two days are looking for a job online. Yesterday a netizen asked a question, said SendMessage in. NET call failed.
I looked at his code, which was written in vb.net. So I switched to C # to write a little test program.
Using System.Runtime.InteropServices;

[DllImport ("user32.dll")]
private static extern long Sendmessagew (int hwnd,int wmsg,int wparam,int lParam);

System.diagnostics.process[] p;
P=system.diagnostics.process.getprocessesbyname ("notepad");
int i=p[0]. Handle.toint32 ();
Sendmessagew (i,16,0,0);

(because SendMessage is divided into two versions, one is SendMessageA one is Sendmessagew, because the NT under the internal use is Sendmessagew, And SendMessageA's call is to convert the parameters before calling Sendmessagew, so here I am using Sendmessagew. )
The function of this program is to find a Notepad program and then send a shutdown message to him.
The experiment was a failure.
At first I suspected the problem of. NET, because a netizen once said that calling successful code in VB failed in vb.net. So I used ILDASM to disassemble the program. The post-compile IL code is as follows.
. entrypoint
. custom instance void [Mscorlib]system.stathreadattribute::.ctor () = (01 00 00 00)
Code size (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
Here is the part of the SENDMESSAGEW call will be p[0]. Handle.toint32 () put the 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
Put 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
Do not see any problem, originally I thought is not a value is boxed and so on operation. A look at all the parameters are standard int32 type, this is a value AH.
I am not aware of this, but still stubborn suspicion is. NET out of the problem.
Then run the program, set a breakpoint at Sendmessagew (i,16,0,0), and look at the sink code. (At run time, there's a disassembly tab next to the code, and you can see the sink code through it)
0000004f Push 0
00000051 Push 0
00000053 mov Ecx,edi
00000055 mov edx,10h
0000005a call DWORD ptr ds:[009c50f8h]
00000060 NOP
The method used to invoke API functions in the WIN32 assembly is to push the value of the parameter into the stack (the backward show), so the parameter indentation order is 4321.
Statements in the Assembly
0000004f Push 0
00000051 Push 0
00000053 mov Ecx,edi
00000055 mov edx,10h
All right. The only thing that could have gone wrong
0000005a call DWORD ptr ds:[009c50f8h]
So the question is clear,. NET calls the API function method is not wrong, is passed the parameter out of error. With VC + + tools to see the tool spy, and sure enough to find the handle and p[0]. Handle.toint32 () does not match. Accidentally found P[0]. Mainwindowhandle and Spy + + results match, suddenly dawned. Cursed themselves stupidly clearly need to send a message to the program's form, you give that process ID transmission, people who talk to you ah!
Switch to P[0]. Mainwindowhandle experiment, Notepad was closed. Although made silly but summed up a few solutions. NET out of some of the problems, if you have some questions that are inexplicable, can not find a way to use ILDASM to decompile, look at the IL code may be helpful, if not, simply to see the exchange code, perhaps the problem will be understood.
After telling him, he asked me another question, how does a form get its own handle, in fact this is very simple, this. Handle.toint32 () is the handle of his own.
At the end of the advertising bar, I soft open work two years to find a monthly salary of 3000 yuan in Beijing's work. Interested parties please contact me fjl716@163.com


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.