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

Reading file contents to unmanaged memory block (IntPtr) from C#

Hi,

In my project I need to use VirtualAlloc (kernel32) to allocate memory to
ensure that data is 4K aligned. I need to fill the data block with file
contents. I notice that there are no 'Read' operations to memory pointed to
be an IntPtr (I checked FileStream & BinaryReader). Can I do this from
C#/.NET?

I'm currently mapping C-functions to achieve this, and I'm not too fond of
doing that. Can someone help me with this?

Thanks
--
Tom Tempelaere.
Nov 16 '05 #1
4 11338
One thing you can do is; use BinaryReader.Read to read the data into a
byte[], when done use Marshal.Copy to copy the byte[] to the native heap
pointed to by your IntPtr.

Willy.

"TT (Tom Tempelaere)" <_|\|_0$P@|/\|titi____AThotmail.com|/\|@P$0_|\|_>
wrote in message news:E3**********************************@microsof t.com...
Hi,

In my project I need to use VirtualAlloc (kernel32) to allocate memory to
ensure that data is 4K aligned. I need to fill the data block with file
contents. I notice that there are no 'Read' operations to memory pointed
to
be an IntPtr (I checked FileStream & BinaryReader). Can I do this from
C#/.NET?

I'm currently mapping C-functions to achieve this, and I'm not too fond of
doing that. Can someone help me with this?

Thanks
--
Tom Tempelaere.

Nov 16 '05 #2
Tom,

You can use the static ReadByte, ReadInt16, ReadInt32, ReadInt64 and
ReadIntPtr methods in order to read blocks of bytes from a location in
memory.

I also believe you can use the static Copy method to copy unmanaged
memory into an array of bytes.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"TT (Tom Tempelaere)" <_|\|_0$P@|/\|titi____AThotmail.com|/\|@P$0_|\|_>
wrote in message news:E3**********************************@microsof t.com...
Hi,

In my project I need to use VirtualAlloc (kernel32) to allocate memory to
ensure that data is 4K aligned. I need to fill the data block with file
contents. I notice that there are no 'Read' operations to memory pointed
to
be an IntPtr (I checked FileStream & BinaryReader). Can I do this from
C#/.NET?

I'm currently mapping C-functions to achieve this, and I'm not too fond of
doing that. Can someone help me with this?

Thanks
--
Tom Tempelaere.

Nov 16 '05 #3
Tom,

I would use unsafe code for this. Basically, I would allocate a byte
array. Then, I would enter an unsafe block and populate the byte array with
calls to CreateFile (to open) and ReadFile, etc, etc. Then, when you exit
the unsafe block, you should have the contents in a byte array. You can
always reverse the process to flush the contents back.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"TT (Tom Tempelaere)" <_|\|_0$P@|/\|titi____AThotmail.com|/\|@P$0_|\|_>
wrote in message news:E3**********************************@microsof t.com...
Hi,

In my project I need to use VirtualAlloc (kernel32) to allocate memory to
ensure that data is 4K aligned. I need to fill the data block with file
contents. I notice that there are no 'Read' operations to memory pointed
to
be an IntPtr (I checked FileStream & BinaryReader). Can I do this from
C#/.NET?

I'm currently mapping C-functions to achieve this, and I'm not too fond of
doing that. Can someone help me with this?

Thanks
--
Tom Tempelaere.

Nov 16 '05 #4
Tom,

If you need to read file data effeciently into native heap memory, you
shouldn't be using C# for this.
If you don't need the data to cross the native/managed heap boundary, you
should use native C++. If you want your data to cross this boundary you
should use MC++.

Willy.
"TT (Tom Tempelaere)" <_|\|_0$P@|/\|titi____AThotmail.com|/\|@P$0_|\|_>
wrote in message news:F2**********************************@microsof t.com...
Hi Willy,

The problem is efficiency if I would do it like that. The data is read
into
memory to start a DMA operation (hence the use of VirtualAlloc). I can't
read
the file contents to a managed array and then copy it to unmanaged memory,
that would be too unefficient.

Currently, I'm using fopen,fread,fclose,... from MSVCRT.dll to pull it
off.
It works but as I said, I'm not too fond of it.

Thank you,
Tom T.

"Willy Denoyette [MVP]" wrote:
One thing you can do is; use BinaryReader.Read to read the data into a
byte[], when done use Marshal.Copy to copy the byte[] to the native heap
pointed to by your IntPtr.

Willy.

"TT (Tom Tempelaere)" <_|\|_0$P@|/\|titi____AThotmail.com|/\|@P$0_|\|_>
wrote in message
news:E3**********************************@microsof t.com...
> Hi,
>
> In my project I need to use VirtualAlloc (kernel32) to allocate memory
> to
> ensure that data is 4K aligned. I need to fill the data block with file
> contents. I notice that there are no 'Read' operations to memory
> pointed
> to
> be an IntPtr (I checked FileStream & BinaryReader). Can I do this from
> C#/.NET?
>
> I'm currently mapping C-functions to achieve this, and I'm not too fond
> of
> doing that. Can someone help me with this?
>
> Thanks
> --
> Tom Tempelaere.


Nov 16 '05 #5

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

Similar topics

1
by: Eric Hendriks | last post by:
// In an unmanaged DLL the following function must be called: // int VFGeneralize(const BYTE * const * features); // "features" parameter is supposed to be an array of byte arrays. // function is...
10
by: cppdev | last post by:
Hi All! I want to clear the string contents from sensitive information such as passwords, and etc. It's always a case that password will appear as string at some point or another. And i feel...
1
by: Jón Sveinsson | last post by:
Hello everyone I have been able to read data from binary files to filestrean, the data in the files are structured, what I'm trying to do is to loop through the binary files and add data to my...
8
by: Don | last post by:
I have a third party C++ DLL that I am trying to use from C#. The specific function I am trying to use is declared in C++ as follows: ladybugConvertToMultipleBGRU32( LadybugContext ...
3
by: Nadav | last post by:
Hi, I have to read files generated by 3rd party application, these files are in a propriotary format ( not generated using the .NET framework ), I want to implement a custom DeSerializer to read...
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...
15
by: Sharon | last post by:
Hello gurus, I have a System.IntPtr pointing to the memory address of my image (not a ..NET image). I wish to save this image data pointed by the IntPtr to a raw data file. The way I now to...
1
by: Frustrated | last post by:
Since C# and VB.NET are similar, I am posting this here also I have a C++ DLL that I need to access from VB.NET or C#. The following is my attempt via VB.NET. The function I am trying to call...
4
by: Uriel88 | last post by:
Hello, I am working with developing an application that uses the Netmon 3.2 API. Currently they have a PInvoke wrapper to access unmanaged C++ DLL functions. Basically what I am attempting to do...
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: 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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
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
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,...
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...

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.