473,770 Members | 5,862 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(,*d im)] 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 NullReferenceEx ception
img.GetPlane(pP lane, out planeSize);
byte* pByte = (byte*)pPlane.T oPointer();
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 15739
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(po inter, 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.co m

"Beorne" <ma*******@gmai l.comwrote in message
news:11******** *************@5 7g2000hsv.googl egroups.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(,*d im)] 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 NullReferenceEx ception
img.GetPlane(pP lane, out planeSize);
byte* pByte = (byte*)pPlane.T oPointer();
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
13831
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 and I can get the base address using Process.MainModule.BaseAddress (which returns a IntPtr). I thought that by using IntPtr.ToPointer() and casting to a char* I would be able to read the memory as a stream of chars but it doesn't work because it...
6
41874
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 look into using the Windows API. Any ideas, Darrell
3
2662
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 straightforward, but ... :( TIA, Joergen Bech
1
2340
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 winamp API will only return a pointer to the position of the asked path. An article available on http://msmvps.com/ch21st/archive/2004/02/26.aspx provides a VB6 implementation of this, using the ReadProcessMemory Windows API command. Through...
4
2365
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 mode - I need very fast access to this memory, that's why I need to map it directly to my program.
2
5064
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 application monitors all the textboxes and other input & display controls on that application. The data from the textboxes and listboxes are retrived fine. But, when it comes to Listview there I got memory exception. I know that I need to do the in memory...
3
1791
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 is to be loaded into the process of the C# application, i.e. I want to avoid expensive IPC
6
4499
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 exception occurs pointing to FileSaveDialog1.ShowDialog(). The exception also indicates some problem with system.drawing.dll. The exception text is: "Attempted to read or write protected memory. This is often an
1
8106
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 unexpectedly quits without throwing any exceptions. Even if I start the executable in the debugger it works okay. To see the failure, I need to start the application and then attach the debugger to the process. The function call will work okay twice...
0
9591
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9425
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10225
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10053
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10001
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9867
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7415
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5449
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3573
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.