Results 1 to 2 of 2

Thread: [VB6, VBA, twinBASIC] Intro to Vectored Exception Handling: Crash-proof CopyMemory

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    7,653

    [VB6, VBA, twinBASIC] Intro to Vectored Exception Handling: Crash-proof CopyMemory

    Name:  copymemsafe.jpg
Views: 85
Size:  27.1 KB

    It's been a long standing problem that access violations like a bad address for CopyMemory and other exceptions can't be handled by On Error. One solution to that is Vectored Exception Handling (VEH). You can register a procedure to handle true exceptions like access violations, then set it to skip the offending instruction.

    This is a small module to introduce the concept that allows you to call CopyMemory safely, your app will not crash even if you supply an invalid address. If an invalid address is provided, the operation is skipped and it returns False.

    This works by modifying the CONTEXT structure, which contains among other things the contents of all registers (where things like arguments and return values are actually stored at the assembly code/hardware level), including the instruction pointer register that tells the system exactly what instruction is executing- Eip for 32bit, Rip for 64bit. If an access violation is encountered, we skip the instruction by adding the instruction size-- this is where it gets the most complicated, and to be honest I used Claude AI for the functions to calculate the length, and don't totally understand it, since it's dynamic at runtime and not just looking at the disassembly on disk.

    Requirements: This is a standalone module with no dependencies.
    The definitions were copied from Windows Development Library for twinBASIC, so if you use that package, you can remove all the declares/types/enums/consts.

    Usage Just add modSafeCopy.bas to your project and use CopyMemorySafe in place of CopyMemory. Note you'll have to use VarPtr/StrPtr/ObjPtr since neither VB6 nor tB supports As Any in local functions. You can also use CopyMemorySafe as a function, if the copy is successful without being passed a null pointer and without an exception it returns True.

    NOTE: In twinBASIC this currently only works in compiled exes.

    Important: For VBA, the document must be saved in a Trusted Location

    Download on GitHub
    Last edited by fafalone; May 23rd, 2026 at 05:09 PM.

  2. #2

    Thread Starter
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    7,653

    Re: [VB6, VBA, twinBASIC] Intro to Vectored Exception Handling: Crash-proof CopyMemor

    Update

    Version 1.1 added a set of 64bit defs for VBA support after I found it works there. So it's now universally compatible everywhere except running from the tB IDE for now. VB6 IDE and compiled, VBA, and tB compiled are all ok. This removes the WinDevLib dependency, but the defs come from there so are optional in tB if you have that package.

    Version 1.2 turns CopyMemorySafe into a function that returns True if no exception occurred. Also it turns out it *does not* work in the tB IDE, and a conditional has been added to just perform an unsafe copy if running from the tB IDE. Updated usage notes after discovering that for it to work in VBA after being saved and reopened, the file must be saved in a "Trusted Location" (See Macro Security).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width