Click here to Skip to main content
15,891,748 members
Articles / Programming Languages / C#

Reading Unmanaged Data Into Structures

Rate me:
Please Sign up or sign in to vote.
4.93/5 (34 votes)
8 May 2008CPOL9 min read 196.2K   1.1K   58  
In this article, we will look into reading data from an unmanaged array of bytes into a managed data structure. We will use multiple approaches to optimize the process.

Microsoft Visual Studio Solution File, Format Version 10.00
# Visual Studio 2008
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReadingStructureData", "ReadingStructureData\ReadingStructureData.csproj", "{5717061C-7F83-4934-9882-AA1A91C84B15}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "GenericRead", "GenericRead\GenericRead.vcproj", "{98E082BB-79CC-4D02-9DC1-DE3FACF2440D}"
EndProject
Global
	GlobalSection(SolutionConfigurationPlatforms) = preSolution
		Debug|Any CPU = Debug|Any CPU
		Debug|Mixed Platforms = Debug|Mixed Platforms
		Debug|Win32 = Debug|Win32
		Debug|x64 = Debug|x64
		Release|Any CPU = Release|Any CPU
		Release|Mixed Platforms = Release|Mixed Platforms
		Release|Win32 = Release|Win32
		Release|x64 = Release|x64
	EndGlobalSection
	GlobalSection(ProjectConfigurationPlatforms) = postSolution
		{5717061C-7F83-4934-9882-AA1A91C84B15}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
		{5717061C-7F83-4934-9882-AA1A91C84B15}.Debug|Any CPU.Build.0 = Debug|Any CPU
		{5717061C-7F83-4934-9882-AA1A91C84B15}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
		{5717061C-7F83-4934-9882-AA1A91C84B15}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
		{5717061C-7F83-4934-9882-AA1A91C84B15}.Debug|Win32.ActiveCfg = Debug|Any CPU
		{5717061C-7F83-4934-9882-AA1A91C84B15}.Debug|x64.ActiveCfg = Debug|Any CPU
		{5717061C-7F83-4934-9882-AA1A91C84B15}.Release|Any CPU.ActiveCfg = Release|Any CPU
		{5717061C-7F83-4934-9882-AA1A91C84B15}.Release|Any CPU.Build.0 = Release|Any CPU
		{5717061C-7F83-4934-9882-AA1A91C84B15}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
		{5717061C-7F83-4934-9882-AA1A91C84B15}.Release|Mixed Platforms.Build.0 = Release|Any CPU
		{5717061C-7F83-4934-9882-AA1A91C84B15}.Release|Win32.ActiveCfg = Release|Any CPU
		{5717061C-7F83-4934-9882-AA1A91C84B15}.Release|x64.ActiveCfg = Release|Any CPU
		{5717061C-7F83-4934-9882-AA1A91C84B15}.Release|x64.Build.0 = Release|Any CPU
		{98E082BB-79CC-4D02-9DC1-DE3FACF2440D}.Debug|Any CPU.ActiveCfg = Debug|Win32
		{98E082BB-79CC-4D02-9DC1-DE3FACF2440D}.Debug|Mixed Platforms.ActiveCfg = Debug|Win32
		{98E082BB-79CC-4D02-9DC1-DE3FACF2440D}.Debug|Mixed Platforms.Build.0 = Debug|Win32
		{98E082BB-79CC-4D02-9DC1-DE3FACF2440D}.Debug|Win32.ActiveCfg = Debug|Win32
		{98E082BB-79CC-4D02-9DC1-DE3FACF2440D}.Debug|Win32.Build.0 = Debug|Win32
		{98E082BB-79CC-4D02-9DC1-DE3FACF2440D}.Debug|x64.ActiveCfg = Debug|x64
		{98E082BB-79CC-4D02-9DC1-DE3FACF2440D}.Debug|x64.Build.0 = Debug|x64
		{98E082BB-79CC-4D02-9DC1-DE3FACF2440D}.Release|Any CPU.ActiveCfg = Release|Win32
		{98E082BB-79CC-4D02-9DC1-DE3FACF2440D}.Release|Mixed Platforms.ActiveCfg = Release|Win32
		{98E082BB-79CC-4D02-9DC1-DE3FACF2440D}.Release|Mixed Platforms.Build.0 = Release|Win32
		{98E082BB-79CC-4D02-9DC1-DE3FACF2440D}.Release|Win32.ActiveCfg = Release|Win32
		{98E082BB-79CC-4D02-9DC1-DE3FACF2440D}.Release|Win32.Build.0 = Release|Win32
		{98E082BB-79CC-4D02-9DC1-DE3FACF2440D}.Release|x64.ActiveCfg = Release|x64
		{98E082BB-79CC-4D02-9DC1-DE3FACF2440D}.Release|x64.Build.0 = Release|x64
	EndGlobalSection
	GlobalSection(SolutionProperties) = preSolution
		HideSolutionNode = FALSE
	EndGlobalSection
EndGlobal

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer SELA Group
Israel Israel
Sasha Goldshtein is the CTO of SELA Group, an Israeli company specializing in training, consulting and outsourcing to local and international customers.

Sasha's work is divided across these three primary disciplines. He consults for clients on architecture, development, debugging and performance issues; he actively develops code using the latest bits of technology from Microsoft; and he conducts training classes on a variety of topics, from Windows Internals to .NET Performance.

You can read more about Sasha's work and his latest ventures at his blog: http://blogs.microsoft.co.il/blogs/sasha

Sasha writes from Jerusalem, Israel.

Comments and Discussions