AccessViolationException in 2008 but not 2005

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rmiller407@gmail.com

    AccessViolationException in 2008 but not 2005

    I am getting an AccessViolation Exception when calling an old legacy
    fortran dll from c# in vs2008 express, but the same code worked fine
    in vs2005.

    If I create a vs2005 c# wrapper dll to call the old fortran dll, then
    I get the exception if I call that wrapper dll from a vs2008 console
    app, but not if I call it from a vs2005 console app. I've tried
    changing the vs2008 code to target the 2.0 framework, but I still get
    the exception. This is all done on the same machine.

    I'm running Xpsp2, so I don't think it's the data execution prevention
    issue. I tried using editbin to change the nxcomp bit in console app
    exe anyway, but it didn't make any difference. I also tried
    explicitly excluding that console app exe from the dep through the
    control panel, and that didn't make any difference either.

    I can call a couple of simple utility methods on the dll without
    getting the exception, but when I call the main processing method then
    it happens. The dll is old enough that I'm sure there really is a
    memory allocation or pointer problem in there. But I need to use it
    from vs2008 and I can't get the dll changed.

    Any thoughts about what changed in vs2008 that might have caused this,
    and what I might be able to do to work around it?

    If it helps, the call to the main processing method looks like this:
    [DllImport("foo. dll", EntryPoint = "foo")]
    protected static extern void foo(ref InputData inputData, ref
    OutputData outputData);

    The InputData and OutputData are structures that include old fortran
    style fixed arrays and types. They're quite large, so I won't post
    the whole thing uless you think it's important. But here's a subset
    as an example:

    [StructLayout(La youtKind.Sequen tial)]
    public struct OutputData
    {
    [MarshalAs(Unman agedType.ByValA rray, SizeConst = 10)]
    public char[] Version;
    public int ValidSolution;
    public int ErrorNo;
    [MarshalAs(Unman agedType.ByValA rray, SizeConst = 500)]
    public char[] ErrorMessage;
    public int NumberOfUnits;
    }

    Thanks in advance for your help.
  • rmiller407@gmail.com

    #2
    Re: AccessViolation Exception in 2008 but not 2005

    Thanks Marc, but I tried that. If I make a wrapper dll in vs2005,
    then it works fine if I call that wrapper from a vs2005 exe, but not
    if I call it from a vs2008 exe.

    Any other ideas?

    Comment

    • Marc Gravell

      #3
      Re: AccessViolation Exception in 2008 but not 2005

      Can I clarify; is this all on the same machine?

      Marc


      Comment

      • rmiller407@gmail.com

        #4
        Re: AccessViolation Exception in 2008 but not 2005

        Yes, it is all on the same machine.

        Rob

        Comment

        • Marc Gravell

          #5
          Re: AccessViolation Exception in 2008 but not 2005

          OK; that's odd.

          .... {stumped} ...


          Comment

          • Marc Gravell

            #6
            Re: AccessViolation Exception in 2008 but not 2005

            (Cheers, I was kind-of hoping you might spot this one ;-p)


            Comment

            • rmiller407@gmail.com

              #7
              Re: AccessViolation Exception in 2008 but not 2005

              Here's the exception message:

              System.AccessVi olationExceptio n was unhandled
              Message="Attemp ted to read or write protected memory. This is often
              an indication that other memory is corrupt."
              Source="OldFort ranDllWrapper"
              StackTrace:
              at OldFortranDllWr apper.FortranDl l.foo(InputData & inputData,
              OutputData& outputData)
              <snipthe rest of the stack trace is just my code down to this call </
              snip>
              InnerException:

              I agree that it is probably a bad pointer or memory allocation in the
              fortran dll. But it works when the calling c# exe was created in
              vs2005, but not in vs2008. Is vs2008 less forgiving of this type of
              thing, and if so is there a way of turning that off or working around
              this?

              Rob

              Comment

              • Willy Denoyette [MVP]

                #8
                Re: AccessViolation Exception in 2008 but not 2005

                "Marc Gravell" <marc.gravell@g mail.comwrote in message
                news:%23H9fRuma IHA.1208@TK2MSF TNGP03.phx.gbl. ..
                (Cheers, I was kind-of hoping you might spot this one ;-p)
                >

                Well, all I know is that the OP is dealing with some invalid pointer issue.
                One could argue why this works in V1.X and not in V2, one of the the reasons
                might be that V2 is linked with the safe CRT library, and the interop layer
                enforces more strict pointer validation rules. So it's possible that this
                faulty code ran 'perfectly' well on V1.X but fails (as it should) on V2.

                Willy.

                Comment

                • rmiller407@gmail.com

                  #9
                  Re: AccessViolation Exception in 2008 but not 2005

                  Thanks Willy. That makes sense, and that means I'm probably out of
                  luck. I may have to try to reverse-engineer that old dll. :-(

                  Rob

                  Comment

                  • Marc Gravell

                    #10
                    Re: AccessViolation Exception in 2008 but not 2005

                    One could argue why this works in V1.X and not in V2

                    2005 vs 2008 - both v2 and the same runtime, surely?

                    Comment

                    • Willy Denoyette [MVP]

                      #11
                      Re: AccessViolation Exception in 2008 but not 2005

                      "Marc Gravell" <marc.gravell@g mail.comwrote in message
                      news:6e6a8994-a741-4a40-8afb-5da20fca4620@s8 g2000prg.google groups.com...
                      >One could argue why this works in V1.X and not in V2
                      >
                      2005 vs 2008 - both v2 and the same runtime, surely?

                      Sure, my bad, I meant V2. and V2 SP1. Sorry for the confusion.

                      Willy.

                      Comment

                      • rmiller407@gmail.com

                        #12
                        Re: AccessViolation Exception in 2008 but not 2005

                        Running dumpbin /headers | findstr "DLL characteristics " on the exe
                        didn't produce any output. I had tried running editbin /nxcompat:no
                        on the exe before and it did not make any difference.
                        Mind to post some of the caller's code, or a small but complete sample that
                        illustrates the problem?
                        Would you mind if I just emailed an example project to you, Willy?
                        The fortran dll comes from another company and I am not comfortable
                        posting it all to a public site.

                        Rob

                        Comment

                        • Willy Denoyette [MVP]

                          #13
                          Re: AccessViolation Exception in 2008 but not 2005

                          <rmiller407@gma il.comwrote in message
                          news:43439c23-3a87-4516-9f1c-ee3f9ff68313@b2 g2000hsg.google groups.com...
                          Running dumpbin /headers | findstr "DLL characteristics " on the exe
                          didn't produce any output. I had tried running editbin /nxcompat:no
                          on the exe before and it did not make any difference.
                          >
                          >Mind to post some of the caller's code, or a small but complete sample
                          >that
                          >illustrates the problem?
                          >
                          Would you mind if I just emailed an example project to you, Willy?
                          The fortran dll comes from another company and I am not comfortable
                          posting it all to a public site.
                          >
                          Rob

                          Ok, go ahead ;-)

                          Willy.

                          Comment

                          Working...