I have a strange issue when using a COM component in VS 2005 or 2008. If I
compile my application and run it, it works on any xp machine, but does not
work on most vista machines. I get an "Attempted to read or write protected
memory" error on a certain line.
I contacted the vendor of the com component about this issue, sent them a
sample project to demonstrate it and they were able to reproduce it with the
build i sent them. But, when they recompiled it, the issue went away. I ran
their build of my sample project on several installations of vista and all
editions and it works just fine. My question is, what could cause a problem
like this? I did a binary comparison of both builds and there are definate
differences between them (not that I know what they mean). No code was
changed at all, and i even tried changing the compile settings such as
turning off optimizations, switching between release/debug and even turned
off the "suppress JIT optimizations on module load" option in
tools->options->debugging->general and nothing changed.
What could be different about my install of visual studio 2005 and 2008 that
would produce different IL on different machines? Anyone run into this
problem before?
This may be due to ASLR and DEP (or a combination thereof) on Vista. ASLR
basically means the load/heap/stack address of the application is randomly
changed for each execution. This is a security feature that makes it hard to
exploit an application's buffer over-/under-flow flaws. DEP is another
security feature that disallows execution of code from data pages. For non
exploited applications expecting to access data a constant memory address
(ASLR) or expecting to be able to execute code from a data page, this would
cause the error you see.
I think DEP is also in XP, so it may simply be ASLR causing the problem.
As far as I know a binary has to opt into ASLR, which means the vendor is
compiling/linking with the /DYNAMICBASE command-line argument.
You could try disabling DEP to see if that makes the problem go away.
There's some good websites that explain how to do that, like :
http://vistasupport.mvps.org/disable_data_execution_prevention.htm

