.


:




:

































 

 

 

 





(global stack unwind) , . 4.1. , , , , , FP-. .

, , . 4.1, , , . :

.

, .

:

, , ExitProcess ExitThread, , , TerminateProcess TerminateThread . tryexcept tryfinally.

, exit main .

SEH C++

C++ catch throw, SEH. , C++ SEH . , , C++, . , __except C++, C++.

, , , C++ SEH-, RaiseException. Microsoft Windows C++ C++.

, Windows , C++.

:

, .

toupper ( 4.2) , . toupper , , . UC_ , "" . , ( ). , , , , . , , , , , . 4.2 , , , SEH. , Web- .

4.2. toupper:

/* 4. toupper. */

/* . UC_. */

#include "EvryThng.h"

 

int _tmain(DWORD argc, LPTSTR argv[]) {

HANDLE hIn = INVALID_HANDLE_VALUE, hOut = INVALID_HANDLE_VALUE;

DWORD FileSize, nXfer, iFile, j;

CHAR OutFileName [256] = "", *pBuffer = NULL;

OVERLAPPED ov = {0, 0, 0, 0, NULL}; /* . */

if (argc <= 1) ReportError(_T(": toupper "), 1, FALSE);

/* , . */

for (iFile = 1; iFile < argc; iFile++) __try { /* . */

/* , pBuffer == NULL, OutFileName . . */

_stprintf(OutFileName, "UC_%s", argv[iFile]);

__try { /* try-finally. */

/* , */

/* " ". */

/* , */

/* . */

/* ( , ). */

hIn = CreateFile(argv[iFile], GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);

if (hIn == INVALID_HANDLE_VALUE) ReportException(argv[iFile], 1);

FileSize = GetFileSize(hIn, NULL);

hOut = CreateFile(OutFileName, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, NULL);

if (hOut == INVALID_HANDLE_VALUE) ReportException(OutFileName, 1);

/* . */

pBuffer = malloc(FileSize);

if (pBuffer == NULL) ReportException(_T(" "), 1);

/* . */

if (!LockFileEx(hIn, LOCKFILE_FAIL_IMMEDIATELY, 0, FileSize, 0, &ov) ReportException(_T(" "), 1);

if (!LockFileEx(hOut, LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY, 0, FileSize, 0, &ov) ReportException(_T(" "), 1);

/* , . */

/* */

/* ; . */

if (!ReadFile(hIn, pBuffer, FileSize, &nXfer, NULL)) ReportException(_T(" "), 1);

for (j = 0; j < FileSize; j++) /* . */

if (isalpha(pBuffer [j])) pBuffer[j] = toupper(pBuffer [j]);

if(WriteFile(hOut, pBuffer, FileSize, &nXfer, NULL)) ReportException(T(" "), 1);

} __finally {

/* , ,*/

/* */

/* . */

if (pBuffer!= NULL) free (pBuffer);

pBuffer = NULL;

if (hIn!= INVALID_HANDLE_VALUE) {

UnlockFileEx(hIn, 0, FileSize, 0, &ov);

CloseHandle(hIn);

hIn = INVALID_HANDLE_VALUE;

}

if (hOut!= INVALID_HANDLE_VALUE) {

UnlockFileEx(hOut, 0, FileSize, 0, &ov);

CloseHandle(hOut);

hOut = INVALID_HANDLE_VALUE;

}

_tcscpy(OutFileName, _T(""));

}

}

/* try. */

/* . */

__except(EXCEPTION_EXECUTE_HANDLER) {

_tprintf(_T(" %s\n"), argv[iFile]);

DeleteFile (OutFileName);

}

_tprintf(_T(" , \n"));

return 0;

}

:

4.3 , , . , . -; , , . , , , .

__finally FP-. , , , , , . , , , , ( 8) ( 3 6). 4.4.

, ; 5.

4.3. Exception:

#include "EvryThng.h"

#include <float.h>

 

DWORD Filter(LPEXCEPTION_POINTERS, LPDWORD);

double x = 1.0, = 0.0;

 

int _tmain(int argc, LPTSTR argv[]) {

DWORD ECatgry, i = 0, ix, iy = 0;

LPDWORD pNull = NULL;

BOOL Done = FALSE;

DWORD FPOld, FPNew;

FPOld = _controlfp(0, 0); /* . */

/* FP-. */

FPNew = FPOld & ~(EM_OVERFLOW | EM_UNDERFLOW | EM_INEXACT | EM_ZERODIVIDE | EM_DENORMAL | EM_INVALID);

_controlfp(FPNew, MCW_EM);

while (!Done) _try { /* try-finally. */

_tprintf(_T(" : "));

_tprintf(_T(" 1: Mem, 2: Int, 3: Flt 4: User 5: __leave "));

_tscanf(_T("%d"), &i);

__try { /* try-except. */

switch (i) {

case 1: /* . */

ix = *pNull;

*pNull = 5;

break;

case 2: /* . */

ix = ix / iy;

break;

case 3: /* FP-. */

x = x / ;

_tprintf(_T("x = %20e\n"), x);

break;

case 4: /* . */

ReportException(_T(" "), 1);

break;

case 5: /* _leave .*/

__leave;

default:

Done = TRUE;

}

} /* __try. */

__except(Filter(GetExceptionInformation(), &ECatgry)) {

switch(ECatgry) {

case 0:

_tprintf(_T(" \n"));

break;

case 1:

_tprintf(_T(" \n"));

continue;

case 2:

_tprintf(_T(" \n"));

break;

case 3:

_tprintf(_("F-\n"));

_clearfp();

break;

case 10:

_tprintf(_T(" \n"));

break;

default:

_tprintf(_T(" \n"));

break;

} /* switch. */

_tprintf(_(" \n"));

}

/* try-except. */

} /* while . */

__finally { /* while. */

_tprintf(_T(" ?: %d\n"),

AbnormalTermination());

}

_controlfp(FPOld, 0xFFFFFFFF); /* FP-.*/

return 0;

}

4.4 , 4.3. . , Web- , , , .





:


: 2015-09-20; !; : 636 |


:

:

.
==> ...

1719 - | 1503 -


© 2015-2024 lektsii.org - -

: 0.03 .