VB.NET Accessing C DLL Problems

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • webroten
    New Member
    • Nov 2007
    • 2

    #1

    VB.NET Accessing C DLL Problems

    I've been working through trying to access a C DLL from VB.NET. I've read many online postings, but I'm still having problems. Now, my error is the "Attempted to read or write protected memory" error. Here's my Structure declaration:

    [CODE=vbnet]<StructLayout(L ayoutKind.Seque ntial, charset:=CharSe t.Ansi)> _
    Public Structure CONFIG_PARM
    <MarshalAs(Unma nagedType.LPStr )> Public address1 As String
    <MarshalAs(Unma nagedType.LPStr )> Public addrindex As String
    <MarshalAs(Unma nagedType.LPStr )> Public cdrom As String
    <MarshalAs(Unma nagedType.LPStr )> Public citystate As String
    <MarshalAs(Unma nagedType.LPStr )> Public crossref As String
    <MarshalAs(Unma nagedType.LPStr )> Public system As String
    <MarshalAs(Unma nagedType.LPStr )> Public elot As String
    <MarshalAs(Unma nagedType.LPStr )> Public elotindex As String
    <MarshalAs(Unma nagedType.LPStr )> Public llkpath As String
    <MarshalAs(Unma nagedType.LPStr )> Public ewspath As String
    <MarshalAs(Unma nagedType.LPStr )> Public dpvpath As String
    <MarshalAs(Unma nagedType.LPStr )> Public fnsnpath As String
    <MarshalAs(Unma nagedType.ByVal TStr, SizeConst:=124) > Public rsvd As String
    End Structure

    <StructLayout(L ayoutKind.Seque ntial, CharSet:=CharSe t.Ansi)> _
    Public Structure Z4OPEN_PARM
    <MarshalAs(Unma nagedType.ByVal TStr, SizeConst:=50)> Public rsvd1 As String
    <MarshalAs(Unma nagedType.I4)> Public status As Integer
    <MarshalAs(Unma nagedType.LPStr )> Public fname As String
    <MarshalAs(Unma nagedType.Struc t)> Public config As CONFIG_PARM
    <MarshalAs(Unma nagedType.ByVal TStr, SizeConst:=1)> Public ewsflag As String
    <MarshalAs(Unma nagedType.ByVal TStr, SizeConst:=1)> Public elotflag As String
    <MarshalAs(Unma nagedType.ByVal TStr, SizeConst:=1)> Public llkflag As String
    <MarshalAs(Unma nagedType.ByVal TStr, SizeConst:=1)> Public dpvflag As String
    <MarshalAs(Unma nagedType.ByVal TStr, SizeConst:=1)> Public systemflag As String
    <MarshalAs(Unma nagedType.ByVal TStr, SizeConst:=511) > Public rsvd2 As String
    End Structure[/CODE]

    The Function declarations and the code used to call them are:

    [CODE=vbnet] <DllImport("zip 4_w32.dll", CharSet:=CharSe t.Ansi)> _
    Public Shared Function z4opencfgSTD(By Ref openparm As Z4OPEN_PARM) As Integer
    End Function

    ...

    Dim openparm As New Z4OPEN_PARM
    Dim configparm As New CONFIG_PARM

    Dim address1 As String = "e:\ams\ams_com m"
    Dim addrindex As String = "e:\ams\ams_com m"
    Dim citystate As String = "e:\ams\ams_com m"
    Dim crossref As String = "e:\ams\ams_com m"
    Dim fnsnpath As String = "e:\ams\ams_com m"
    Dim elot As String = "e:\ams\ams_elo t"
    Dim elotindex As String = "e:\ams\ams_elo t"
    Dim llkpath As String = "e:\ams\lacslin k"
    Dim dpvpath As String = "e:\ams\ams_dpv "
    Dim systempath As String = "e:\projects\wr apz4\wrapz4\deb ug"
    Dim ewspath As String = ""
    Dim rsvd_string As New StringBuilder
    Dim rsvd As String = ""
    rsvd.PadLeft(12 4)
    Dim rsvd1 As String = ""
    rsvd1.PadLeft(5 0)
    Dim rsvd2 As String = ""
    rsvd2.PadLeft(5 11)
    Dim cdrom As String = ""
    Dim fname As String = ""

    configparm.addr ess1 = address1
    configparm.addr index = addrindex
    configparm.cdro m = cdrom
    configparm.city state = citystate
    configparm.cros sref = crossref
    configparm.syst em = systempath
    configparm.elot = elot
    configparm.elot index = elotindex
    configparm.llkp ath = llkpath
    configparm.ewsp ath = ewspath
    configparm.dpvp ath = dpvpath
    configparm.fnsn path = fnsnpath
    configparm.rsvd = rsvd

    openparm.status = 2
    openparm.rsvd1 = rsvd1
    openparm.rsvd2 = rsvd2
    openparm.elotfl ag = "Y"
    openparm.llkfla g = "Y"
    openparm.ewsfla g = "N"
    openparm.dpvfla g = "Y"
    openparm.system flag = "Y"
    openparm.fname = fname

    openparm.config = configparm

    Connection.Text = z4functions.z4o pencfgSTD(openp arm)[/CODE]

    Why would I be getting the memory error, "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."?

    Thanks for your help.
    Last edited by Plater; Jan 23 '08, 09:20 PM. Reason: added =vbnet to [code] tags
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Originally posted by webroten
    I've been working through trying to access a C DLL from VB.NET. I've read many online postings, but I'm still having problems. Now, my error is the "Attempted to read or write protected memory" error. Here's my Structure declaration:

    ...

    Why would I be getting the memory error, "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."?

    Thanks for your help.
    I just encountered this type of error today.
    What's happening is your C DLL is not properly using the memory allocated to it.

    In my case I had to increase the size of the array that I passed to a method because this array was not large enough to hold the data that the function was manipulating. You would think that the developers of the DLL would have managed the memory correctly but this isn't the case.

    I haven't really taken a close look at your code that you've posted...but I recommend going through and making sure that any variables declared in your .NET program are allocating enough memory for the C variables....

    Especially Strings! C code is notorious for not handling memory allocation for Strings correctly.

    (In your case...you may be providing Strings that are too long for the C DLL to handle)

    Comment

    • webroten
      New Member
      • Nov 2007
      • 2

      #3
      That corrected the problem - thanks! I'm still having problems with the DLL reading my structure, but I don't think it's related to this issue. It could be something else with the marshalling or something.

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Originally posted by webroten
        That corrected the problem - thanks! I'm still having problems with the DLL reading my structure, but I don't think it's related to this issue. It could be something else with the marshalling or something.
        I'm glad you solved the problem!
        If you need help with your other issue start a new post.

        Congratz!

        -Frinny

        Comment

        • jm1576
          New Member
          • Jan 2008
          • 3

          #5
          I am working on the same exact project. Were you able to solve your issue ? I have just received the CD from USPS. Could you post the rest of the code that call the other Z4 functions ?

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            Originally posted by jm1576
            I am working on the same exact project. Were you able to solve your issue ? I have just received the CD from USPS. Could you post the rest of the code that call the other Z4 functions ?
            Hi Jm1576,

            Webroten has only ever posted twice. This user is not very active.
            Could you please post your exact error so that we might be able to help you?

            -Frinny

            Comment

            • jm1576
              New Member
              • Jan 2008
              • 3

              #7
              I am using the above code that was posted. This the error that I get

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

              Comment

              • Frinavale
                Recognized Expert Expert
                • Oct 2006
                • 9749

                #8
                Originally posted by jm1576
                I am using the above code that was posted. This the error that I get

                "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
                This is often a sign that the unmanaged code (the C code) is not using memory correctly. The previous person found that the problem was resolved when they allocated more memory for the C code to use...or else they shortened the strings that they were passing to the C code.

                -Frinny

                Comment

                • jm1576
                  New Member
                  • Jan 2008
                  • 3

                  #9
                  Hi,
                  This is what I have changed.
                  <DllImport("zip 4_w32.dll", CallingConventi on:=CallingConv ention.StdCall, EntryPoint:="z4 opencfgSTD", ExactSpelling:= False), ReliabilityCont ract(Consistenc y.WillNotCorrup tState, Cer.None)> _
                  Public Shared Function z4opencfgSTD(<[In](), Out()> <MarshalAs(Unma nagedType.LPStr uct)> ByVal parm As Z4OPEN_PARM) As Integer
                  End Function

                  This is the error that I am getting now.
                  "Cannot marshal 'parameter #1': Invalid managed/unmanaged type combination (this value type must be paired with Struct)"

                  Comment

                  Working...