2
votes

I have a VB6 DLL that wraps a call to a 3rd party component. When I call my DLL from VB6, everything works fine, but when I call it from vb.net (2.0 framework targeted - VS2010) I get this error:

AccessViolationException occurred

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

This error only occurs on Windows 7 (Windows XP clients work fine).

I've looked this up and all the articles I found talked about the declaration not being correct. I am not declaring any APIs calls though, the 3rd party component is early bound in my VB6 DLL. I can run the DLL, set a breakpoint, and it goes into my VB6 function, but errors calling a function in the 3rd party component.

My VB6 DLL takes 3 string and one 32bit numeric (long in VB6) parameters. The 3rd party's DLL function that I am calling is taking a string (bstrDNSID as string is what Intellisense shows in VB6). This is where it errors.

Does anyone know how this might be resolved?

Update: None of the marshalling has helped, so I tried creating a test sub in my VB6 DLL. I hardcoded all the values within the DLL's test sub. It works fine when called from VB6, but gives the same error as above when running from .NET. Also of interest, when I have the VB6 DLL running from the VB6 IDE, I do not get the error when calling the DLL from .NET.

3
possible duplicate of Calling a VB6 method from a .NET DLLmmmmmm
Is the 3rd party component COM? What are the argument types of the function you're calling? This is a very general error, but I've gotten it when I wasn't marshaling strings correctly.ken
@Mark - That is not my question at all. I know how to call VB6 DLLs from .NET and vice-versa.UnhandledExcepSean
@ken I've updated my question. How do I go about marshaling the strings correctly? I figured by calling the VB6 DLL I wouldn't need to marshal any strings.UnhandledExcepSean
@Mark This is not a duplicate.MarkJ

3 Answers

0
votes

This doesn't really answer your question, but I couldn't fit these examples in the comments.

When marshaling strings to unmanaged DLL calls from .NET, I would receive an AccessViolationException on occasion because I wasn't specifying the right charset. I fixed it by explicitly converting an IntPtr to a string in the charset I needed.

[DllImport("MyDLL.dll", CharSet = CharSet.Ansi)]
static extern void do_something(IntPtr str);

void DoSomethingWrapper(string str) {
    var ptr = Marshal.StringToHGlobalAnsi(str);
    do_something(ptr);
}

You might need Marshal.StringToBSTR. I don't know if you can call the VB6 function with a pointer or if you'll have to create the pointer in the VB6 DLL.

Here's a question you might find useful: How can I get the charset VB6 is using?

0
votes

Workaround:

I have found one solution that works. I simply created an ActiveX EXE in VB6 that called the 3rd party component. Works like a charm from .NET.

Also of note, I'd never created an ActiveX EXE and didn't know that regsvr32 will not work to register it. Here is the proper way to register ActiveX EXEs.

-1
votes

VB6 probably declares integer as 16 bit. This is a problem with calling DLLs from VBA too. The solution should be to change your integer to long