473,399 Members | 3,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 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 2696
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Amadeus W. M. | last post by:
I have a member static const int x defined in class Foo, and I'm passing it by reference, and by value elsewhere (see the code below). Passing it by value works, but by reference it doesn't: it...
6
by: Darrell Wesley | last post by:
A VB2003 application upgraded to VB2005 that builds an Excel spreadsheet. Everything appears to work correctly except that the Excel object does not go away it is still in the Process list in task...
0
by: simon.hudon | last post by:
Hi everyone, I have a strange problem with precompiled headers with VC8. I have the problem while trying to use a precompiled header to build another precompiled header. I tried the sample...
2
by: Jeff | last post by:
Hey asp.net 2.0 My asp.net 2.0 project has got a assembly load problem: Some of my web.config settings: <membership defaultProvider="AH_MembershipProvider" userIsOnlineTimeWindow="15">
16
by: Bill Nguyen | last post by:
I'm running into a very weird problem regarding subtraction. Subtraction behaves as if it's an addition in the below sub txtJacoCost.Text = Format(mRackc - (mDisc + mJaEc), "0.#####0") ...
2
by: yalbizu | last post by:
#include <iostream> #include <string> #include <fstream> #include <iomanip> using namespace std; const int NO_OF_STUDENTS=20; struct studentType { string studentFName; string studentLName;
6
by: Author | last post by:
I have VS 2005 professional. Here is the version info: Microsoft Visual Studio 2005 Version 8.0.50727.762 (SP .050727-7600) &copy; 2005 Microsoft Corporation. All rights reserved. When I...
3
by: =?Utf-8?B?YzY3NjIyOA==?= | last post by:
Hi all, I cut and paste the following code from msdn help page which it just introduces view and multiview server controls. Here is what I do: in vs studio 2005, File --New Web Site, it...
1
by: elke | last post by:
Hi, I want to use an unmanaged dll in C# .net and I'm having some troubles witch a function that should return an array. I'm new at this, so I don't know what I'm doing wrong. Here is some...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.