471,350 Members | 1,410 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

Problem with Marshal class in Visual Studio 2005

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

Feb 14 '06 #1
6 2596
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?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<wi**************@bayer.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
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

Feb 14 '06 #2
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 );
}

Feb 15 '06 #3
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.

<wi**************@bayer.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
| 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 );
| }
|
Feb 15 '06 #4
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

Feb 16 '06 #5

<wi**************@bayer.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
| 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.
Feb 16 '06 #6
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

Feb 16 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by Amadeus W. M. | last post: by
6 posts views Thread by Darrell Wesley | last post: by
reply views Thread by simon.hudon | last post: by
2 posts views Thread by Jeff | last post: by
16 posts views Thread by Bill Nguyen | last post: by
3 posts views Thread by =?Utf-8?B?YzY3NjIyOA==?= | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.