473,385 Members | 1,341 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,385 software developers and data experts.

How to directly access the memory using an intPtr

I have imported a corporate image handling COM object in my C#
project.
To access in a fast way the memory of the image there is a method that
returns a pointer to the memory (in byte) of the underlying image.
This method can be used to read as well to rewrite the pixel values.
In the interface .idl file the method is declared as:

[helpstring("Get a pointer to the pixels of a plane")]
HRESULT GetPlane([out,size_is(,*dim)] BYTE ** pPlane,
[out,unique] long *dim);

C# imports the method as:
void GetPlane(intPtr pPlane, out int dim)

I haven't found a way to directly access (read and write) the memory
pointed by intPtw without the need of copying to managed memory.

An example of error:

int planeSize;
byte b;
int v;
unsafe
{
IntPtr pPlane = new IntPtr((void*)&b); // here I tried with pPlane =
new IntPtr.Zero but gave me NullReferenceException
img.GetPlane(pPlane, out planeSize);
byte* pByte = (byte*)pPlane.ToPointer();
for (int i = 0; i < planeSize; i++)
v = greylevels[*(pByte + i)]++; // access violation exception
}

Is it possible to do it? How could I do?
Thanks

Jul 27 '07 #1
1 15587
Beorne,

You are going to need to pass the address of a pointer to the GetPlane
method, like so:

// The size of the plane.
int planeSize = 0;

// This will be populated with the pointer to your array.
IntPtr array = IntPtr.Zero;

// Unsafe code.
unsafe
{
// Get the pointer to the array variable.
IntPtr pointer = new IntPtr((void*) &array);

// Make the call.
img.GetPlane(pointer, out planeSize);

// At this point, array should have the first memory location
// of the item in the array.
byte* pByte = (byte*) array.ToPointer();

// Manipulate the array here.
}
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Beorne" <ma*******@gmail.comwrote in message
news:11*********************@57g2000hsv.googlegrou ps.com...
>I have imported a corporate image handling COM object in my C#
project.
To access in a fast way the memory of the image there is a method that
returns a pointer to the memory (in byte) of the underlying image.
This method can be used to read as well to rewrite the pixel values.
In the interface .idl file the method is declared as:

[helpstring("Get a pointer to the pixels of a plane")]
HRESULT GetPlane([out,size_is(,*dim)] BYTE ** pPlane,
[out,unique] long *dim);

C# imports the method as:
void GetPlane(intPtr pPlane, out int dim)

I haven't found a way to directly access (read and write) the memory
pointed by intPtw without the need of copying to managed memory.

An example of error:

int planeSize;
byte b;
int v;
unsafe
{
IntPtr pPlane = new IntPtr((void*)&b); // here I tried with pPlane =
new IntPtr.Zero but gave me NullReferenceException
img.GetPlane(pPlane, out planeSize);
byte* pByte = (byte*)pPlane.ToPointer();
for (int i = 0; i < planeSize; i++)
v = greylevels[*(pByte + i)]++; // access violation exception
}

Is it possible to do it? How could I do?
Thanks

Jul 30 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Matt Burland | last post by:
I am trying to read the memory being used by a process but I can't quite figure out how to do it (or if it's even possible). I can get a reference to the process using Process.GetProcessesByName...
6
by: redneon | last post by:
Is it possible to draw directly to the screen using the .net libraries? I thought that it might be possible with GDI+ but I can't find out how to do it anywhere. A part of me suspects I may have to...
3
by: Joergen Bech | last post by:
Am trying to paint a simple rectangle directly on the screen DC using the code below, but nothing happens?!? Weird. Anyone who can tell me what I am doing wrong? This ought to be...
1
by: pangel83 | last post by:
I've been trying for days to write a piece of VB.NET code that will read from winamp's memory space the paths of the files from the current winamp playlist. The GETPLAYLISTFILE command of the...
4
by: Martin | last post by:
Hello, I haven't found any answer on this - if it is possible or not. I have a PCI card with a memory on it. I have also a driver (written in C++) that maps the memory from the card to user...
2
by: =?Utf-8?B?SXJmYW4=?= | last post by:
Hello, It may be a repeated question but I don't find the solution to the situation that I encounter in it. My application is monitoring another application that is built in VB6. The...
3
by: Sune | last post by:
Hi all, I'm not a C# programmer so please be gentle: Prereq: --------------- - The lookup service (see below) implemented by a C module cannot be re-written in C# ;-) - The lookup service...
6
by: Scott Gravenhorst | last post by:
Windows XP SP3 My application is set to open a SaveFile dialog when an exit is requested. When I click the app's close button, the save dialog opens, but when I click to change the folder, the...
1
by: tk121 | last post by:
Hey all - I have some unmanaged code that I'm calling into with C# .NET 2.0, and everything works flawlessly in the debugger, but as soon as I run the application as a standalone executable it...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.