. , , ,

,,,

,

̳

Բ

ϲ-39

8

.

2011 .


, .

. , . , . .

data.dat. , . 20-30 . : 10 100. ( , ASCII !!!)

1.

1.  . 4.

2.  , . , .

3.  . data.dat. .

4.  , . 1- , .

5.  , , .

1.

1.  . 4.

2.  . data.dat. . .

3.  0.5 . , . , . , , *, .

. ( ), . , ( ), , . .

. , .

1

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

HANDLE FileMappingHandle, FileHandle, MutexHandle;

HINSTANCE exec;

int* Image;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,

PSTR szCmdLine, int iCmdShow)

{

static char szAppName[] = "FileImageViewer" ;

HWND hwnd ;

MSG msg ;

WNDCLASSEX wndclass ;

wndclass.cbSize = sizeof (wndclass) ;

wndclass.style = CS_HREDRAW | CS_VREDRAW ;

wndclass.lpfnWndProc = WndProc ;

wndclass.cbClsExtra = 0 ;

wndclass.cbWndExtra = 0 ;

wndclass.hInstance = hInstance ;

wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;

wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;

wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;

wndclass.lpszMenuName = NULL ;

wndclass.lpszClassName = szAppName ;

wndclass.hIconSm = LoadIcon (NULL, IDI_APPLICATION) ;

RegisterClassEx (&wndclass) ;

hwnd = CreateWindow (szAppName,

"FileImageViewer",

WS_OVERLAPPEDWINDOW,

200,

10,

CW_USEDEFAULT,

CW_USEDEFAULT,

NULL,

NULL,

hInstance,

NULL) ;

ShowWindow (hwnd, iCmdShow) ;

UpdateWindow (hwnd) ;

while (GetMessage (&msg, NULL, 0, 0))

{

TranslateMessage (&msg) ;

DispatchMessage (&msg) ;

}

return msg.wParam ;

}

LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)

{

HDC hdc ;

PAINTSTRUCT ps ;

RECT rect ;

switch (iMsg)

{

int i;

case WM_CREATE:

FileHandle=CreateFile(".\\..\\mass.dat", GENERIC_WRITE|GENERIC_READ,

FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, CREATE_ALWAYS,

FILE_ATTRIBUTE_NORMAL, NULL);

if(FileHandle==NULL)

{

MessageBox(hwnd, " ", "", MB_ICONWARNING);

exit(1);

}

srand((unsigned)time(NULL));

str="";

buf.push_back(str);

int t[26];

for(i=0; i<26; i++)

{

t[i]=rand()%100;

if(t[i]<10)

t[i]+=10;

}

unsigned long z;

exec=ShellExecute(hwnd, NULL, ".\\..\\SysLab8_2\\Debug\\SysLab8_2.exe", NULL,

".\\SysLab8_2\\Debug\\", SW_SHOWNORMAL);

SetTimer(hwnd, NULL, 500, NULL);

WriteFile(FileHandle, t, sizeof(int)*26, &z, NULL);

FileMappingHandle=CreateFileMapping(FileHandle, NULL, PAGE_READWRITE, 0, 0, "mass");

if(FileMappingHandle==NULL)

{

MessageBox(hwnd, " ", "", MB_ICONWARNING);

exit(1);

}

Image=(int*)MapViewOfFile(FileMappingHandle, FILE_MAP_READ, 0, 0, 0);

if(Image==NULL)

{

MessageBox(hwnd, " ", "", MB_ICONWARNING);

exit(1);

}

MutexHandle=CreateMutex(0,FALSE, "massmutex");

if(MutexHandle==NULL)

{

MessageBox(hwnd, " ", "", MB_ICONWARNING);

exit(1);

}

return 0 ;

case WM_PAINT:

hdc = BeginPaint (hwnd, &ps) ;

GetClientRect (hwnd, &rect) ;

for(i=1; (unsigned)i<(unsigned)buf.size(); i++)

TextOut(hdc, 10, i*15, buf[i].c_str(), buf[i].length());

EndPaint (hwnd, &ps) ;

return 0 ;

case WM_TIMER:

__try

{

WaitForSingleObject(MutexHandle, INFINITE);

buf.clear();

str="";

buf.push_back(str);

for(i=1; i<26; i++)

{

for(int j=0; j<*(Image+i); j++)

str+="*";

buf.push_back(str);

str="";

}

}

__finally

{

ReleaseMutex(MutexHandle);

}

InvalidateRect (hwnd, NULL, TRUE) ;

return 0;

case WM_DESTROY :

UnmapViewOfFile(Image);

CloseHandle(FileHandle);

CloseHandle(MutexHandle);

PostQuitMessage (0) ;

return 0 ;

}

return DefWindowProc (hwnd, iMsg, wParam, lParam) ;

}