Signature
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#
> I have a strange issue when using a COM component in VS 2005 or 2008. If I
> compile my application and run it, it works on any xp machine, but does not
[quoted text clipped - 16 lines]
> would produce different IL on different machines? Anyone run into this
> problem before?
Jason Rodman - 23 Dec 2007 16:51 GMT
Thanks for your reply Peter. Im not sure this makes much sense, why would one
build from one machine work, and another build from a different machine not
work. Same exact vb code, same exact COM dll. If it was ASLR or DEP, wouldnt
it break for both builds? I dont think the issue is on the host vista
machine, but more in the vb compiler. What could be different in the build
that would cause this to fail?
> This may be due to ASLR and DEP (or a combination thereof) on Vista. ASLR
> basically means the load/heap/stack address of the application is randomly
[quoted text clipped - 34 lines]
> > would produce different IL on different machines? Anyone run into this
> > problem before?
Peter Ritchie [C# MVP] - 23 Dec 2007 17:10 GMT
It could be that the vendor does not have VS 2005 SP1 installed. the
/DYNAMICBASE option was added in SP1 [1]. If you were using it in your
project in VS 2005 w/SP1 and compiled it there and give it to someone else,
it would be randomizing load/stack/heap addresses; if you gave someone
without SP1 the project, they would compile it without radomized
load/stack/heap addresses and may not exhibit the problem.
[1]
http://blogs.msdn.com/michael_howard/archive/2006/09/26/visual-studio-2005-sp1-b
eta-windows-vista-and-aslr.aspx

Signature
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#
> Thanks for your reply Peter. Im not sure this makes much sense, why would one
> build from one machine work, and another build from a different machine not
[quoted text clipped - 41 lines]
> > > would produce different IL on different machines? Anyone run into this
> > > problem before?
Jason Rodman - 23 Dec 2007 18:11 GMT
I see. So by disabling this we can test to see if this is the issue. If this
does turn out to be the problem, how could I fix the issue on my end without
having to affect the end user's machine?
> It could be that the vendor does not have VS 2005 SP1 installed. the
> /DYNAMICBASE option was added in SP1 [1]. If you were using it in your
[quoted text clipped - 51 lines]
> > > > would produce different IL on different machines? Anyone run into this
> > > > problem before?
Peter Ritchie [C# MVP] - 23 Dec 2007 18:27 GMT
After a bit more research, if that's the issue, it would mean you're
compiling a C/C++ project. If you're not, it sounds like DEP; but it doesn't
explain why rebuilding makes the problem go away.
If you are compiling a C/C++ project then VS 2005 doesn't have a UI for the
DYNAMICBASE linker option; but you can add /DYNAMICBASE:NO to the linker
command-line options in the Linker/Advanced properties.
In VS 2008, there is a "Randomized Base Address" option in the
Linker/Advance properties that affects the /DYNAMICBASE command-line option
for C/C++ projects.

Signature
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#
> I see. So by disabling this we can test to see if this is the issue. If this
> does turn out to be the problem, how could I fix the issue on my end without
[quoted text clipped - 55 lines]
> > > > > would produce different IL on different machines? Anyone run into this
> > > > > problem before?
Jason Rodman - 23 Dec 2007 18:35 GMT
This is actually a vb.net project. Is there an option for it there?
> After a bit more research, if that's the issue, it would mean you're
> compiling a C/C++ project. If you're not, it sounds like DEP; but it doesn't
[quoted text clipped - 67 lines]
> > > > > > would produce different IL on different machines? Anyone run into this
> > > > > > problem before?
Peter Ritchie [C# MVP] - 23 Dec 2007 19:05 GMT
I can't verify it, but it appears VB.NET and C# applications have the ASLR
bit set in VS 2005 SP1. Without verifying it, I can't tell if there's a way
to change it or not.
If you can verify that the vendor is in fact not using SP1, that would be a
step in the right direction.

Signature
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#
> This is actually a vb.net project. Is there an option for it there?
>
[quoted text clipped - 69 lines]
> > > > > > > would produce different IL on different machines? Anyone run into this
> > > > > > > problem before?
Jason Rodman - 23 Dec 2007 19:28 GMT
Okay. I will get back to you as soon as they respond to my email. Thanks.
> I can't verify it, but it appears VB.NET and C# applications have the ASLR
> bit set in VS 2005 SP1. Without verifying it, I can't tell if there's a way
[quoted text clipped - 76 lines]
> > > > > > > > would produce different IL on different machines? Anyone run into this
> > > > > > > > problem before?
Jason Rodman - 23 Dec 2007 22:22 GMT
I had one of end users follow the instructions to disable DEP and the error
goes away, so we are on the right track. I will hunt around to see if there
is an option in the compiler options to turn off this option when i compile
it. I will let you know what I find out.
> Okay. I will get back to you as soon as they respond to my email. Thanks.
>
[quoted text clipped - 78 lines]
> > > > > > > > > would produce different IL on different machines? Anyone run into this
> > > > > > > > > problem before?
Willy Denoyette [MVP] - 24 Dec 2007 11:32 GMT
Your problem is IMO due to a COM component which uses ATL version 7.1 or
earlier.
These versions of ATL have a known bug that triggers an NX fault when run on
a system with DEP enabled for user processes. Rebuilding the COM component
with VS2005 or higher updates the ATL to a version with correct NX behavior.
Willy.
>I had one of end users follow the instructions to disable DEP and the error
> goes away, so we are on the right track. I will hunt around to see if
[quoted text clipped - 132 lines]
>> > > > > > > > > run into this
>> > > > > > > > > problem before?
Jason Rodman - 27 Dec 2007 13:05 GMT
I verified that the vendor does indeed have SP1 installed as well. Any other
suggestions?
> I can't verify it, but it appears VB.NET and C# applications have the ASLR
> bit set in VS 2005 SP1. Without verifying it, I can't tell if there's a way
[quoted text clipped - 76 lines]
> > > > > > > > would produce different IL on different machines? Anyone run into this
> > > > > > > > problem before?