IDTRYAGAIN . TRY AGAIN
IDYES . YES
, "" Win32. Visual Studio 2010, File | New | Project... Win32 Project. Location , Name ( 3). , 4, Next, ( 5) Empty project ( ) Finish , .
3.
4.
5.
( 6) , 7. ( Project | Add New Item)
6.
7.
, , ► (Start Debugging), 8 , .
8. Windows-
1. () ( ) . .
2. 4 , . , .
3. . , .
4. 100*100 "" , .
5. .
1. Win32 API?
2. API Win32?
3. Win32 API?
4. Win32 API?
5. Win32?
6. Win32?
2
: Win32 API
|
|
:
1. Win32API,
2. , .
3. Win32 API, .
1. Windows
1.1.
Windows , , . , Windows . . . . ExitProcess TerminateProcess, .
Windows CreateProcess, :
BOOL CreateProcess(
LPCTSTR lpApplicationName,//
LPTSTR lpCommandLine, //
LPSECURITY_ATTRIBUTES lpProcessAttributes,//
LPSECURITY_ATTRIBUTES lpThreadAttributes,//
BOOL bInheritHandle, //
DWORD dwCreationFlags, //
LPVOID lpEnvironment, //
LPCTSTR lpCurrentDirectory, //
LPSTARTUPINFO lpStartUpInfo, //
LPPROCES S_INFORMATION lpProcessInformation //
);
CreateProcess TRUE, . FALSE. , , (parent process) . , , (child process) .
CreateProcess. . lpApplicationName exe-, . . , .
1. ,
#include <conio.h>
int main(int argc, char *argv[])
{
int i;
_cputs("I am created.");
_cputs("\nMy name is: ");
_cputs(argv[0]);
for (i = 1; i < argc; i++)
_cprintf ("\n My %d parameter = %s", i, argv[i]);
_cputs("\nPress any key to finish.\n");
_getch();
return 0;
}
exe-, C ConsoleProcess.exe. exe- .
2. , .
#include <windows.h>
#include <conio.h>
int main()
{
char lpszAppName[] = "C:\\ConsoleProcess.exe";
STARTUPINFO si;
PROCESS_INFORMATION piApp;
ZeroMemory(&si, sizeof(STARTUPINFO));
|
|
si.cb = sizeof(STARTUPINFO);
//
if (!CreateProcess(lpszAppName, NULL, NULL, NULL, FALSE,
CREATE_NEW_CONSOLE, NULL, NULL, &si, &piApp))
{
_cputs("The new process is not created.\n");
_cputs("Check a name of the process.\n");
_cputs("Press any key to finish. \n");
_getch(); return 0;
}
_cputs("The new process is created.\n");
// WaitForSingleObject(piApp.hProcess, INFINITE);
// CloseHandle(piApp.hThread);
CloseHandle(piApp.hProcess);
return 0;
}
. -, ConsoleProcess.exe si STARTUPINFO . ZeroMemory, :
VOID ZeroMemory(
PVOID Destination, //
SIZE_T Length //
);
Windows. -, dwCreationFlags CREATE_NEW_CONSOLE. , . NULL, .
piApp PROCESS_INFORMATION . . FALSE bInheritHandle , . .
, CreateProcess. .
3. ,
#include <windows.h>
#include <conio.h>
int main()
{
char lpszCommandLine[] = "C:\\01-1-ConsoleProcess.exe p1 p2 p3";
STARTUPINFO si;
PROCESS_INFORMATION piCom;
ZeroMemory(&si, sizeof(STARTUPINFO)); si.cb = sizeof(STARTUPINFO);
//
CreateProcess(NULL, lpszCommandLine, NULL, NULL, FALSE,
CREATE_NEW_CONSOLE, NULL, NULL, &si, &piCom);
// CloseHandle(piCom. hThread);
CloseHandle(piCom.hProcess);
_cputs("The new process is created.\n");
_cputs("Press any key to finish.\n");
_getch(); return 0;
}
, . exe-, exe-. lpCommandLine exe- :
;
;
Windows;
Windows;
, PATH .
Notepad.exe, . , , .
4. Notepad
#include <windows.h>
#include <iostream> using namespace std;
int main()
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
// STARTUPINFO ZeroMemory(&si, sizeof(STARTUPINFO)); si.cb = sizeof(STARTUPINFO);
// Notepad if (!CreateProcess(
|
|
NULL, //
"Notepad.exe", // , NULL, //
NULL, //
FALSE, //
0, // NORMAL_PRIORITY_CLASS
NULL, //
NULL, // ,
&si, // -
&pi //
//
)
)
{
cout << "The mew process is not created." << endl << "Check a name of the process." << endl;
return 0;
}
Sleep(1000); //
// CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
return 0;
}
1.2.
ExitProcess, :
VOID ExitProcess(
UINT uExitCode //
);
ExitProcess , . , ExitProcess.
5. ExitProcess
#include <windows.h>
#include <iostream> using namespace std;
volatile UINT count; volatile char c;
void thread()
{
for (;;)
{
count++;
Sleep(100);
}
}
int main()
{
HANDLE hThread;
DWORD IDThread;
hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)thread, NULL, 0, &IDThread); if (hThread == NULL)
return GetLastError();
for (;;)
{
cout << "Input 'y' to display the count or 'e' to exit: "; cin >> (char)c; if (c == 'y')
cout << "count = " << count << endl; if (c == 'e')
ExitProcess(l);
}
}
TerminateProcess, :
BOOL TerminateProcess(
HANDLE hProcess,
UINT uExitCode
);
TerminateProcess , TRUE. FALSE. TerminateProcess , , . .
, TerminateProcess. -, ConsoleProcess.exe C.
6.
#include <windows.h>
#include <iostream> using namespace std; int count;
void main()
{
for (;;)
{
count++;
Sleep(1000);
cout << "count = " << count << endl;
}
}
, , .
7. , , TerminateProcess
#include <windows.h>
#include <conio.h>
int main()
{
char lpszAppName[] = "C:\\ConsoleProcess.exe";
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(STARTUPINFO)); si.cb=sizeof(STARTUPINFO);
//
if (!CreateProcess(lpszAppName, NULL, NULL, NULL, FALSE,
CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi))
{
_cputs("The new process is not created.\n");
|
|
_cputs("Check a name of the process.\n");
_cputs("Press any key to finish. \n");
_getch(); return 0;
}
_cputs("The new process is created.\n");
while (true)
{
char c;
_cputs("Input 't' to terminate the new console process: ");
c = _getch(); if (c == 't')
{
_cputs("t\n");
//
TerminateProcess(pi.hProcess, 1)
; break;
}
}
//
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
return 0;
}
e
Parent Child, .
Parent:
1. , :
struct emp
{
int num; //
char name[10]; //
double grade; //
};
.
2. .
3. , .
4. , , 3.
5. Child, , 4.
6. Child.
7. .
8. .
Child:
1. , .
2. , Parent, , .. .
3. .
Child :
DWORD WaitForSingleObject(
HANDLE hHandle, //
DWORD dwMilliseconds //
);
INFINITE,
WaitForSingleObject(hProcess, INFINITE); //
hProcess Child.
1. ?
2. ?
3. CreateProcess?
4. OpenProcess?
5. ExitProcess?
6. WaitForSingleObject?
3
: Win32 API
:
1. Win32 API, .
2. , .
3. Win32 API, .
. . . , . , , , .
:
− ;
− ;
− ;
− Win32 ;
− ;
: , . (quantum, time slice). , . .
, , . , () , .
: , /, ., . , .
, . , . "" , , .
"" .
|
|
, /. , . (symmetric threads) , . , , , (pool) .
, .
(asymmetric threads) , , , . :
− , ;
− / (-, , . .);
− .
"" (application), , "". , , . , , , . , , " " ; , , , , .
, , , ?
, , . , ; ( ) - .
Win32 API ; , Delphi. .
, : . , , .
, .
− Real time;
− Normal;
− High;
− Below normal;
− Above normal;
− Idle.
, . , . , , , . . .
High , , . , ; , . , high . , .
. , - .
, , , . , , (system agents).
. .
0 31. , , ; .
, , , idle, .
CreateThread, :
HANDLE CreateThread (
LPSECURITY ATTRIBUTES lpThreadAttributes,//
DWORD dwStackSize,//
LPTHREAD_START_ROUTINE lpStartAddress,//
LPVOID lpParameter,//
DWORD dwCreationFlags,//
LPDWORD lpThreadId//
);
CreateThread , . NULL.
CreateThread. lpThreadAttributes . Windows, Windows NT Access Control Win32 API, NULL Windows. , . .
dwStackSize , . , , 1 . , . dwStackSize , , , 1. Windows , 4 .
lpStartAddress . :
DWORD WINAPI ThreadProc (LPVOID lpParameters);
lpParameter , .
dwCreationFlags , . 0, .
CREATE_SUSPENDED, . ResumeThread.
lpThreadId , Windows. , Windows , .
, CreateThread , .