By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,983 Members | 1,627 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,983 IT Pros & Developers. It's quick & easy.

AccessViolationException in 2008 but not 2005

P: n/a
I am getting an AccessViolationException 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(LayoutKind.Sequential)]
public struct OutputData
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
public char[] Version;
public int ValidSolution;
public int ErrorNo;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 500)]
public char[] ErrorMessage;
public int NumberOfUnits;
}

Thanks in advance for your help.
Feb 8 '08 #1
Share this Question
Share on Google+
12 Replies


P: n/a
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?
Feb 8 '08 #2

P: n/a
Can I clarify; is this all on the same machine?

Marc
Feb 8 '08 #3

P: n/a
Yes, it is all on the same machine.

Rob
Feb 8 '08 #4

P: n/a
OK; that's odd.

.... {stumped} ...
Feb 8 '08 #5

P: n/a
(Cheers, I was kind-of hoping you might spot this one ;-p)
Feb 8 '08 #6

P: n/a
Here's the exception message:

System.AccessViolationException was unhandled
Message="Attempted to read or write protected memory. This is often
an indication that other memory is corrupt."
Source="OldFortranDllWrapper"
StackTrace:
at OldFortranDllWrapper.FortranDll.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
Feb 8 '08 #7

P: n/a
"Marc Gravell" <ma**********@gmail.comwrote in message
news:%2****************@TK2MSFTNGP03.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.

Feb 8 '08 #8

P: n/a
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
Feb 8 '08 #9

P: n/a
One could argue why this works in V1.X and not in V2

2005 vs 2008 - both v2 and the same runtime, surely?
Feb 8 '08 #10

P: n/a
"Marc Gravell" <ma**********@gmail.comwrote in message
news:6e**********************************@s8g2000p rg.googlegroups.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.

Feb 8 '08 #11

P: n/a
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
Feb 11 '08 #12

P: n/a
<rm********@gmail.comwrote in message
news:43**********************************@b2g2000h sg.googlegroups.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.

Feb 11 '08 #13

This discussion thread is closed

Replies have been disabled for this discussion.