Exception Handling for Windows core programming

Source: Internet
Author: User

BelowProgramIt is an array with memory reserved, but no physical memory is allocated. Exception Handling is not accessible. when data is written, an exception is triggered and the memory is applied for, which saves memory usage.

// Seh. CPP: defines the entry point for the console application. // # include "stdafx. H "# include <windows. h> # include <stdio. h> # include <assert. h> # include <strsafe. h> typedef struct mytest {char name [10]; int age;} cell, * pcell; const int g_h = 1024; const int g_l = 512; int g_len = 1024*512; typedef cell mystruct [g_h] [g_l]; mystruct * pmytest = NULL; void Init (pbyte * pdata, DWORD dwsize) {* pdata = (pbyte) virtual Alloc (null, dwsize, mem_reserve | mem_top_down, page_readwrite); Assert (null! = Pdata);} Long exceptionfilter (pexception_pointers Pep, DWORD dwsize) {// default to trying another filter (safest thing to do) Long ldisposition = exception_execute_handler ;; // We only fix access violationsif (PEP-> predictionrecord-> predictioncode! = Exception_access_violation) Return (exception_continue_execution); // get address of attempted access and get attempted read or writepvoid pvaddrtouched = (pvoid) pep-> predictionrecord-> predictioninformation [1]; bool battemptedread = (PEP-> predictionrecord-> predictioninformation [0] = 0); void * paddress = pmytest; // is attempted access within this vmarray's reserved region? If (paddress <= pvaddrtouched) & (pvaddrtouched <(pbyte) paddress + dwsize) {tchar SZ [200]; stringcchprintf (SZ, _ countof (sz ), text ("Violation: attempting to % s"), battemptedread? Text ("read"): Text ("write"); printf ("% s \ n", SZ); If (! Battemptedread) {bool bcommittedstorage = false; // assume committing storage failsdo {bcommittedstorage = (null! = Virtualalloc (pvaddrtouched, sizeof (cell), mem_commit, page_readwrite); If (! Bcommittedstorage) {MessageBox (null, text ("Please close some other applications and press OK. "), text (" insufficient memory available "), mb_iconwarning | mb_ OK) ;}} while (! Bcommittedstorage); ldisposition = (bcommittedstorage? Prediction_continue_execution: execution) ;}} return (ldisposition);} static long winapi myhandledexceptionfilter (pexception_pointers pep) {messageboxa (null, "1", "1", mb_ OK ); return exception_execute_handler;} int _ tmain (INT argc, _ tchar * argv []) {// hmodule hmudule = loadlibrary ("kernel32.dll"); // punhandledexceptionfilter = (punhandledexceptionfilter) getprocaddress (hmudule, // ""); setunhandledexceptionfilter (myhandledexceptionfilter); int I = 0; strlen (null); Int J = 10/I; Init (pbyte *) & pmytest, g_len) ;__ try {int A = (* pmytest) [0] [0]. age ;:__ T (exceptionfilter (getexceptioninformation (), g_len) {printf ("111 \ n") ;:__ try {(* pmytest) [0] [0]. age = 10; (* pmytest) [0] [1]. age = 11; (* pmytest) [0] [2]. age = 11 ;:__ T (predictionfilter (getexceptioninformation (), g_len) {printf ("222 \ n") ;}getchar (); Return 0 ;}

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.