Trouble upgrading from VB6 to

Posted on 2006-05-18
Last Modified: 2012-06-22
I have some code that interfaces with a 3rd party dll and works fine in VB 6, but when I try to upgrade it to .net Im getting some errors.  This Dll receives several structures as parameters, and within these structures are several fixed lengthen strings, and Arrays.      I think passing these is the problem.  Here is the error Im getting when I call a function in the dll:

“Type could not be marshaled because the length of an embedded array instance does not match the declared length in the layout.”

I’ve include several lines that the upgrade wizard changed.
The original code was:

Private Type in_record
path_str As String * 80        
mstrUser_Code As String * 48
TraceFile As String * 80
End type

After .net did its upgrade this is the code produced.
Private Structure in_record
<VBFixedString(80), System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst:=81)> Public path_str() As Char
'UPGRADE_WARNING: Fixed-length string size must fit in the buffer.
<VBFixedString(48), System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst:=50)> Public mstrUser_Code() As Char
'UPGRADE_WARNING: Fixed-length string size must fit in the buffer.
 <VBFixedString(80), System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst:=81)> Public TraceFile() As Char
End Structure

If just for kicks change these vars to just plan ol strings.  I get this error when the dll is called:
“Attempted to read or write protected memory. This is often an indication that other memory is corrupt.”

Any help would be appreciated, thanks.
Question by:huston8481
    LVL 85

    Accepted Solution


        Private Structure in_record
           <VBFixedString(80)> Public path_strAs String
           <VBFixedString(48)> Public mstrUser_Code As String
           <VBFixedString(80)> Public TraceFile As String
        End Structure

    LVL 85

    Expert Comment

    by:Mike Tomlinson
    I missed a space in that first line...

             <VBFixedString(80)> Public path_str As String
    LVL 44

    Expert Comment

    the reason for the problem is the where VB 6 allows FIXED LENGTH strings (Dim MyString as String * 80)

    VB.NET DOES NOT allow Fixed length strings, except by using the <VBFixedString(80)>  attribute declaration, as noted by Idle_Mind


    Author Comment

    Thanks guys I will try out your suggestion and get back to you.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Introduction When many people think of the WebBrowser ( control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now