Read and Write Memory

G

Giuseppe

Hi all, I'm novice with winCE and i'm writing a C# code to access to the
memory.
I'm using ceddk.dll functions:

....

public struct PHYSICAL_ADDRESS{
long lowPart;
long highPart;
public PHYSICAL_ADDRESS(long lPart,long hPart){
lowPart=lPart;
highPart=hPart;
}
}

[DllImport("ceddk.dll", EntryPoint="READ_REGISTER_ULONG")]
public static extern ulong READ_REGISTER_ULONG(ulong pulongAddress);

[DllImport("ceddk.dll", EntryPoint="MmMapIoSpace")]
public static extern unsafe void* MmMapIoSpace(PHYSICAL_ADDRESS
pucharAddress, ulong nbytes,bool cacheEnable);


private void button1_Click(object sender, System.EventArgs e)
{
unsafe
{
ulong* pmem;
PHYSICAL_ADDRESS pAdd;
pAdd= new PHYSICAL_ADDRESS(0x80920000,0x00);
pmem= (ulong*)MmMapIoSpace ( pAdd, 0x10, false );
}
}
....

This code raise an exception, please could you help me with a piece of
code?
Thank you
Giuseppe
 
G

Giuseppe

Il Mon, 21 Mar 2005 11:14:38 -0500, Chris Tacke, eMVP ha scritto:
An unmanaged ULONG and a managed ulong are not the same. Use a uint or
UInt32.

Sorry, i don't understand your answer...
Where is the problem?I think here:

[DllImport("ceddk.dll", EntryPoint="MmMapIoSpace")]
public static extern unsafe void* MmMapIoSpace(PHYSICAL_ADDRESS
pucharAddress, ulong nbytes,bool cacheEnable);

because when i try:

PHYSICAL_ADDRESS pAdd;
pAdd= new PHYSICAL_ADDRESS(0x80920000,0x00);
MmMapIoSpace ( pAdd, 0x10, false ); //this function raise exception!

i have an error unmanaged.

Thank you very much for your help
Giuseppe
 
P

Peter Foot [MVP]

As Chris suggested, change your declaration to:-

[DllImport("ceddk.dll", EntryPoint="MmMapIoSpace")]
public static extern unsafe void* MmMapIoSpace(PHYSICAL_ADDRESS
pucharAddress, uint nbytes,bool cacheEnable);

Peter

--
Peter Foot
Windows Embedded MVP
http://www.inthehand.com | http://blog.opennetcf.org/pfoot/

Giuseppe said:
Il Mon, 21 Mar 2005 11:14:38 -0500, Chris Tacke, eMVP ha scritto:
An unmanaged ULONG and a managed ulong are not the same. Use a uint or
UInt32.

Sorry, i don't understand your answer...
Where is the problem?I think here:

[DllImport("ceddk.dll", EntryPoint="MmMapIoSpace")]
public static extern unsafe void* MmMapIoSpace(PHYSICAL_ADDRESS
pucharAddress, ulong nbytes,bool cacheEnable);

because when i try:

PHYSICAL_ADDRESS pAdd;
pAdd= new PHYSICAL_ADDRESS(0x80920000,0x00);
MmMapIoSpace ( pAdd, 0x10, false ); //this function raise exception!

i have an error unmanaged.

Thank you very much for your help
Giuseppe
 
C

Chris Tacke, eMVP

