473,574 Members | 2,701 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.WriteI nt32 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.WriteIn t32, 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 2713
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.co m

<wi************ **@bayer.com> wrote in message
news:11******** **************@ g47g2000cwa.goo glegroups.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.WriteI nt32 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.WriteIn t32, 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*)0xa87020 04 );
Int32 regVal = 0;

// Marshal works correctly with VS2003 but not VS2005
regVal = Marshal.ReadInt 32( ptr ); // reads 0x2000ffe0 (correct)
Marshal.WriteIn t32( ptr, regVal ); // write save value that was
just read
regVal = Marshal.ReadInt 32( ptr ); // reads 0x20002020 (wrong)
Marshal.WriteIn t32( ptr, 0x2000ffe1 );
regVal = Marshal.ReadInt 32( ptr ); // reads 0x20002020 (wrong)
Marshal.WriteIn t32( ptr, 0x2000ffe0 );
regVal = Marshal.ReadInt 32( 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.ReadInt 32( ptr ); // reads 0x2000ffe0 (correct)
}

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

__declspec(dlle xport) int MyReadInt32( void * pAddr, int offset )
{
return *( reinterpret_cas t<int *>(pAddr) + offset );
}

Feb 15 '06 #3
This:

IntPtr ptr = new IntPtr( (void*)0xa87020 04 );

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.goo glegroups.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*)0xa87020 04 );
| Int32 regVal = 0;
|
| // Marshal works correctly with VS2003 but not VS2005
| regVal = Marshal.ReadInt 32( ptr ); // reads 0x2000ffe0 (correct)
| Marshal.WriteIn t32( ptr, regVal ); // write save value that was
| just read
| regVal = Marshal.ReadInt 32( ptr ); // reads 0x20002020 (wrong)
| Marshal.WriteIn t32( ptr, 0x2000ffe1 );
| regVal = Marshal.ReadInt 32( ptr ); // reads 0x20002020 (wrong)
| Marshal.WriteIn t32( ptr, 0x2000ffe0 );
| regVal = Marshal.ReadInt 32( 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.ReadInt 32( ptr ); // reads 0x2000ffe0 (correct)
| }
|
| // The C++ functions in Marshaler.dll look like (extern "C" in .h
| file)...
| __declspec(dlle xport) void MyWriteInt32( void * pAddr, int offset, int
| value )
| {
| volatile int * ptr = reinterpret_cas t<int *>(pAddr) + offset;
| *ptr = value;
| }
|
| __declspec(dlle xport) int MyReadInt32( void * pAddr, int offset )
| {
| return *( reinterpret_cas t<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.goo glegroups.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 compactframewor k 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 compactframewor k 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
2569
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 thinks x is undefined. Could someone explain what's going on here? Why can't I pass a static const member by reference? This is how I compile it: ...
6
2284
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 manager. This problem did not exist in the VB2003 version. Are there a any ideas of what might be happening? All of the Excel objects are...
0
1933
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 code provided on msdn for the use of /Yu /Yc and while it works well with VC++ 7.0, it doesn't with VC++ 8.0...
2
3372
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
1755
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") txtWfCost.Text = Format(mRackc - mDisc + mWfEC, "0.#####0")
2
14891
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
1861
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 try to create a new project, I don't have a web application project option. In the New Project window, I select Visual C# under Project types, and on...
3
2920
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 brings me to the dir: C:\Visual Studio 2005\WebSites\WebSite1, it creates default.aspx and default.aspx.vb and I pasted the following code into...
1
5786
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 code: #define USERINT_FUNC __cdecl #ifdef __cplusplus extern "C" { #endif
0
8065
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8246
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8102
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6458
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5303
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3742
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3755
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2250
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1063
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.