Problem with Marshal class in Visual Studio 2005

  • Thread starter Thread starter william.thorpe.b
  • Start date Start date
W

william.thorpe.b

I have recently switched from VS2003 to VS2005 and at the same time
from V1 to V2 of the .NET Compact Framework. The target is a Windows
CE 5.0 device and an ARMV4I processor.

System.Runtime.InteropServices.Marshal.WriteInt32 used to work fine but
now is misbehaving.

I wrote some native-code (C++) alternates to some Marshal methods and
when I P/Invoke them they work fine; I can write to an address and I
read back the same value that I just wrote.

When I use Marshal.WriteInt32, the value that I read back (with either
technique) gives a bogus value. I get the same bogus value regardless
of what value I wrote to the address.

Any ideas?

Thanks,
Bill
 
Bill,

Without seeing any code (to call Marshal, or your method, or the
implementation of your method), it would be hard to say.

Can you provide any of those things?
 
Hi Nicholas,

Here is the source code. Hopefully you will see something wrong. Has
the Marshal class changed from V1 to V2? Is there now some
trick/configuration to using it properly in V2?

The C# code looks like...

[DllImport( "Marshaler.dll" )]
private static extern void MyWriteInt32( IntPtr addr, Int32 offset,
Int32 value );

[DllImport( "Marshaler.dll" )]
private static extern Int32 MyReadInt32( IntPtr addr, Int32 offset );

unsafe void Foo()
{
IntPtr ptr = new IntPtr( (void*)0xa8702004 );
Int32 regVal = 0;

// Marshal works correctly with VS2003 but not VS2005
regVal = Marshal.ReadInt32( ptr ); // reads 0x2000ffe0 (correct)
Marshal.WriteInt32( ptr, regVal ); // write save value that was
just read
regVal = Marshal.ReadInt32( ptr ); // reads 0x20002020 (wrong)
Marshal.WriteInt32( ptr, 0x2000ffe1 );
regVal = Marshal.ReadInt32( ptr ); // reads 0x20002020 (wrong)
Marshal.WriteInt32( ptr, 0x2000ffe0 );
regVal = Marshal.ReadInt32( ptr ); // reads 0x20002020 (wrong)

// Using P/Invoke to my own routines works in both VS2003 and
VS2005
regVal = MyReadInt32( ptr, 0 ); // reads 0x20002020 (wrong)
MyWriteInt32( ptr, 0, 0x2000ffe2 );
regVal = MyReadInt32( ptr, 0 ); // reads 0x2000ffe2 (correct)
MyWriteInt32( ptr, 0, 0x2000ffe0 );
regVal = MyReadInt32( ptr, 0 ); // reads 0x2000ffe0 (correct)

regVal = Marshal.ReadInt32( ptr ); // reads 0x2000ffe0 (correct)
}

// The C++ functions in Marshaler.dll look like (extern "C" in .h
file)...
__declspec(dllexport) void MyWriteInt32( void * pAddr, int offset, int
value )
{
volatile int * ptr = reinterpret_cast<int *>(pAddr) + offset;
*ptr = value;
}

__declspec(dllexport) int MyReadInt32( void * pAddr, int offset )
{
return *( reinterpret_cast<int *>(pAddr) + offset );
}
 
This:

IntPtr ptr = new IntPtr( (void*)0xa8702004 );

indicates that you run on 64bit windows, or you have /4GT tuning enabled,
but the question is what is ptr pointing at and how do you know it's not a
moving target, or a memory location you don't own?
I'm not clear on what you are trying to achieve, but this looks very unsafe.


Willy.

