Creation and destruction of MFC processes, creation and interaction of threads

Source: Internet
Author: User

Creation of processes

Startupinfo si;//* * Member DWORD DwFlags; Indicates which members of the struct are valid. **startf_useshowwindow| Startf_usepositionprocess_information Pi; ZeroMemory (&si,sizeof(SI)); SI.CB=sizeof(SI); ZeroMemory (&PI,sizeof(PI)); //LPTSTR pszcmdline = TEXT ("C:\\windows\\system32\\notedap.exe");//TCHAR pszcmdline[] = TEXT ("C:\\windows\\system32\\notedap.exe"); //Windows core Programming specifically has this problem CreateProcess will modify the command line string passed to it, LPTSTR is the string pointer cannot be modified .... *//char* szcommandline = "C:\\Program Files (x86) \\KuGou\\KGMusic\\KuGou.exe";TCHAR szcommandline[] = TEXT ("Notepad ReadMe.txt");//ReadMe.txt in the current directory of the parent process:: CreateProcess (NULL,//executable file name (the. exe must be added.) If the path is not added, only the current directory will be searched. So is generally null)(LPWSTR) Szcommandline,//parameters passed to the execution module, equivalent to input szcommandline in the run bar (can be automatically searched for EXE in some directories)Null//Process SecurityNull//Thread SafetyFALSE,//whether an inheritable handle to the current process can be inherited by a new processNull//Create flags such as Creat_new_consoleNull//Environment VariablesNull//current directory&si,//the display information of the parent to the child process&PI);//flag information for this process id\ handle&pi.dwProcessId;//Process ID&pi.dwThreadId;//main thread ID in process&pi.hProcess;//Process Kernel handle&pi.hThread;//main thread kernel handle in process

Terminating a process

    //:: ExitProcess (0); //terminates the current process, exit code 0//BOOL bbet=::terminateprocess (pi.hprocess,-1);//Exit code-1 (Shutdown process failed)HANDLE hprocess=:: OpenProcess (Process_all_access,//Desired access RightsFALSE,//whether the returned handle can be inheritedPI.DWPROCESSID);//Process IDBOOL Bbet =:: TerminateProcess (hprocess,-1);//(shutdown process succeeded)CloseHandle (hprocess);  CloseHandle (pi.hprocess); //Close if not used

Creating Threads

DWORD dwThreadID;    HANDLE Hhandle; Hhandle=:: CreateThread (NULL,//Thread-Safe PropertiesNull//Thread Stack sizeThreadfun,//thread function Start addressNull//arguments passed to the thread function                             0,//whether to start the thread now&dwthreadid);//Get thread ID//generally use the following methodsUINT uId;    HANDLE hhandlecopy; Hhandlecopy= (HANDLE):: _beginthreadex (NULL,//Thread-Safe PropertiesNull//Thread Stack sizeThreadProc,//thread function Start addressNull//arguments passed to the thread function                               0,//whether to start the thread now&UID);//Get thread ID

The thread functions are as follows:

//============================================================================================//definition of a thread function//DWORD WINAPI threadfun ()//parameter LPVoid Ipparam is requiredDWORD WINAPI threadfun (lpvoid ipparam) {startupinfo si;    Process_information Pi; ZeroMemory (&si,sizeof(SI)); SI.CB=sizeof(SI); ZeroMemory (&PI,sizeof(PI)); TCHAR szcommandline[]= TEXT ("C:\\Program Files (x86) \\KuGou\\KGMusic\\KuGou.exe"); :: CreateProcess (NULL, (LPWSTR) szcommandline,null,null,false,null,null, NULL,&si,&pi); return 0;}//=============================================================================================UINT _stdcall ThreadProc (lpvoid ipparam) {:: WaitForSingleObject (G_hevent,infinite);    Startupinfo si;    Process_information Pi; ZeroMemory (&si,sizeof(SI)); SI.CB=sizeof(SI); ZeroMemory (&PI,sizeof(PI)); TCHAR szcommandline[]= TEXT ("Notepad ReadMe.txt"); :: CreateProcess (NULL, (LPWSTR) szcommandline, NULL, NULL, FALSE, NULL, NULL, NULL ,&si, &pi); return 0;}//================================================================================================

Thread Communication Interaction

    //Interaction between Threads (event kernel object)//HANDLE g_hevent;G_hevent =:: CreateEvent (NULL,//Event Object Security PropertiesFALSE,//whether to manually reset the event object to untrusted (otherwise the system resets automatically)FALSE,//Initial state (trusted/untrusted) in execution: untrustedNULL);//Event object Name (available for the third parameter of the OpenEvent () function, similar to a process):: WaitForSingleObject (Hhandle,//Object HandleINFINITE);//wait time (\ milliseconds)Sleep (10000);    SetEvent (g_hevent); //retevent (g_hevent);//no reset (automatic reset) in automatic mode:: WaitForSingleObject (Hhandlecopy,infinite);//wait//:: WaitForMultipleObjects (2,//number of object handles//H,//array of Object handles//TRUE,//whether to wait for all kernel objects to become trusted (otherwise there is one)//INFINITE)//wait time (\ milliseconds)//It is useful to wait for the specified thread to execute (without the thread having finished executing the main process is finished)

Among them SetEvent (g_hevent); The g_hevent in the handle is the global variable g_hevent;

SetEvent (g_hevent) causes the event object g_hevent to become a trusted state,

at this in the ThreadProc function :: WaitForSingleObject (g_hevent,infinite), the function detects that it is a trusted time to start executing the next code.

Terminating a thread is similar to terminating a process, generally does not use the Terminate process and thread functions, and generally uses a communication mechanism to tell the process or thread to close to let it exit itself.

A forced termination makes it too late to perform destructors, reclaim memory, and cause memory leaks.

Creation and destruction of MFC processes, creation and interaction of threads

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.