In managed code, a ulong is 64 bits. In unmanaged code, a ULONG is 32 bits.
Therein lies the problem. I would use this (this is simply typed into the
OE editor, so it's not been checked, compiled, etc):

[DllImport("ceddk.dll", EntryPoint="MmMapIoSpace, SetLastError=true")]
public static extern IntPtr MmMapIoSpace(uint pucharAddress, uint nbytes,
int cacheEnable);

private const uint MY_ADDRESS = 0x80920000;
IntPtr pMem = MmMapIoSpace(MY_ADDRESS, 0x10, 0);

Also, you may want to check, but the WRITE_REGISTER_xxxx functions are
usually macros, and not exported. If you get a MissingMEthodException
there, that's the reason.

--
Chris Tacke
Co-founder
OpenNETCF.org
Has OpenNETCF helped you? Consider donating to support us!
http://www.opennetcf.org/donate


Giuseppe said:
Il Mon, 21 Mar 2005 11:14:38 -0500, Chris Tacke, eMVP ha scritto:
An unmanaged ULONG and a managed ulong are not the same. Use a uint or
UInt32.

Sorry, i don't understand your answer...
Where is the problem?I think here:

[DllImport("ceddk.dll", EntryPoint="MmMapIoSpace")]
public static extern unsafe void* MmMapIoSpace(PHYSICAL_ADDRESS
pucharAddress, ulong nbytes,bool cacheEnable);

because when i try:

PHYSICAL_ADDRESS pAdd;
pAdd= new PHYSICAL_ADDRESS(0x80920000,0x00);
MmMapIoSpace ( pAdd, 0x10, false ); //this function raise exception!

i have an error unmanaged.

Thank you very much for your help
Giuseppe
 
C

Chris Tacke, eMVP

Oops, I's stick with your PHYSICAL_MEMORY class use. I typically use
VirtualAlloc which takes the uint.

--
Chris Tacke
Co-founder
OpenNETCF.org
Has OpenNETCF helped you? Consider donating to support us!
http://www.opennetcf.org/donate


Giuseppe said:
Il Mon, 21 Mar 2005 11:14:38 -0500, Chris Tacke, eMVP ha scritto:
An unmanaged ULONG and a managed ulong are not the same. Use a uint or
UInt32.

Sorry, i don't understand your answer...
Where is the problem?I think here:

[DllImport("ceddk.dll", EntryPoint="MmMapIoSpace")]
public static extern unsafe void* MmMapIoSpace(PHYSICAL_ADDRESS
pucharAddress, ulong nbytes,bool cacheEnable);

because when i try:

PHYSICAL_ADDRESS pAdd;
pAdd= new PHYSICAL_ADDRESS(0x80920000,0x00);
MmMapIoSpace ( pAdd, 0x10, false ); //this function raise exception!

i have an error unmanaged.

Thank you very much for your help
Giuseppe
 
G

Giuseppe

Il Mon, 21 Mar 2005 16:44:34 -0000, Peter Foot [MVP] ha scritto:
[DllImport("ceddk.dll", EntryPoint="MmMapIoSpace")]
public static extern unsafe void* MmMapIoSpace(PHYSICAL_ADDRESS
pucharAddress, uint nbytes,bool cacheEnable);

Thank you Peter but doesn't change anything. I think that the dll is not in
the search path, is it possible?
Giuseppe
 
P

Peter Foot [MVP]

So what exactly is the error? Can you post the full error description, codes
etc

Peter

--
Peter Foot
Windows Embedded MVP
http://www.inthehand.com | http://blog.opennetcf.org/pfoot/

Giuseppe said:
Il Mon, 21 Mar 2005 16:44:34 -0000, Peter Foot [MVP] ha scritto:
[DllImport("ceddk.dll", EntryPoint="MmMapIoSpace")]
public static extern unsafe void* MmMapIoSpace(PHYSICAL_ADDRESS
pucharAddress, uint nbytes,bool cacheEnable);

Thank you Peter but doesn't change anything. I think that the dll is not
in
the search path, is it possible?
Giuseppe
 
P

Peter Foot [MVP]

One option would be to use a ulong (UInt64) passed by reference for the
physical address since you can't pass values greater than 4bytes by value
e.g.

[DllImport("ceddk.dll", EntryPoint="MmMapIoSpace")]
public static extern IntPtr MmMapIoSpace(ref ulong pucharAddress, uint
nbytes,bool cacheEnable);

Peter

--
Peter Foot
Windows Embedded MVP
http://www.inthehand.com | http://blog.opennetcf.org/pfoot/

Giuseppe said:
Il Mon, 21 Mar 2005 16:44:34 -0000, Peter Foot [MVP] ha scritto:
[DllImport("ceddk.dll", EntryPoint="MmMapIoSpace")]
public static extern unsafe void* MmMapIoSpace(PHYSICAL_ADDRESS
pucharAddress, uint nbytes,bool cacheEnable);

Thank you Peter but doesn't change anything. I think that the dll is not
in
the search path, is it possible?
Giuseppe
 
C

Chris Tacke, eMVP

Did you physically confirm with Explorer that CEDDK is there?

--
Chris Tacke
Co-founder
OpenNETCF.org
Has OpenNETCF helped you? Consider donating to support us!
http://www.opennetcf.org/donate


Giuseppe said:
Il Mon, 21 Mar 2005 16:44:34 -0000, Peter Foot [MVP] ha scritto:
[DllImport("ceddk.dll", EntryPoint="MmMapIoSpace")]
public static extern unsafe void* MmMapIoSpace(PHYSICAL_ADDRESS
pucharAddress, uint nbytes,bool cacheEnable);

Thank you Peter but doesn't change anything. I think that the dll is not
in
the search path, is it possible?
Giuseppe
 
C

Chris Tacke, eMVP

Another oops - use your struct, but modify it, again becasue you've got the
32 and 64 bit mix.

public struct PHYSICAL_ADDRESS
{
public uint lowPart;
public uint highPart;
}


--
Chris Tacke
Co-founder
OpenNETCF.org
Has OpenNETCF helped you? Consider donating to support us!
http://www.opennetcf.org/donate


Chris Tacke said:
Oops, I's stick with your PHYSICAL_MEMORY class use. I typically use
VirtualAlloc which takes the uint.

--
Chris Tacke
Co-founder
OpenNETCF.org
Has OpenNETCF helped you? Consider donating to support us!
http://www.opennetcf.org/donate


Giuseppe said:
Il Mon, 21 Mar 2005 11:14:38 -0500, Chris Tacke, eMVP ha scritto:
An unmanaged ULONG and a managed ulong are not the same. Use a uint or
UInt32.

Sorry, i don't understand your answer...
Where is the problem?I think here:

[DllImport("ceddk.dll", EntryPoint="MmMapIoSpace")]
public static extern unsafe void* MmMapIoSpace(PHYSICAL_ADDRESS
pucharAddress, ulong nbytes,bool cacheEnable);

because when i try:

PHYSICAL_ADDRESS pAdd;
pAdd= new PHYSICAL_ADDRESS(0x80920000,0x00);
MmMapIoSpace ( pAdd, 0x10, false ); //this function raise exception!

i have an error unmanaged.

Thank you very much for your help
Giuseppe
 
G

Giuseppe

Il Mon, 21 Mar 2005 12:13:56 -0500, Chris Tacke, eMVP ha scritto:
Did you physically confirm with Explorer that CEDDK is there?

No, i can't. I have ceddk.dll generated by PB but where must be placed?In
the same folder of my app?
Giuseppe
 
G

Giuseppe

Il Mon, 21 Mar 2005 17:02:09 -0000, Peter Foot [MVP] ha scritto:
So what exactly is the error? Can you post the full error description, codes
etc

Peter

This is all code
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;

namespace TestMemory
{
// <summary>
/// Descrizione di riepilogo per Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Timer timer1;

public Form1()
{
//
// Necessario per il supporto di Progettazione Windows Form
//
InitializeComponent();

//
// TODO: aggiungere il codice del costruttore dopo la chiamata a
InitializeComponent
//
}
/// <summary>
/// Pulire le risorse in uso.
/// </summary>
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#region Codice generato da Progettazione Windows Form
/// <summary>
/// Metodo necessario per il supporto della finestra di progettazione.
Non modificare
/// il contenuto del metodo con l'editor di codice.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.timer1 = new System.Windows.Forms.Timer();
//
// button1
//
this.button1.Location = new System.Drawing.Point(248, 16);
this.button1.Size = new System.Drawing.Size(80, 24);
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.ClientSize = new System.Drawing.Size(354, 120);
this.Controls.Add(this.button1);
this.Text = "Form1";

}
#endregion

/// <summary>
/// Il punto di ingresso principale dell'applicazione.
/// </summary>

static void Main()
{
Application.Run(new Form1());
}



[DllImport("ceddk.dll", EntryPoint="READ_REGISTER_ULONG")]
public static extern ulong READ_REGISTER_ULONG(ulong pulongAddress);

[DllImport("ceddk.dll", EntryPoint="MmMapIoSpace, SetLastError=true")]
public static extern IntPtr MmMapIoSpace(uint pucharAddress, uint nbytes,
int cacheEnable);

private void button1_Click(object sender, System.EventArgs e)
{
const uint MY_ADDRESS = 0x80920000;
IntPtr pMem = MmMapIoSpace(MY_ADDRESS, 0x10, 0);
}


}


}

