Some Win32 API failures in win 7

Source: Internet
Author: User

In response to longware's feedback this afternoon, we found that some Win32 APIs such as findwindow, findjavaswex, and sendmessage may fail in Windows 7 in some cases.

The discovery process was as follows: longware users reported my experiment in "screen recording function Technology Exploration and sharing ".ProgramThe screen recording process cannot be completed normally in Windows 7. After my workshop test, I found that the findwindowauto used in the program obtained the handle of the camstudio_cl.exe process (0 is returned ).

Surface cause: In the video recording method,

// Do not create a process window
P. startinfo. createnowindow = true;
Under Vista/win 7Findwindow or find1_wex cannot obtain the form handle.So you cannot end the process by sending messages.

Theoretical reasons:

Impact of session 0 isolation on services and driversIn Windows

SourceHttp://www.microsoft.com/whdc/system/sysinternals/Session0Changes.mspx

Updated: December 10,200 9

In Windows XP, Windows Server 2003, and earlier versions of Windows, all services run in session 0 along with applications. this situation poses a security risk. in Windows Vista, Windows Server 2008, and later versions of Windows, the operating system isolates services in session 0 and runs applications in other sessions, so services are protected from attacks that originate in application code.

This paper describes changes to the way in which services are run. it provides guidelines for developers to modify application services and driver services to run in Windows Vista, Windows Server 2008, and later versions of Windows.

This information applies to the following operating systems:
Windows 7
Windows Server 2008 r2
Windows Server 2008
Windows Vista

Theoretically, this means that in the operating system before Vista (such as XP/win Server 2003), all service threads and applications run in session 0, this method has security risks. The operating system after Vista/win7 isolates services and applications, so that the service can be free from the potential from this application.Code.

I understand that in win 7, a program process that does not create a form will be considered as a service and thus run in session 0. Therefore, the application cannot obtain the form handle of the process by calling the findwindow method.

Solution:
After my experiments,

1) "[dllimport (" kernel32.dll ", setlasterror = true, charset = charset. Auto, callingconvention = callingconvention. stdcall)]
Private Static extern intptr getconsolewindow ();"The problem cannot be solved, and the handle cannot be obtained.

2) Replacing findwindow with findwindow cannot solve the problem.

3) You can only create a form when you allow video recording in win 7,P. startinfo. createnowindow = false;To get the process form handle and end the recording process. No better method is found.

Other knowledge points: Send the keyboard key value to an external program

1. Start the process first, process. Start ("mspaint.exe", "C: \ 1.jpg ");
2. Then activate the drawing program.
3. Then, send Ctrl + S to the modification process: sendkeys. Send ("^ s ");

Code

The main program is disabled instead of the drawing program.
[Dllimport ( " User32.dll " , Charset = Charset. Auto)]
Public   Static   Extern Intptr findwindow ( String Classname, String Windowname );
[Dllimport ( " User32.dll " , Charset = Charset. Auto, exactspelling =   True )]
Public   Static   Extern   Bool Setforegroundwindow (intptr hwnd );
[Dllimport ( " User32.dll " , Charset = Charset. Auto, exactspelling =   True )]
Public   Static   Extern   Bool Isiconic (intptr hwnd );
[Dllimport ( " User32.dll " , Charset = Charset. Auto, exactspelling =   True )]
Public   Static   Extern   Bool Openicon (intptr hwnd );
Private   Void Button#click ( Object Sender, eventargs E)
{
Process. Start ( " Mspaint.exe " );
Intptr handle = Findwindow ( Null , " Untitled-Drawing " );
If (Handle ! = Intptr. Zero)
{
If (Isiconic (handle ))
{
Openicon (handle );
}
Else  
{
Setforegroundwindow (handle );
}
}
Sendkeys. Send ( " % {F4} " );
}
--------- Reply --------------
Ensure availability ~ Split... first open to minimize, then active, and then top !!!
C # code
[Dllimport ( " User32.dll " )] Static   Extern Intptr setactivewindow (intptr hwnd); [dllimport ( " User32.dll " )] [ Return : Financialas (unmanagedtype. bool)] Static   Extern   Bool Openicon (intptr hwnd); [dllimport ( " User32.dll " )] [ Return : Financialas (unmanagedtype. bool)] Static   Extern   Bool Setforegroundwindow (intptr hwnd ); Public   Static   Void Getmainwindow ( String Proname) {process [] pros = Process. getprocessesbyname (proname. Trim ()); If (Pros. Length ! =   0 ) {Intptr pgame = Pros [ 0 ]. Main1_whandle; openicon (pgame); setactivewindow (pgame); setforegroundwindow (pgame); thread. Sleep ( 2000 );} Else {Console. writeline ( " Can't get the main window. " );}}

 

 

Refer:

Http://stackoverflow.com/questions/340122/findwindow-fails-from-service-application with similar issues I encountered

Http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_21590318.html

Http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/ea8b0fd5-a660-46f9-9dcb-d525cc22dcbd/

Http://www.khgl.cn/html/82/n-895082.html

 

 

This blog states that I have received material and spiritual support from sinosam during my technical exploration. In the future, all technical exploration achievements in my blog will be jointly owned by "no trace", "guoxin Si Nan", and "blog Park". Original works should be reprinted if necessary, please note this blog statement.

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.