Here are the examples of the csharp api System.Runtime.InteropServices.Marshal.SizeOf(TStructure) taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
3 Examples
19
Source : Win32DeviceControl.cs
with MIT License
from meziantou
with MIT License
from meziantou
internal static byte[] ControlWithInput<TStructure>(ChangeJournalSafeHandle handle, Win32ControlCode code, ref TStructure structure, int bufferlen) where TStructure : struct
{
uint datalen;
bool controlResult;
GCHandle structureHandle;
GCHandle bufferHandle;
IntPtr structurePointer;
IntPtr bufferPointer;
var buffer = new byte[bufferlen];
bufferHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
structureHandle = GCHandle.Alloc(structure, GCHandleType.Pinned);
bufferPointer = bufferHandle.AddrOfPinnedObject();
structurePointer = structureHandle.AddrOfPinnedObject();
try
{
controlResult = Win32Methods.DeviceIoControl(handle, (uint)code, structurePointer, (uint)Marshal.SizeOf(structure), bufferPointer, (uint)buffer.Length, out datalen, IntPtr.Zero);
}
finally
{
structureHandle.Free();
bufferHandle.Free();
}
if (!controlResult)
throw new Win32Exception(Marshal.GetLastWin32Error());
if (datalen < bufferlen && datalen != 0)
{
var tempBuffer = new byte[datalen];
Array.Copy(buffer, 0, tempBuffer, 0, datalen);
buffer = tempBuffer;
}
return buffer;
}
19
Source : Win32DeviceControl.cs
with MIT License
from meziantou
with MIT License
from meziantou
internal static void ControlWithOutput<TStructure>(ChangeJournalSafeHandle handle, Win32ControlCode code, ref TStructure structure) where TStructure : struct
{
bool controlResult;
GCHandle structureHandle;
IntPtr structurePointer;
// get our object pointer
structureHandle = GCHandle.Alloc(structure, GCHandleType.Pinned);
structurePointer = structureHandle.AddrOfPinnedObject();
try
{
controlResult = Win32Methods.DeviceIoControl(handle, (uint)code, IntPtr.Zero, 0, structurePointer, (uint)Marshal.SizeOf(structure), out _, IntPtr.Zero);
}
finally
{
// always release GH handle
structureHandle.Free();
}
if (!controlResult)
throw new Win32Exception(Marshal.GetLastWin32Error());
structure = Marshal.PtrToStructure<TStructure>(structurePointer);
}
19
Source : ReadWrite.cs
with GNU General Public License v3.0
from Sewer56
with GNU General Public License v3.0
from Sewer56
[HandleProcessCorruptedStateExceptions]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte[] ConvertStructureToByteArray<TStructure>(ref TStructure structure)
{
// Retrieve size of structure and allocate buffer.
int structSize = Marshal.SizeOf(structure);
byte[] buffer = new byte[structSize];
// Allocate memory and marshal structure into it.
IntPtr structPointer = Marshal.AllocHGlobal(structSize);
Helpers.ThirdParty.FastStructure<TStructure>.StructureToPtr(ref structure, structPointer);
// Copy the structure into our buffer.
Marshal.Copy(structPointer, buffer, 0, structSize);
// Free allocated memory and return structure.
Marshal.FreeHGlobal(structPointer);
return buffer;
}