home *** CD-ROM | disk | FTP | other *** search
/ Mastering Visual Basic 6 / mastvb6.iso / numega / sc501.exe / data1.cab / Examples / APIERR.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1997-11-25  |  5.9 KB  |  241 lines

  1. (*
  2.  * ApiErr.PAS
  3.  * $Header: /BoundsChecker/Examples/BUGBNCHX/DLPHIERR/APIERR.PAS 6     4/21/97 10:05a Bob $
  4.  *
  5.  * Description:
  6.  *    File includes routines for the ApiCheck option on the tree control.
  7.  *
  8.  * Notes:
  9.  *  <implementation notes go here>
  10.  *
  11.  ***********************************************************************
  12.  *
  13.  * Nu-Mega Technologies, Inc.
  14.  * P.O. Box 7780
  15.  * Nashua, NH 03060
  16.  *
  17.  * (c) Copyright 1994, 1995 Nu-Mega Technologies, Inc.
  18.  * ALL RIGHTS RESERVED.
  19.  *
  20.  ***********************************************************************
  21.  *
  22.  **********************************************************************)
  23. (*
  24.    LoadBitmapFail tries to load a non-existant bitmap.  LoadBitmap will
  25.    fail causing a BoundsChecker popup.
  26. *)
  27. procedure LoadBitmapFail;
  28. var
  29.    Penguin: HBITMAP;
  30. begin
  31.    try
  32.       Penguin := LoadBitmap (  GetModuleHandle(NIL), 'HappyHappy');
  33.    except
  34.    end;
  35. end;
  36.  
  37. (*
  38.    LoadMenu will fail because there is no menu selection Hello in 
  39.    bugbnchx.exe
  40. *)
  41. procedure LoadMenu_Fail;
  42. var
  43.    hMenu : THandle;
  44. begin
  45.    try
  46.       hMenu := LoadMenu( GetModuleHandle('bugbnchx.exe'), 'Hello');
  47.    except
  48.    end;
  49. end;
  50.  
  51. (*
  52.    HandleLocked allocates a pointer, locks it, then frees it.  While
  53.    GlobalFree will free a locked pointer, chances are it was locked for
  54.    a reason and should be checked.
  55. *)
  56. procedure HandleLocked;
  57. var
  58.    Flip: HGLOBAL;
  59.    Flap: PChar;
  60.  
  61. begin
  62.    try
  63.       Flip := GlobalAlloc(GHND, $100);
  64.       Flap := GlobalLock ( Flip );
  65.       GlobalFree ( Flip );
  66.    except
  67.    end;
  68. end;
  69.  
  70. (*
  71.    HandleUnlocked unlocks a GlobalPointer whose reference count was already
  72.    0.  While this causes no major problems in itself, it the handle might
  73.    have been unlocked before it should have.
  74. *)
  75. procedure HandleUnlocked;
  76. var
  77.    Flip: HGLOBAL;
  78. begin
  79.    Flip := GlobalAlloc(GHND, $100);
  80.    GlobalUnlock( Flip );
  81.    GlobalFree( Flip );
  82. end;
  83.  
  84. procedure BadDestPtr;
  85. var
  86.    buffer, destination :PChar;
  87. begin
  88.    if ( IDNO = MessageBox ( GetActiveWindow() , 
  89.                   'This function attempts to copy a string into a NIL pointer. '+
  90.                     'This will probably lead to an '+
  91.                     'exception condition.   '+ #13#10#13#10 +
  92.                     'Do you wish to skip this function?',
  93.                   'BugBench Warning', 
  94.                     MB_YESNO OR MB_APPLMODAL) )
  95.    then 
  96.       try
  97.         destination := nil;
  98.         buffer := StrNew ( 'Valid source buffer');
  99.         StrCopy ( destination, buffer );
  100.         StrDispose ( buffer );
  101.       except
  102.       end;
  103. end;
  104.  
  105. (*
  106.    BadFreeMem calls FreeMem twice with the same pointer.  The side effects
  107.    of this action could cause GPFs within a program.
  108. *)
  109. procedure BadFreeMem;
  110. var
  111.    MemoryBlock: Pointer;
  112. begin
  113.    try
  114.       MemoryBlock := AllocMem(50);
  115.       FreeMem(MemoryBlock,50);
  116.       FreeMem(MemoryBlock,50);
  117.    except
  118.    end;
  119. end;
  120.  
  121. (*
  122.    The following call to ReallocMem generates no compiler warning, but
  123.    will cause the program to fault.  BoundsChecker will pop up informing
  124.    you that there was a bad address passed to ReallocMem
  125. *)
  126. procedure BadReallocMem;
  127. var
  128.    MemoryBlock : PChar;
  129. begin
  130.    try
  131.       MemoryBlock := 'Not an allocated block';
  132.       ReallocMem ( Pointer(MemoryBlock), 50 );
  133.    except
  134.    end;
  135. end;
  136.  
  137. procedure BadSourcePointer;
  138. var
  139.    Flip : array [0..12] of Char;
  140. begin
  141.    if ( IDNO = MessageBox ( GetActiveWindow() , 
  142.                   'This function attempts to copy from a NIL pointer. '+
  143.                     'This will probably lead to an '+
  144.                     'exception condition.   '+ #13#10#13#10 +
  145.                     'Do you wish to skip this function?',
  146.                   'BugBench Warning', 
  147.                     MB_YESNO OR MB_APPLMODAL) )
  148.    then 
  149.   
  150.    try
  151.       StrLCopy ( Flip, NIL, 4 );
  152.    except
  153.    end;
  154.  
  155. end;
  156.  
  157. (*
  158.    Several Windows API functions require that the size field be initialized before
  159.    the API is called.  In these cases, the API will fail.  GetVersionEx is an example
  160.    of this type of API.  BoundsChecker knows all of these APIs and validates the size
  161.    field for you.
  162. *)
  163. procedure UninitSizeField;
  164. var
  165.    stOSVI : TOSVersionInfo;
  166. begin
  167.    try
  168.       FillChar (stOSVI, Sizeof(stOSVI), 0 );
  169.       GetVersionEx(stOSVI);
  170.    except
  171.    end;
  172. end;
  173.  
  174. (*
  175.    Windows API expect certain types for parameters ( i.e. valid HANDLEs, HDCs etc ).
  176.    BoundsChecker validates all API calls to verify that the parameters passed meet
  177.    the criteria for the API.  If there is a problem, BoundsChecker will pop up with
  178.    and error.  The following two routines show examples of this behavior.
  179. *)
  180. procedure GetTextColor_Bad_Param;
  181. var
  182.    color : COLORREF;
  183. begin
  184.    color := GetTextColor ( HDC($abcd));
  185. end;
  186.  
  187. procedure DeleteMenu_Bad_Param;
  188. begin
  189.    DeleteMenu ( HMENU($FF00), 0, MF_BYPOSITION);
  190. end;
  191.  
  192. (*
  193.    Argument out of range, GetKeyState
  194. *)
  195. procedure OutOfRange_GetKeyState;
  196. begin
  197.    try
  198.       GetKeyState( $FFFF );
  199.    except
  200.    end;
  201. end;
  202.  
  203. (*
  204.   API functions like AppendMenu allow certain flags to be passed describing
  205.   actions that the API should take.  If a flag is passed that the API knows
  206.   nothing about, BoundsChecker will pop up with an error
  207. *)
  208.  
  209. procedure IllegalFlags_AppendMenu;
  210. var
  211.    TestMenu :HMENU;
  212. begin
  213.    try
  214.       TestMenu := CreateMenu ( );
  215.       AppendMenu ( TestMenu,
  216.                    $80000000,
  217.                    $100,
  218.                    '&New Item' );
  219.       DestroyMenu ( TestMenu );
  220.    except
  221.    end;
  222. end;
  223.  
  224. procedure ConflictingFlags_GetStringType;
  225. var
  226.    szSource : PChar;
  227.    szDest : string[12];
  228. begin
  229.    try
  230.       szSource := 'Happy String';
  231.       GetStringTypeEx ( GetUserDefaultLCID(),
  232.                         CT_CTYPE1 OR CT_CTYPE2,
  233.                         szSource,
  234.                         -1,
  235.                         szDest        );
  236.    except
  237.    end;
  238. end;
  239.  
  240.  
  241.