Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using Background Threads with Visual Basic 6: AccessViolationExcep

501 views
Skip to first unread message

Jake Lawlor

unread,
Jan 15, 2008, 12:30:01 PM1/15/08
to
I and am implementing Scott Swigart's MSDN solution detailed in
http://msdn2.microsoft.com/en-us/library/aa719109.aspx. It details how to
leverage the .Net 2.0 BackgroundWorker component via interop from a VB6
application.

When I run the compiled example ("make exe") Project1.exe, I get the
following error when it tries to invoke the DoWork method/delegate.
Interestingly, it works fine when I run the same VB6 example from the VB6 IDE
instead of from the exe.

AccessViolationException

Message: "Attempted to read or write protected memory. This is often an
indication that other memory is corrupt."}

Source: "NetFX20Wrapper"

StackTrace: " at
NetFX20Wrapper.BackgroundWorkerWrapper.DoWork.Invoke(Object& argument,
RunWorkerCompletedEventArgsWrapper& e)
at NetFX20Wrapper.BackgroundWorkerWrapper.backgroundWorker_DoWork(Object
sender, DoWorkEventArgs e) in C:\data\ProductionSupport\ETAG\FusionExample\VS
2005 NetFX20Wrapper\NetFX20Wrapper\BackgroundWorkerWrapper.vb:line 43
at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)
at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object
argument)"

Can anyone provide me hints as to what this might be caused from? I am
currently researching CAS to see if there are security attributes that might
resolve this. It appears that .Net is not allowing the VB6 DoWork method to
be called.

Any guidance is greatly appreciated!

Jeroen Mostert

unread,
Jan 15, 2008, 2:20:11 PM1/15/08
to
Jake Lawlor wrote:
> I and am implementing Scott Swigart's MSDN solution detailed in
> http://msdn2.microsoft.com/en-us/library/aa719109.aspx. It details how to
> leverage the .Net 2.0 BackgroundWorker component via interop from a VB6
> application.
>
Well, this is certainly creative. Anything to keep the old VB6 codebase
alive, I guess. :-)

> When I run the compiled example ("make exe") Project1.exe, I get the
> following error when it tries to invoke the DoWork method/delegate.
> Interestingly, it works fine when I run the same VB6 example from the VB6 IDE
> instead of from the exe.
>

That's because you're debugging in the IDE environment, where memory layouts
are completely different. The application contains a bug, it just isn't
detected when running in the IDE.

> AccessViolationException
>
> Message: "Attempted to read or write protected memory. This is often an
> indication that other memory is corrupt."}
>

Did you get a dialog box with the option to attach a debugger? If so, you
should be able to attach VS and poke around for obviously bad values.

> Source: "NetFX20Wrapper"
>
> StackTrace: " at
> NetFX20Wrapper.BackgroundWorkerWrapper.DoWork.Invoke(Object& argument,
> RunWorkerCompletedEventArgsWrapper& e)
> at NetFX20Wrapper.BackgroundWorkerWrapper.backgroundWorker_DoWork(Object
> sender, DoWorkEventArgs e) in C:\data\ProductionSupport\ETAG\FusionExample\VS
> 2005 NetFX20Wrapper\NetFX20Wrapper\BackgroundWorkerWrapper.vb:line 43
> at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)
> at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object
> argument)"
>
> Can anyone provide me hints as to what this might be caused from? I am
> currently researching CAS to see if there are security attributes that might
> resolve this. It appears that .Net is not allowing the VB6 DoWork method to
> be called.
>

No, you're barking up the wrong tree here. CAS causes SecurityExceptions,
not access violations (yes, the terminology can be confusing). The "access"
you're violating is that of an operating system page that you shouldn't be
reading from or writing to, and this is the result of a bad pointer (or
reference, as the case may be here). It has nothing to do with CAS. See the
MSDN on AccessViolationException.

See if objects may be going out of scope and being garbage collected when
you're not expecting them to, or if you've failed to initialize some
critical fields. Alternatively, there may be a mismatch between object
layouts as declared on either side of the fence, or a simple error in
declaring the marshaling interfaces.

--
J.

Jake Lawlor

unread,
Jan 15, 2008, 2:36:00 PM1/15/08
to
Jeroen,

can you kindly just try the referenced MSDN article example. I have
investigated many avenues and have been unsuccessful.

My bet is that you will run into the same error, running the example. If
you have specific ideas to make this example work, lets talk.

Jeroen Mostert

unread,
Jan 15, 2008, 3:07:01 PM1/15/08
to
Jake Lawlor wrote:
> can you kindly just try the referenced MSDN article example. I have
> investigated many avenues and have been unsuccessful.
>
I just meant the documentation for AccessViolationException, which documents
the circumstances in which it occurs. This was just to illustrate that it
has nothing to do with CAS, it's the good old "segmentation violation". For
completeness, it can be found here, but it won't do you much good for
solving the problem at hand since it's a very general error:
http://msdn2.microsoft.com/en-us/library/system.accessviolationexception.aspx

> My bet is that you will run into the same error, running the example.

That's possible and perhaps even likely, but I don't even have the VB
redistributables installed on this machine, let alone a development
environment for VB 6 code! Without that, it's going to be rather hard to
pinpoint the problem.

> If you have specific ideas to make this example work, lets talk.
>

I have no experience with VB6/.NET interop (plenty of experience with
unmanaged interop other than VB, though). Perhaps someone who does can run
the example themselves and tell you specifically what's wrong. All I can
tell you is that the interop is going wrong somehow, but without something
to attach my debugger to I can't be any more specific than that, I'm afraid.
This particular example is marshalling delegates, which is always tricky,
especially when mixing it up with COM and threading.

The author (Scott Swigart) kindly provided his e-mail address with the
sample; if it's still live, he might be willing to take a look.

--
J.

Jeroen Mostert

unread,
Jan 15, 2008, 3:08:34 PM1/15/08
to
Jeroen Mostert wrote:
> Jake Lawlor wrote:
>> can you kindly just try the referenced MSDN article example. I have
>> investigated many avenues and have been unsuccessful.
>>
> I just meant the documentation for AccessViolationException, which

My bad, I misread what you wrote. I did download the sample, but as
mentioned, I can't do anything with it. Well, I can compile the .NET part,
but that's no good.

--
J.

Jake Lawlor

unread,
Jan 15, 2008, 3:53:02 PM1/15/08
to
Thanks for your earnestness Jeroen. This is a tricky one and quite
surprising it doesn't work as the MSDN article details. I have opened a case
with Microsoft and will be sure to post their resolution.

jss

unread,
Jan 17, 2008, 6:15:15 PM1/17/08
to
On Jan 15, 11:30 am, Jake Lawlor
<JakeLaw...@discussions.microsoft.com> wrote:

I'm trying the same thing (to get a little more use from some code)
and could not get the exe of the example to work. In my case, it was
'error 91, Object variable or With Block variable not set'. I changed
from compile to native code' to 'compile to P-code' and it ran. I'll
have to see what effect this setting has on my real application.

Good luck.

0 new messages