Help and Support
 

powered byLive Search

PRB: Access Violation in VB Run-Time Using AddressOf

Article ID:198607
Last Review:March 18, 2005
Revision:2.1
This article was previously published under Q198607

SYMPTOMS

When running your Visual Basic application, you may encounter an error similar to the following:
The instruction at 0x660bd3b1 referenced memory at 0x0000009c. The memory could not be written.
This error may occur immediately following a declared API function call within a Visual Basic callback function.

Back to the top

CAUSE

When the error occurs, the callback function is called by a thread that was not created by Visual Basic.

For instance, certain API calls such as CreateThread() and RegisterServiceCtrlHandler() create an additional thread before invoking the callback function that was previously setup by using the AddressOf operator. Although the same code may have worked in previous versions of Visual Basic, calling API's of this nature are currently unsupported in Visual Basic.

Back to the top

MORE INFORMATION

These API functions use a Free threading model, but Visual Basic only supports Apartment-model threading. Furthermore, additional care must be taken with regards to what code can be executed within the callback function. Any use of the following within the callback function may cause undesirable results:
File I/O.
Error handling.
Fixed size arrays.
Set statements.
COM method calls that return HRESULTs (such as any Visual Basic ActiveX object).
Declare calls.
Global objects such as the Application object.
Most of the Visual Basic run-time files.

Back to the top

REFERENCES

For more information on using threads in Visual Basic, please see the following: Visual Basic Books Online, "Apartment-Model Threading in Visual Basic"

Pattison, Ted, Programming Distributed Applications with COM and Microsoft Visual Basic 6.0. Microsoft Press, 1999, Chapter 7, "Exploring Apartments and Multithreading".

McKinney, Bruce, HardCore Visual Basic. Microsoft Press, 1997 Chapter 11, "Threads and Synchronization"

Appleman, Daniel, A Thread to Visual Basic article located at the following Web URL:
http://www.desaware.com (http://WWW.desaware.com)
For additional information, please see the following articles in the Microsoft Knowledge Base:
189133 (http://support.microsoft.com/kb/189133/EN-US/) Make C DLL More Accessible to VB with a Type Library

143258 (http://support.microsoft.com/kb/143258/EN-US/) How to Create Constants and DLL Declarations in a Type Library

Back to the top


APPLIES TO
Microsoft Visual Basic 6.0 Learning Edition
Microsoft Visual Basic 6.0 Professional Edition
Microsoft Visual Basic 6.0 Enterprise Edition

Back to the top

Keywords: 
kbthread kbapi kbprb KB198607

Back to the top

Article Translations

 

Other Support Options

  • Need More Help?
    Contact a Support professional by Email, Online or Phone.
  • Customer Service
    For non-technical assistance with product purchases, subscriptions, online services, events, training courses, corporate sales, piracy issues, and more.
  • Newsgroups
    Pose a question to other users. Discussion groups and Forums about specific Microsoft products, technologies, and services.