2

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

HANDLE FileMappingHandle, FileHandle, MutexHandle;

LPVOID Image;

int *a;

std::string str="";

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,

PSTR szCmdLine, int iCmdShow)

{

static char szAppName[] = "FileImageSorter" ;

HWND hwnd ;

MSG msg ;

WNDCLASSEX wndclass ;

wndclass.cbSize = sizeof (wndclass) ;

wndclass.style = CS_HREDRAW | CS_VREDRAW ;

wndclass.lpfnWndProc = WndProc ;

wndclass.cbClsExtra = 0 ;

wndclass.cbWndExtra = 0 ;

wndclass.hInstance = hInstance ;

wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;

wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;

wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;

wndclass.lpszMenuName = NULL ;

wndclass.lpszClassName = szAppName ;

wndclass.hIconSm = LoadIcon (NULL, IDI_APPLICATION) ;

RegisterClassEx (&wndclass) ;

hwnd = CreateWindow (szAppName,

"FileImageSorter",

WS_OVERLAPPEDWINDOW,

10,

10,

170,

80,

NULL,

NULL,

hInstance,

NULL) ;

ShowWindow (hwnd, iCmdShow) ;

UpdateWindow (hwnd) ;

while (GetMessage (&msg, NULL, 0, 0))

{

TranslateMessage (&msg) ;

DispatchMessage (&msg) ;

}

return msg.wParam ;

}

LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)

{

HDC hdc ;

PAINTSTRUCT ps ;

RECT rect ;

switch (iMsg)

{

case WM_CREATE:

return 0 ;

case WM_PAINT:

hdc = BeginPaint (hwnd, &ps) ;

GetClientRect (hwnd, &rect) ;

TextOut(hdc, 10,10,str.c_str(),str.size());

EndPaint (hwnd, &ps) ;

return 0 ;

case WM_KEYDOWN:

if(wParam==VK_SPACE)

{

FileMappingHandle=OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, "mass");

if(FileMappingHandle==NULL)

{

MessageBox(hwnd, " ", "", MB_ICONWARNING);

exit(1);

}

Image=MapViewOfFile(FileMappingHandle, FILE_MAP_WRITE, 0, 0, 0);

if(Image==NULL)

{

MessageBox(hwnd, " ", "", MB_ICONWARNING);

exit(1);

}

MutexHandle=OpenMutex(MUTEX_ALL_ACCESS, FALSE, "massmutex");

if(MutexHandle==NULL)

{

MessageBox(hwnd, " ", "", MB_ICONWARNING);

exit(1);

}

str=" ...";

InvalidateRect (hwnd, NULL, TRUE) ;

__try

{

a= (int *)Image;

long i, j;

int x;

for( i=1; i < 26; i++)

for( j = 26-1; j > i; j-- )

{

Sleep(200);

__try

{

WaitForSingleObject(MutexHandle, INFINITE);

if ( a[j-1] > a[j] )

{

x=a[j-1];

a[j-1]=a[j];

a[j]=x;

if(i==3)

exit(1);

}

}

__finally

{

ReleaseMutex(MutexHandle);

}

}

}

__finally

{

str=" ";

UnmapViewOfFile(Image);

CloseHandle(FileHandle);

CloseHandle(MutexHandle);

}

}

InvalidateRect (hwnd, NULL, TRUE) ;

return 0;

case WM_DESTROY:

UnmapViewOfFile(Image);

CloseHandle(FileHandle);

CloseHandle(MutexHandle);

PostQuitMessage (0) ;

return 0 ;

}

return DefWindowProc (hwnd, iMsg, wParam, lParam) ;

}


, . , : , . .

̳ Բ ϲ-39 8

 

 

 

! , , , .
. , :