| Hi Nicholas,
|
| Here is the source code. Hopefully you will see something wrong. Has
| the Marshal class changed from V1 to V2? Is there now some
| trick/configuration to using it properly in V2?
|
| The C# code looks like...
|
| [DllImport( "Marshaler.dll" )]
| private static extern void MyWriteInt32( IntPtr addr, Int32 offset,
| Int32 value );
|
| [DllImport( "Marshaler.dll" )]
| private static extern Int32 MyReadInt32( IntPtr addr, Int32 offset );
|
| unsafe void Foo()
| {
| IntPtr ptr = new IntPtr( (void*)0xa8702004 );
| Int32 regVal = 0;
|
| // Marshal works correctly with VS2003 but not VS2005
| regVal = Marshal.ReadInt32( ptr ); // reads 0x2000ffe0 (correct)
| Marshal.WriteInt32( ptr, regVal ); // write save value that was
| just read
| regVal = Marshal.ReadInt32( ptr ); // reads 0x20002020 (wrong)
| Marshal.WriteInt32( ptr, 0x2000ffe1 );
| regVal = Marshal.ReadInt32( ptr ); // reads 0x20002020 (wrong)
| Marshal.WriteInt32( ptr, 0x2000ffe0 );
| regVal = Marshal.ReadInt32( ptr ); // reads 0x20002020 (wrong)
|
| // Using P/Invoke to my own routines works in both VS2003 and
| VS2005
| regVal = MyReadInt32( ptr, 0 ); // reads 0x20002020 (wrong)
| MyWriteInt32( ptr, 0, 0x2000ffe2 );
| regVal = MyReadInt32( ptr, 0 ); // reads 0x2000ffe2 (correct)
| MyWriteInt32( ptr, 0, 0x2000ffe0 );
| regVal = MyReadInt32( ptr, 0 ); // reads 0x2000ffe0 (correct)
|
| regVal = Marshal.ReadInt32( ptr ); // reads 0x2000ffe0 (correct)
| }
|
| // The C++ functions in Marshaler.dll look like (extern "C" in .h
| file)...
| __declspec(dllexport) void MyWriteInt32( void * pAddr, int offset, int
| value )
| {
| volatile int * ptr = reinterpret_cast<int *>(pAddr) + offset;
| *ptr = value;
| }
|
| __declspec(dllexport) int MyReadInt32( void * pAddr, int offset )
| {
| return *( reinterpret_cast<int *>(pAddr) + offset );
| }
|
 
We are running on Windows CE 5.0 (32bit) and the address is definitely
valid.

Does the Marshal class in V2 of the compact framework have trouble with
a big address? It doesn't in V1.

-Bill
 
| We are running on Windows CE 5.0 (32bit) and the address is definitely
| valid.
|
| Does the Marshal class in V2 of the compact framework have trouble with
| a big address? It doesn't in V1.
|
| -Bill
|

Sorry, I missed this one in your original post.
Ok, the User space sits at the upper range so it could be a valid address,
but the question remains, what is this pointer pointing at? If it points to
the GC heap, I won't be supprised the contents changes when the GC compacts
the heap, it can also point to a location not owned by the process, I don't
know how user address space is laid-out in CE. Guess you might get better
answers when you post such questions to the compactframework NG.
Willy.
 
This particular address allows us to control the chip select
configuration. It is definitely valid for me to read and write it and
I only picked this address as a test case to illustrate the bigger
problem. I have a lot of code (many addresses) that works fine under
vs2003 but not under vs2005. Something changed - maybe a V2 framework
bug?

I did post my original question to the compactframework newsgroup at
the same time I posted here but there have not been any replies yet.

Thanks,
Bill
 
Hi Bill,

I have the same problem than yours!

I am working to the developpement of a Win CE 5.0 application with a PXA270 cpu on a Toradex evaluation board. I'm using .Net compact framework 2.0.

I need to configure the GPIO registry. Then I read and write in memory. I am sure than I used the valid adress. But like you when I write in memory the result is not the one I am waiting for. Like you I am using Marshal.ReadInt32(ptr) and Marshal.WriteInt32(ptr, value) methods.

My code is very near then yours in C#. When I tried in native code everything is working fine. I don't know if somebody can help us. You posted this problem a long time ago... I don't feel very confident!!!

I hope somebody has an answer.

Jonathan Lacroix



From http://www.developmentnow.com/g/36_...m-with-Marshal-class-in-Visual-Studio-2005.ht

Posted via DevelopmentNow.com Group
http://www.developmentnow.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top