When I click button1 in the Emulator, occurs an unknown error.
Thank guys for your help
 
G

Giuseppe

Il Mon, 21 Mar 2005 12:35:51 -0500, Chris Tacke, eMVP ha scritto:
It must be in the \Windows folder

Nothing to do {:-((. I placed ceddk.dll in c:\windows and
c:\windows\system32. I generated ceddk.dll for ARM micro but emulator is
for x86, right?
Sorry and I really thank you for your help.
Is there some book to learn how develop CE application/drivers?
Giuseppe
 
G

Giuseppe

Il Mon, 21 Mar 2005 11:45:39 -0500, Chris Tacke, eMVP ha scritto:
In managed code, a ulong is 64 bits. In unmanaged code, a ULONG is 32 bits.
Therein lies the problem. I would use this (this is simply typed into the
OE editor, so it's not been checked, compiled, etc):

[DllImport("ceddk.dll", EntryPoint="MmMapIoSpace, SetLastError=true")]
public static extern IntPtr MmMapIoSpace(uint pucharAddress, uint nbytes,
int cacheEnable);

private const uint MY_ADDRESS = 0x80920000;
IntPtr pMem = MmMapIoSpace(MY_ADDRESS, 0x10, 0);

Also, you may want to check, but the WRITE_REGISTER_xxxx functions are
usually macros, and not exported. If you get a MissingMEthodException
there, that's the reason.

Now i have the error on my device:
Error
TestMemory.exe
MissingMethodException
....

what can I do? Thanks
Giuse
 
C

Chris Tacke, eMVP

You can't write drivers in managed code. There are no books or even
articles that I'm aware of that deal with talking directly from managed code
to hardware - I've thought about doing it, but my proposal for an MEDC talk
on the subject was declined so I've decided to work on other things.

What you need to understand to get where you're trying to go:
- How to write a driver from unmanaged code
- In-depth knowledge of how the managed marshaler works
- Knowledge of the CF GC

If I were to make a recommendation, I'd say the first thing you need to do
is write a simple driver that turns on and off a hardware bit (GPIO) using
C. Once you have that, try to migrate the "guts" of the driver to managed
code or simply wrap the driver you create with a managed interface.

--
Chris Tacke
Co-founder
OpenNETCF.org
Has OpenNETCF helped you? Consider donating to support us!
http://www.opennetcf.org/donate
 
G

Giuseppe

Il Mon, 21 Mar 2005 13:22:05 -0500, Chris Tacke, eMVP ha scritto:
You can't write drivers in managed code. There are no books or even
articles that I'm aware of that deal with talking directly from managed code
to hardware - I've thought about doing it, but my proposal for an MEDC talk
on the subject was declined so I've decided to work on other things.

What you need to understand to get where you're trying to go:
- How to write a driver from unmanaged code
- In-depth knowledge of how the managed marshaler works
- Knowledge of the CF GC

If I were to make a recommendation, I'd say the first thing you need to do
is write a simple driver that turns on and off a hardware bit (GPIO) using
C. Once you have that, try to migrate the "guts" of the driver to managed
code or simply wrap the driver you create with a managed interface.

Thank you very very much for your help...
Bye
Giuseppe
 
D

Daniel Moth

Maybe you can achieve your goal with VirtualAlloc/VisrtualCopy... Search my
blog for an example...

Cheers
Daniel
 

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

Top