I created the type IntPtr<T> to act like a generic pointer that c/c++ have:
public struct IntPtr<T> : IDisposable where T : struct
{
private static Dictionary<IntPtr, GCHandle> handles = new Dictionary<IntPtr, GCHandle>();
private IntPtr ptr;
public IntPtr(ref T value)
{
GCHandle gc = GCHandle.Alloc(value, GCHandleType.Pinned);
ptr = gc.AddrOfPinnedObject();
handles.Add(ptr, gc);
}
public IntPtr(ref T[] value)
{
GCHandle gc = GCHandle.Alloc(value, GCHandleType.Pinned);
ptr = gc.AddrOfPinnedObject();
handles.Add(ptr, gc);
}
public IntPtr(IntPtr value)
{ ptr = value; }
public IntPtr(IntPtr<T> value)
{ ptr = value.ptr; }
public void Dispose()
{
if (handles.ContainsKey(ptr))
{
GCHandle gc = handles[ptr];
gc.Free();
handles.Remove(ptr);
ptr = IntPtr.Zero;
}
}
public T? this[int index]
{
get
{
if (ptr == IntPtr.Zero) return null;
if (index < 0) throw new IndexOutOfRangeException();
if (handles.ContainsKey(ptr))
{
GCHandle gc = handles[ptr];
if (gc.Target is Array) return ((T[])gc.Target)[index];
return (T)gc.Target;
}
return null;
}
set
{
if (index < 0) throw new IndexOutOfRangeException();
// not yet implemented
}
}
private T[] getArray()
{
if (handles.ContainsKey(ptr)) return (T[])handles[ptr].Target;
return null;
}
public int Count
{
get
{
if(handles.ContainsKey(ptr))
{
GCHandle gc = handles[ptr];
if (gc.Target is Array) return ((T[])gc.Target).Length;
return 1;
}
return 0;
}
}
public static implicit operator IntPtr(IntPtr<T> value) { return value.ptr; }
public static implicit operator T(IntPtr<T> value) { return (T)value[0]; }
public static implicit operator T[](IntPtr<T> value) { return value.getArray(); ; }
public static implicit operator T?(IntPtr<T> value) { return value[0]; }
}
It's not complete yet but for now it works, the problem is i keep track of GCHandle by storing them in handles now i need to free the GCHandle once it no more needed so i have to declare a destrcutor but c# don't allow struct to have destrcutor or to override 'Finalize' method and if the such variable of type IntPtr<T> goes out of scope the destruction take place but the GCHandle won't be free.
UPDATE
As an example of this class usage, suppose we going to interpo COAUTHIDENTITY and COAUTHINFO from COM, here what it will look like:
[StructLayout(LayoutKind.Sequential)]
struct COAUTHIDENTITY
{
[MarshalAs(UnmanagedType.LPWStr)] string User;
uint UserLength;
[MarshalAs(UnmanagedType.LPWStr)] string Domain;
uint DomainLength;
[MarshalAs(UnmanagedType.LPWStr)] string Password;
uint PasswordLength;
uint Flags;
}
[StructLayout(LayoutKind.Sequential)]
struct COAUTHINFO
{
uint dwAuthnSvc;
uint dwAuthzSvc;
[MarshalAs(UnmanagedType.LPWStr)] string pwszServerPrincName;
uint dwAuthnLevel;
uint dwImpersonationLevel;
IntPtr<COAUTHIDENTITY> pAuthIdentityData;
uint dwCapabilities;
}
Instead to make pAuthIdentityData an IntPtr and use Marshal member functions to get object of type COAUTHIDENTITY, IntPtr<T> will make it more simple.
The question is: where should i write the code to free GCHandle when the IntPtr<T> is released?
You're reinventing the wheel. Look at the SafeHandle class. Use an existing descendant or create your own descendant.
Related
I would like to write a tool in C# which can detect the presence of multiple CPU groups in Windows. I have seen this referred to as kGroups, and also as NUMA nodes.
This need has grown out of multiple performance related issues where we discovered the customer was running HP servers which often have their NUMA Group Size Optimization in the BIOS set to "Clustered" instead of "Flat" which can result in multiple CPU groups in Windows. Any one process, unless otherwise designed to operate across kGroups, will only be able to use logical processors within the kGroup the process affinity is set to.
I have found a number of resources that can detect information about number of physical/logical processors, but I'm unable to find information on if/how those CPU's might be logically grouped. I'm open to getting this info through p/invoke or WMI among other methods.
Edit: Found the following post with a full example of the GetLogicalProcessorInformationEx call via p/invoke. Will update when I can confirm how to test numa node configuration.
https://stackoverflow.com/a/6972620/3736007
references: http://h17007.www1.hpe.com/docs/iss/proliant_uefi/UEFI_Gen9_060216/s_set_NUMA_group.html
Solved (I think). I was able to use David Heffernan's c# implementation, wrapped it up in a class and added some properties to return some of the information I'm after. Unsafe code is still a bit of black magic to me so I know it could be done better, but it's working so far.
public static class LogicalProcessorInformation
{
public static int CpuPackages
{
get
{
if (_buffer == null)
_buffer = MyGetLogicalProcessorInformation();
return _buffer.Count(b => b.Relationship == LOGICAL_PROCESSOR_RELATIONSHIP.RelationProcessorPackage);
}
}
public static int CpuCores
{
get
{
if (_buffer == null)
_buffer = MyGetLogicalProcessorInformation();
return _buffer.Count(b => b.Relationship == LOGICAL_PROCESSOR_RELATIONSHIP.RelationProcessorCore);
}
}
public static int LogicalProcessors
{
get
{
if (_buffer == null)
_buffer = MyGetLogicalProcessorInformation();
var count = 0;
foreach (var obj in _buffer.Where(b => b.Relationship == LOGICAL_PROCESSOR_RELATIONSHIP.RelationProcessorCore))
{
count += CountSetBits(obj.ProcessorMask);
}
return count;
}
}
public static int CpuGroups
{
get
{
if (_buffer == null)
_buffer = MyGetLogicalProcessorInformation();
return _buffer.Count(b => b.Relationship == LOGICAL_PROCESSOR_RELATIONSHIP.RelationGroup);
}
}
public static int NumaNodes
{
get
{
if (_buffer == null)
_buffer = MyGetLogicalProcessorInformation();
return _buffer.Count(b => b.Relationship == LOGICAL_PROCESSOR_RELATIONSHIP.RelationNumaNode);
}
}
private static int CountSetBits(UIntPtr bitMask)
{
// todo: get rid of tostring and figure out the right way.
var bitMaskuint = uint.Parse(bitMask.ToString());
uint count = 0;
while (bitMaskuint != 0)
{
count += bitMaskuint & 1;
bitMaskuint >>= 1;
}
return (int)count;
}
private static SYSTEM_LOGICAL_PROCESSOR_INFORMATION[] _buffer;
[StructLayout(LayoutKind.Sequential)]
private struct PROCESSORCORE
{
public byte Flags;
};
[StructLayout(LayoutKind.Sequential)]
private struct NUMANODE
{
public uint NodeNumber;
}
private enum PROCESSOR_CACHE_TYPE
{
CacheUnified,
CacheInstruction,
CacheData,
CacheTrace
}
[StructLayout(LayoutKind.Sequential)]
private struct CACHE_DESCRIPTOR
{
public byte Level;
public byte Associativity;
public ushort LineSize;
public uint Size;
public PROCESSOR_CACHE_TYPE Type;
}
[StructLayout(LayoutKind.Explicit)]
private struct SYSTEM_LOGICAL_PROCESSOR_INFORMATION_UNION
{
[FieldOffset(0)]
public PROCESSORCORE ProcessorCore;
[FieldOffset(0)]
public NUMANODE NumaNode;
[FieldOffset(0)]
public CACHE_DESCRIPTOR Cache;
[FieldOffset(0)]
private UInt64 Reserved1;
[FieldOffset(8)]
private UInt64 Reserved2;
}
private enum LOGICAL_PROCESSOR_RELATIONSHIP
{
RelationProcessorCore,
RelationNumaNode,
RelationCache,
RelationProcessorPackage,
RelationGroup,
RelationAll = 0xffff
}
private struct SYSTEM_LOGICAL_PROCESSOR_INFORMATION
{
public UIntPtr ProcessorMask;
public LOGICAL_PROCESSOR_RELATIONSHIP Relationship;
public SYSTEM_LOGICAL_PROCESSOR_INFORMATION_UNION ProcessorInformation;
}
[DllImport(#"kernel32.dll", SetLastError = true)]
private static extern bool GetLogicalProcessorInformation(
IntPtr Buffer,
ref uint ReturnLength
);
private const int ERROR_INSUFFICIENT_BUFFER = 122;
private static SYSTEM_LOGICAL_PROCESSOR_INFORMATION[] MyGetLogicalProcessorInformation()
{
uint ReturnLength = 0;
GetLogicalProcessorInformation(IntPtr.Zero, ref ReturnLength);
if (Marshal.GetLastWin32Error() == ERROR_INSUFFICIENT_BUFFER)
{
IntPtr Ptr = Marshal.AllocHGlobal((int)ReturnLength);
try
{
if (GetLogicalProcessorInformation(Ptr, ref ReturnLength))
{
int size = Marshal.SizeOf(typeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION));
int len = (int)ReturnLength / size;
SYSTEM_LOGICAL_PROCESSOR_INFORMATION[] Buffer = new SYSTEM_LOGICAL_PROCESSOR_INFORMATION[len];
IntPtr Item = Ptr;
for (int i = 0; i < len; i++)
{
Buffer[i] = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION)Marshal.PtrToStructure(Item, typeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION));
Item += size;
}
return Buffer;
}
}
finally
{
Marshal.FreeHGlobal(Ptr);
}
}
return null;
}
}
Best way to describe the problem I'm trying to solve is to talk in code. I see a lot of __arglist questions on this forum, but not a lot of helpful answers. I know _arglist should be avoided so I'm open to alternative methods
In one C++ module I have something like the following
void SomeFunction(LPCWSTR pszFormat, va_args args)
{
// this function is not exported...
// it is designed to take a format specifier and a list of variable
// arguments and "printf" it into a buffer. This code
// allocates buffer and uses _vsnwprintf_s to format the
// string.
// I really do not have much flexibility to rewrite this function
// so please steer away from scrutinizing this. it is what is
// and I need to call it from C#.
::_vsnwprintf_s(somebuff, buffsize, _TRUNCATE, pszFormat, args)
}
__declspec(dllexport) void __cdecl ExportedSomeFunction(LPCWSTR pszFormat, ...)
{
// the purpose of this method is to export SomeFunction to C# code.
// it handles any marshaling. I can change this however it makes sense
va_list args ;
va_start(args, pszFormat) ;
SomeFunction(pszFormat, args) ;
va_end(args) ;
}
in another C# module I have code that handles all marshalling to the C++ DLL.
The intent is to hide all complexity of Native APIs and marshalling from user code.
The ultimate goal being a C++ developer or C# developer make the SAME API calls, but the code is written once and exported to both
[DllImport("mymodule.dll", CallingConvention=CallingConvention.Cdecl)]
private static extern void ExportedSomeFunction(
[MarshalAs(UnmanagedType.LPWStr)] out string strPath,
/* ? what goes here ? */);
void SomeFunction(string strFormat, /*? what goes here ?*/ )
{
// handles marshalling incoming data to what ever is needed by exported C function
ExportedSomeFunction(strFormat, /*? something ?*/ ) ;
}
Then the user code in some other module should look like this...
SomeFunction("my format: %ld, %s", 5, "Some Useless string") ;
That would be ideal, but am prepared to live with
SomeFunction("my format: %ld, %s", __arglist(5, "Some Useless string")) ;
I don't care how the data gets marshaled. If I use __arglist or some array, I don't care as long as I end up with a va_args
__arglist looks like the solution, and I can successfully call
ExportedSomeFunction(strFormat, __arglist(5, "Some Useless string")) ;
But I cannot figure out how to call the C# SomeFunction with variable arguments and pass a __arglist to the exported function.
SomeFunction("my format: %ld, %s", __arglist(5, "Some Useless string")) ;
I cannot get this to work....
[DllImport("mymodule.dll", CallingConvention=CallingConvention.Cdecl)]
private static extern void ExportedSomeFunction(
[MarshalAs(UnmanagedType.LPWStr)] out string strPath,
__arglist);
void SomeFunction(string strFormat, __arglist )
{
ExportedSomeFunction(strFormat, __arglist) ; // error cannot convert from RuntimeArgumentHandle to __arglist
}
This compiles, but doesn't produce the desired results. The argument list received in C++ is wrong.
private static extern void ExportedSomeFunction(
[MarshalAs(UnmanagedType.LPWStr)] out string strPath,
RuntimeArgumentHandle args);
Here is my suggestion how to tackle this. Take a look at varargs.h which is part of VisualStudio. This gives you some insight what the va_list means. You can see: typedef char * va_list;. It's just a pointer.
Not only is __arglist undocumented, I don't think it works correctly on 64-bit processes.
You need to build the va_list dynamically on C# side. I believe that this is better solution than undocumented __arglist and it seems to be working nicely. For C#, you want to use params[], and on C++ receiving side, va_list. Every variadic function should have function starting with v..., such as vsprintf, that receives va_list, instead of fiddling with arguments in the stack.
Copy/paste this beauty to your solution:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
// Author: Chris Eelmaa
namespace ConsoleApplication1
{
#region VariableCombiner
class CombinedVariables : IDisposable
{
readonly IntPtr _ptr;
readonly IList<IDisposable> _disposables;
bool _disposed;
public CombinedVariables(VariableArgument[] args)
{
_disposables = new List<IDisposable>();
_ptr = Marshal.AllocHGlobal(args.Sum(arg => arg.GetSize()));
var curPtr = _ptr;
foreach (var arg in args)
{
_disposables.Add(arg.Write(curPtr));
curPtr += arg.GetSize();
}
}
public IntPtr GetPtr()
{
if(_disposed)
throw new InvalidOperationException("Disposed already.");
return _ptr;
}
public void Dispose()
{
if (!_disposed)
{
_disposed = true;
foreach (var disposable in _disposables)
disposable.Dispose();
Marshal.FreeHGlobal(_ptr);
}
}
}
#endregion
#region VariableArgument
abstract class VariableArgument
{
#region SentinelDispose
protected static readonly IDisposable SentinelDisposable =
new SentinelDispose();
class SentinelDispose : IDisposable
{
public void Dispose()
{
}
}
#endregion
public abstract IDisposable Write(IntPtr buffer);
public virtual int GetSize()
{
return IntPtr.Size;
}
public static implicit operator VariableArgument(int input)
{
return new VariableIntegerArgument(input);
}
public static implicit operator VariableArgument(string input)
{
return new VariableStringArgument(input);
}
public static implicit operator VariableArgument(double input)
{
return new VariableDoubleArgument(input);
}
}
#endregion
#region VariableIntegerArgument
sealed class VariableIntegerArgument : VariableArgument
{
readonly int _value;
public VariableIntegerArgument(int value)
{
_value = value;
}
public override IDisposable Write(IntPtr buffer)
{
Marshal.Copy(new[] { _value }, 0, buffer, 1);
return SentinelDisposable;
}
}
#endregion
#region VariableDoubleArgument
sealed class VariableDoubleArgument : VariableArgument
{
readonly double _value;
public VariableDoubleArgument(double value)
{
_value = value;
}
public override int GetSize()
{
return 8;
}
public override IDisposable Write(IntPtr buffer)
{
Marshal.Copy(new[] { _value }, 0, buffer, 1);
return SentinelDisposable;
}
}
#endregion
#region VariableStringArgument
sealed class VariableStringArgument : VariableArgument
{
readonly string _value;
public VariableStringArgument(string value)
{
_value = value;
}
public override IDisposable Write(IntPtr buffer)
{
var ptr = Marshal.StringToHGlobalAnsi(_value);
Marshal.Copy(new[] {ptr}, 0, buffer, 1);
return new StringArgumentDisposable(ptr);
}
#region StringArgumentDisposable
class StringArgumentDisposable : IDisposable
{
IntPtr _ptr;
public StringArgumentDisposable(IntPtr ptr)
{
_ptr = ptr;
}
public void Dispose()
{
if (_ptr != IntPtr.Zero)
{
Marshal.FreeHGlobal(_ptr);
_ptr = IntPtr.Zero;
}
}
}
#endregion
}
#endregion
}
and the example of usage:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(
AmazingSPrintf("I am %s, %d years old, %f meters tall!",
"Chris",
24,
1.94));
}
static string AmazingSPrintf(string format, params VariableArgument[] args)
{
if (!args.Any())
return format;
using (var combinedVariables = new CombinedVariables(args))
{
var bufferCapacity = _vscprintf(format, combinedVariables.GetPtr());
var stringBuilder = new StringBuilder(bufferCapacity + 1);
vsprintf(stringBuilder, format, combinedVariables.GetPtr());
return stringBuilder.ToString();
}
}
[DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)]
static extern int vsprintf(
StringBuilder buffer,
string format,
IntPtr ptr);
[DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)]
static extern int _vscprintf(
string format,
IntPtr ptr);
}
}
The CombinedVariables class is used to build va_list, and then you can pass it to your C++ method void SomeFunction(LPCWSTR pszFormat, va_list args).
You need to take care of the VariableStringArgument as it works with ANSI currently. You're probably looking for Marshal.StringToHGlobalUni.
Note that there is a small difference between va_list and .... printf uses ..., while vprintf uses va_list. A va_list is often a pointer to the first element of the .... __arglist is for ....
For va_list you can use the code of #Erti, or my code:
public class VaList : IDisposable
{
protected readonly List<GCHandle> handles = new List<GCHandle>();
public VaList(bool unicode, params object[] args)
{
if (args == null)
{
throw new ArgumentNullException("args");
}
// The first handle is for the bytes array
handles.Add(default(GCHandle));
int total = 0;
var bytes = new PrimitiveToBytes[args.Length];
for (int i = 0; i < args.Length; i++)
{
int size = Convert(unicode, args[i], ref bytes[i]);
bytes[i].Size = size;
total += size;
}
// Instead of a byte[] we use a IntPtr[], so copying elements
// inside is faster (perhaps :-) )
var buffer = new IntPtr[total / IntPtr.Size];
handles[0] = GCHandle.Alloc(buffer, GCHandleType.Pinned);
for (int i = 0, j = 0; i < args.Length; i++)
{
buffer[j++] = bytes[i].IntPtr;
// long or double with IntPtr == 4
if (bytes[i].Size > IntPtr.Size)
{
buffer[j++] = (IntPtr)bytes[i].Int32High;
}
}
}
// Overload this to handle other types
protected virtual int Convert(bool unicode, object arg, ref PrimitiveToBytes primitiveToBytes)
{
int size;
if (arg == null)
{
primitiveToBytes.IntPtr = IntPtr.Zero;
size = IntPtr.Size;
}
else
{
Type type = arg.GetType();
TypeCode typeHandle = Type.GetTypeCode(type);
switch (typeHandle)
{
case TypeCode.Boolean:
// Boolean converted to Int32
primitiveToBytes.Int32 = (bool)arg ? 1 : 0;
size = IntPtr.Size;
break;
case TypeCode.SByte:
primitiveToBytes.SByte = (sbyte)arg;
size = IntPtr.Size;
break;
case TypeCode.Byte:
primitiveToBytes.Byte = (byte)arg;
size = IntPtr.Size;
break;
case TypeCode.Int16:
primitiveToBytes.Int16 = (short)arg;
size = IntPtr.Size;
break;
case TypeCode.UInt16:
primitiveToBytes.UInt16 = (ushort)arg;
size = IntPtr.Size;
break;
case TypeCode.Int32:
primitiveToBytes.Int32 = (int)arg;
size = IntPtr.Size;
break;
case TypeCode.UInt32:
primitiveToBytes.UInt32 = (uint)arg;
size = IntPtr.Size;
break;
case TypeCode.Int64:
primitiveToBytes.Int64 = (long)arg;
size = sizeof(long);
break;
case TypeCode.UInt64:
primitiveToBytes.UInt64 = (ulong)arg;
size = sizeof(ulong);
break;
case TypeCode.Single:
// Single converted to Double
primitiveToBytes.Double = (double)(float)arg;
size = sizeof(double);
break;
case TypeCode.Double:
primitiveToBytes.Double = (double)arg;
size = sizeof(double);
break;
case TypeCode.Char:
if (unicode)
{
primitiveToBytes.UInt16 = (char)arg;
}
else
{
byte[] bytes = Encoding.Default.GetBytes(new[] { (char)arg });
if (bytes.Length > 0)
{
primitiveToBytes.B0 = bytes[0];
if (bytes.Length > 1)
{
primitiveToBytes.B1 = bytes[1];
if (bytes.Length > 2)
{
primitiveToBytes.B2 = bytes[2];
if (bytes.Length > 3)
{
primitiveToBytes.B3 = bytes[3];
}
}
}
}
}
size = IntPtr.Size;
break;
case TypeCode.String:
{
string str = (string)arg;
GCHandle handle;
if (unicode)
{
handle = GCHandle.Alloc(str, GCHandleType.Pinned);
}
else
{
byte[] bytes = new byte[Encoding.Default.GetByteCount(str) + 1];
Encoding.Default.GetBytes(str, 0, str.Length, bytes, 0);
handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
}
handles.Add(handle);
primitiveToBytes.IntPtr = handle.AddrOfPinnedObject();
size = IntPtr.Size;
}
break;
case TypeCode.Object:
if (type == typeof(IntPtr))
{
primitiveToBytes.IntPtr = (IntPtr)arg;
size = IntPtr.Size;
}
else if (type == typeof(UIntPtr))
{
primitiveToBytes.UIntPtr = (UIntPtr)arg;
size = UIntPtr.Size;
}
else if (!type.IsValueType)
{
GCHandle handle = GCHandle.Alloc(arg, GCHandleType.Pinned);
primitiveToBytes.IntPtr = handle.AddrOfPinnedObject();
size = IntPtr.Size;
}
else
{
throw new NotSupportedException();
}
break;
default:
throw new NotSupportedException();
}
}
return size;
}
~VaList()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
}
protected virtual void Dispose(bool disposing)
{
for (int i = 0; i < handles.Count; i++)
{
if (handles[i].IsAllocated)
{
handles[i].Free();
}
}
handles.Clear();
}
public IntPtr AddrOfPinnedObject()
{
if (handles.Count == 0)
{
throw new ObjectDisposedException(GetType().Name);
}
return handles[0].AddrOfPinnedObject();
}
[StructLayout(LayoutKind.Explicit)]
protected struct PrimitiveToBytes
{
[FieldOffset(0)]
public byte B0;
[FieldOffset(1)]
public byte B1;
[FieldOffset(2)]
public byte B2;
[FieldOffset(3)]
public byte B3;
[FieldOffset(4)]
public byte B4;
[FieldOffset(5)]
public byte B5;
[FieldOffset(6)]
public byte B6;
[FieldOffset(7)]
public byte B7;
[FieldOffset(4)]
public int Int32High;
[FieldOffset(0)]
public byte Byte;
[FieldOffset(0)]
public sbyte SByte;
[FieldOffset(0)]
public short Int16;
[FieldOffset(0)]
public ushort UInt16;
[FieldOffset(0)]
public int Int32;
[FieldOffset(0)]
public uint UInt32;
[FieldOffset(0)]
public long Int64;
[FieldOffset(0)]
public ulong UInt64;
[FieldOffset(0)]
public float Single;
[FieldOffset(0)]
public double Double;
[FieldOffset(0)]
public IntPtr IntPtr;
[FieldOffset(0)]
public UIntPtr UIntPtr;
[FieldOffset(8)]
public int Size;
}
}
Example of use:
[DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)]
static extern int vprintf(string format, IntPtr ptr);
and
using (var list = new VaList(false, // Ansi encoding
true, // bool test
short.MinValue + 1, int.MinValue + 2, long.MinValue + 3, // signed
ushort.MaxValue - 4, uint.MaxValue - 5, ulong.MaxValue - 6, // unsigned
float.MaxValue, double.MaxValue, // float/double
'A', "Foo", Encoding.Default.GetBytes("Bar\0"), null, // char/string
IntPtr.Size == sizeof(int) ? (IntPtr)(int.MinValue + 7) : (IntPtr)(long.MinValue + 7), // signed ptr
UIntPtr.Size == sizeof(uint) ? (UIntPtr)(uint.MaxValue - 8) : (UIntPtr)(ulong.MaxValue - 8))) // unsigned ptr
{
vprintf("%d\n %hd\n %d\n %lld\n %hu\n %u\n %llu\n %f\n %f\n %c\n %s\n %s\n %s\n %p\n %p\n", list.AddrOfPinnedObject());
}
Note that this code is compatible only with Visual C++ for Intel x86/x64. ARM uses another format, and GCC still other formats.
Suppose I have the following code:
IntPtr newPtr = new IntPtr( oldPtr.ToInt32() + 12 );
Is there any way to make the process of increasing oldPtr more streamlined?
Anything I'm missing or I should know?
edited to clarify: my curiosity was in easing the process itself, I'm just trying to get in the world of unmanaged and it's difficult enough without the need to cast everything.
Since Framework 4.0 you can use Add() and Substract() methods.
If you are developing under an older environment, implement this in your project:
public static class IntPtrExtensions
{
#region Methods: Arithmetics
public static IntPtr Decrement(this IntPtr pointer, Int32 value)
{
return Increment(pointer, -value);
}
public static IntPtr Decrement(this IntPtr pointer, Int64 value)
{
return Increment(pointer, -value);
}
public static IntPtr Decrement(this IntPtr pointer, IntPtr value)
{
switch (IntPtr.Size)
{
case sizeof(Int32):
return (new IntPtr(pointer.ToInt32() - value.ToInt32()));
default:
return (new IntPtr(pointer.ToInt64() - value.ToInt64()));
}
}
public static IntPtr Increment(this IntPtr pointer, Int32 value)
{
unchecked
{
switch (IntPtr.Size)
{
case sizeof(Int32):
return (new IntPtr(pointer.ToInt32() + value));
default:
return (new IntPtr(pointer.ToInt64() + value));
}
}
}
public static IntPtr Increment(this IntPtr pointer, Int64 value)
{
unchecked
{
switch (IntPtr.Size)
{
case sizeof(Int32):
return (new IntPtr((Int32)(pointer.ToInt32() + value)));
default:
return (new IntPtr(pointer.ToInt64() + value));
}
}
}
public static IntPtr Increment(this IntPtr pointer, IntPtr value)
{
unchecked
{
switch (IntPtr.Size)
{
case sizeof(Int32):
return new IntPtr(pointer.ToInt32() + value.ToInt32());
default:
return new IntPtr(pointer.ToInt64() + value.ToInt64());
}
}
}
#endregion
#region Methods: Comparison
public static Int32 CompareTo(this IntPtr left, Int32 right)
{
return left.CompareTo((UInt32)right);
}
public static Int32 CompareTo(this IntPtr left, IntPtr right)
{
if (left.ToUInt64() > right.ToUInt64())
return 1;
if (left.ToUInt64() < right.ToUInt64())
return -1;
return 0;
}
public static Int32 CompareTo(this IntPtr left, UInt32 right)
{
if (left.ToUInt64() > right)
return 1;
if (left.ToUInt64() < right)
return -1;
return 0;
}
#endregion
#region Methods: Conversion
public unsafe static UInt32 ToUInt32(this IntPtr pointer)
{
return (UInt32)((void*)pointer);
}
public unsafe static UInt64 ToUInt64(this IntPtr pointer)
{
return (UInt64)((void*)pointer);
}
#endregion
#region Methods: Equality
public static Boolean Equals(this IntPtr pointer, Int32 value)
{
return (pointer.ToInt32() == value);
}
public static Boolean Equals(this IntPtr pointer, Int64 value)
{
return (pointer.ToInt64() == value);
}
public static Boolean Equals(this IntPtr left, IntPtr ptr2)
{
return (left == ptr2);
}
public static Boolean Equals(this IntPtr pointer, UInt32 value)
{
return (pointer.ToUInt32() == value);
}
public static Boolean Equals(this IntPtr pointer, UInt64 value)
{
return (pointer.ToUInt64() == value);
}
public static Boolean GreaterThanOrEqualTo(this IntPtr left, IntPtr right)
{
return (left.CompareTo(right) >= 0);
}
public static Boolean LessThanOrEqualTo(this IntPtr left, IntPtr right)
{
return (left.CompareTo(right) <= 0);
}
#endregion
#region Methods: Logic
public static IntPtr And(this IntPtr pointer, IntPtr value)
{
switch (IntPtr.Size)
{
case sizeof(Int32):
return (new IntPtr(pointer.ToInt32() & value.ToInt32()));
default:
return (new IntPtr(pointer.ToInt64() & value.ToInt64()));
}
}
public static IntPtr Not(this IntPtr pointer)
{
switch (IntPtr.Size)
{
case sizeof(Int32):
return (new IntPtr(~pointer.ToInt32()));
default:
return (new IntPtr(~pointer.ToInt64()));
}
}
public static IntPtr Or(this IntPtr pointer, IntPtr value)
{
switch (IntPtr.Size)
{
case sizeof(Int32):
return (new IntPtr(pointer.ToInt32() | value.ToInt32()));
default:
return (new IntPtr(pointer.ToInt64() | value.ToInt64()));
}
}
public static IntPtr Xor(this IntPtr pointer, IntPtr value)
{
switch (IntPtr.Size)
{
case sizeof(Int32):
return (new IntPtr(pointer.ToInt32() ^ value.ToInt32()));
default:
return (new IntPtr(pointer.ToInt64() ^ value.ToInt64()));
}
}
#endregion
}
And, in your example, call:
IntPtr newPtr = oldPtr.Increment(12);
I am using shared memory for inter-process communication in an unsafe class.
Part of the memory is reserved to hold a fixed array of int.
Basically, I have a method that sets up the shared memory. Something like this:
private int* sizePtr;
private ???* arrayPtr;
void SetupMemory(byte *pointerToSharedMem)
{
this.sizePtr = (int*)pointerToSharedMem;
pointerToSharedMem += sizeof(int);
this.arrayPtr = (???*)pointerToSharedMem;
pointerToSharedMem += sizeof(int) * FixedSizeOfArray;
}
How do I need to declare the pointer such that I can use a property
public int[] MyArray
{
get
{
return some magic with this.arrayPtr;
}
}
ETA:
If possible, I would like to avoid structs and I definitely want to avoid copying data around. I was hoping for some kind of cast construct to use a pointer to the data in shared memory, such that the data can be used immediately (i.e. without copying).
Actually, I can think of another answer.
This may very well get ugly if you don't use it just right, though.
Be careful!
public unsafe class UnsafeArray
{
private readonly int* _start;
public readonly int Length;
public UnsafeArray(int* start, int enforceLength = 0)
{
this._start = start;
this.Length = enforceLength > 0 ? enforceLength : int.MaxValue;
}
public int this[int index]
{
get { return _start[index]; }
set
{
if (index >= this.Length)
{
throw new IndexOutOfRangeException();
}
_start[index] = value;
}
}
}
Does it need to be a pointer, or can you copy the data over?
If that's okay, then check out this link
http://msdn.microsoft.com/en-us/library/aa330463(v=vs.71).aspx
In C# 2.0 and above, a struct can be declared with an embedded array, in an unsafe context:
namespace FixedSizeBuffers
{
internal unsafe struct MyBuffer
{
public fixed int fixedBuffer[128];
}
internal unsafe class MyClass
{
public MyBuffer myBuffer = default(MyBuffer);
}
internal class Program
{
static void Main()
{
MyClass myClass = new MyClass();
unsafe
{
// Pin the buffer to a fixed location in memory.
fixed (int* intPtr = myClass.myBuffer.fixedBuffer)
{
*intPtr = someIntValue;
}
}
}
}
}
http://msdn.microsoft.com/en-us/library/zycewsya(v=vs.100).aspx
Can't think of anything better than memcpy.
[DllImport("msvcrt.dll", EntryPoint = "memcpy", CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
public static extern IntPtr memcpy(IntPtr dest, IntPtr src, UIntPtr count);
private static unsafe int[] GetArray(int* ptr, uint length)
{
var ints = new int[length];
fixed (int* pInts = ints)
{
memcpy(new IntPtr(pInts), new IntPtr(ptr), new UIntPtr(length));
}
return ints;
}
I would like to do the following:
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct SomeStruct
{
public byte SomeByte;
public int SomeInt;
public short SomeShort;
public byte SomeByte2;
}
Is there an alternative since Pack is not supported in the compact framework?
Update: Explicitly setting up the structure and giving FieldOffset for each does not work either as it does not affect how the struct is packed
Update2: If you try the following, the CF program wont even run because of how the structure is packed:
[StructLayout(LayoutKind.Explicit, Size=8)]
public struct SomeStruct
{
[FieldOffset(0)]
public byte SomeByte;
[FieldOffset(1)]
public int SomeInt;
[FieldOffset(5)]
public short SomeShort;
[FieldOffset(7)]
public byte SomeByte2;
}
I know it seems hard to believe, but if you try it you will see. Add it to a CF project and try to run it and you will get a TypeLoadException. Changing the offsets to 0,4,8,10 respectively and it will work (but the size ends up being 12).
I was hoping maybe someone had a solution using reflection maybe to marshal the size of each of the field types individually (something involving recursion to handle structs within structs or arrays of types).
This probably isn't the type of answer you're looking for, but I'll post it anyway for the hell of it:
public struct SomeStruct
{
public byte SomeByte;
public int SomeInt;
public short SomeShort;
public byte SomeByte2;
public byte[] APIStruct
{
get
{
byte[] output = new byte[8];
output[0] = this.SomeByte;
Array.Copy(BitConverter.GetBytes(this.SomeInt), 0,
output, 1, 4);
Array.Copy(BitConverter.GetBytes(this.SomeShort), 0,
output, 5, 2);
output[7] = this.SomeByte2;
return output;
}
set
{
byte[] input = value;
this.SomeByte = input[0];
this.SomeInt = BitConverter.ToInt32(input, 1);
this.SomeShort = BitConverter.ToInt16(input, 5);
this.SomeByte2 = input[7];
}
}
}
Basically it does the packing/unpacking itself in the APIStruct property.
The simplest method of dealing with this type of problem is along the same lines as you might for a bit field, simply pack your data into a private member (or members if it is large) of the appropriate data type and then present public properties that unpack the data for you. The unpacking operations are extremely fast and will have little impact on performance. For your particular type the following is probably what you want:
public struct SomeStruct
{
private long data;
public byte SomeByte { get { return (byte)(data & 0x0FF); } }
public int SomeInt { get { return (int)((data & 0xFFFFFFFF00) << 8); } }
public short SomeShort { get { return (short)((data & 0xFFFF0000000000) << 40); } }
public byte SomeByte2 { get { return (byte)((data & unchecked((long)0xFF00000000000000)) << 56); } }
}
For some structures even this method is not workable due to the unfortunate way a structure has been defined. In those cases you will generally have to use a byte array as a blob of data from which the elements can be unpacked.
EDIT: To expand on what I mean about structs that can't be handled using this simple method. When you can't do simple packing/unpacking like this you need to manually marshal the irregular struct . This can be done using manual methods at the point you call the pInvoked API or by using a custom marshaler. The following is an example of a custom marhsaler that can be easily adapted to on the spot manual marshaling.
using System.Runtime.InteropServices;
using System.Threading;
public class Sample
{
[DllImport("sample.dll")]
public static extern void TestDataMethod([MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(TestDataMarshaler))] TestDataStruct pData);
}
public class TestDataStruct
{
public byte data1;
public int data2;
public byte[] data3 = new byte[7];
public long data4;
public byte data5;
}
public class TestDataMarshaler : ICustomMarshaler
{
//thread static since this could be called on
//multiple threads at the same time.
[ThreadStatic()]
private static TestDataStruct m_MarshaledInstance;
private static ICustomMarshaler m_Instance = new TestDataMarshaler();
public static ICustomFormatter GetInstance(string cookie)
{
return m_Instance;
}
#region ICustomMarshaler Members
public void CleanUpManagedData(object ManagedObj)
{
//nothing to do.
}
public void CleanUpNativeData(IntPtr pNativeData)
{
Marshal.FreeHGlobal(pNativeData);
}
public int GetNativeDataSize()
{
return 21;
}
public IntPtr MarshalManagedToNative(object ManagedObj)
{
m_MarshaledInstance = (TestDataStruct)ManagedObj;
IntPtr nativeData = Marshal.AllocHGlobal(GetNativeDataSize());
if (m_MarshaledInstance != null)
{
unsafe //unsafe is simpler but can easily be done without unsafe if necessary
{
byte* pData = (byte*)nativeData;
*pData = m_MarshaledInstance.data1;
*(int*)(pData + 1) = m_MarshaledInstance.data2;
Marshal.Copy(m_MarshaledInstance.data3, 0, (IntPtr)(pData + 5), 7);
*(long*)(pData + 12) = m_MarshaledInstance.data4;
*(pData + 20) = m_MarshaledInstance.data5;
}
}
return nativeData;
}
public object MarshalNativeToManaged(IntPtr pNativeData)
{
TestDataStruct data = m_MarshaledInstance;
m_MarshaledInstance = null; //clear out TLS for next call.
if (data == null) data = new TestDataStruct(); //if no in object then return a new one
unsafe //unsafe is simpler but can easily be done without unsafe if necessary
{
byte* pData = (byte*)pNativeData;
data.data1 = *pData;
data.data2 = *(int*)(pData + 1);
Marshal.Copy((IntPtr)(pData + 5), data.data3, 0, 7);
data.data4 = *(long*)(pData + 12);
data.data5 = *(pData + 20);
}
return data;
}
#endregion
}
In the case of arrays of these structs you can't use custom marshaling unless the array size is fixed but it is relatively easy to manually marshal the array data as a whole using the same techniques.
Do you absolutely require that specific layout or is it acceptable to simply make the size 8?
I ask this because the lay out as follows
[StructLayout(LayoutKind.Explicit, Size=8)]
public struct SomeStruct
{
[FieldOffset(0)]
public byte SomeByte;
[FieldOffset(1)]
public int SomeInt;
[FieldOffset(5)]
public short SomeShort;
[FieldOffset(7)]
public byte SomeByte2;
}
Has non word aligned fields which may be what is causing your problem.
If you can 'rearrange' things then this might work for you:
[StructLayout(LayoutKind.Explicit, Size=8)]
public struct SomeStruct
{
[FieldOffset(0)]
public byte SomeByte;
[FieldOffset(1)]
public byte SomeByte2;
[FieldOffset(2)]
public short SomeShort;
[FieldOffset(4)]
public int SomeInt;
}
When I test with this on the emulator it works fine.
Obviously unless you are willing to allow the rearrangement there's nothing you can do.
This answer and this old article would strongly indicate that you must at a minimum align your structs on multiples of their size (I tried with an int aligned on offset 2 and this also triggered the error)
Given your need to interoperate with externally defined data then the following is likely your easiest solution:
[StructLayout(LayoutKind.Explicit, Size=8)]
public struct SomeStruct
{
[FieldOffset(0)] private byte b0;
[FieldOffset(1)] private byte b1;
[FieldOffset(2)] private byte b2;
[FieldOffset(3)] private byte b3;
[FieldOffset(4)] private byte b4;
[FieldOffset(5)] private byte b5;
[FieldOffset(6)] private byte b6;
[FieldOffset(7)] private byte b7;
// not thread safe - alter accordingly if that is a requirement
private readonly static byte[] scratch = new byte[4];
public byte SomeByte
{
get { return b0; }
set { b0 = value; }
}
public int SomeInt
{
get
{
// get the right endianess for your system this is just an example!
scratch[0] = b1;
scratch[1] = b2;
scratch[2] = b3;
scratch[3] = b4;
return BitConverter.ToInt32(scratch, 0);
}
}
public short SomeShort
{
get
{
// get the right endianess for your system this is just an example!
scratch[0] = b5;
scratch[1] = b6;
return BitConverter.ToInt16(scratch, 0);
}
}
public byte SomeByte2
{
get { return b7; }
set { b7 = value; }
}
}
You need to post a more relevant example. Setting packing on that struct would have no effect anyway.
My bet is that you need to use LaoutKind.Explicit and then give the offsets for each member. It's way better than messing with the packing anyway, because it's way more obvious to someone looking at the code that the original developer explicitly meant for things to be unaligned.
Something along these lines:
[StructLayout(LayoutKind.Explicit)]
struct Foo
{
[FieldOffset(0)]
byte a;
[FieldOffset(1)]
uint b;
}
I think one should take Stephen Martin's answer, make it accept a T, and use reflection to generically implement the MarshalManagedToNative and MarshalNativeToManaged methods. Then, you'll have a custom packed struct marshaler that will work for any type of struct.
Here's the code:
using System;
using System.Threading;
using System.Reflection;
using System.Runtime.InteropServices;
namespace System.Runtime.InteropServices
{
public class PinnedObject : IDisposable
{
private GCHandle gcHandle = new GCHandle();
public PinnedObject(object o)
{
gcHandle = GCHandle.Alloc(o, GCHandleType.Pinned);
}
public unsafe static implicit operator byte*(PinnedObject po)
{
return (byte*)po.gcHandle.AddrOfPinnedObject();
}
#region IDisposable Members
public void Dispose()
{
if (gcHandle.IsAllocated)
{
gcHandle.Free();
}
}
#endregion
}
public class PackedStructMarshaler<T> : ICustomMarshaler where T : struct
{
private static ICustomMarshaler m_instance = new PackedStructMarshaler<T>();
public static ICustomMarshaler GetInstance()
{
return m_instance;
}
private void ForEachField(Action<FieldInfo> action)
{
foreach (var fi in typeof(T).GetFields(BindingFlags.Public | BindingFlags.NonPublic))
{
// System.Diagnostics.Debug.Assert(fi.IsValueType);
action(fi);
}
}
private unsafe void MemCpy(byte* dst, byte* src, int numBytes)
{
for (int i = 0; i < numBytes; i++)
{
dst[i] = src[i];
}
}
#region ICustomMarshaler Members
public void CleanUpManagedData(object ManagedObj)
{
}
public void CleanUpNativeData(IntPtr pNativeData)
{
Marshal.FreeHGlobal(pNativeData);
}
public int GetNativeDataSize()
{
unsafe
{
int ret = 0;
ForEachField(
(FieldInfo fi) =>
{
Type ft = fi.FieldType;
ret += Marshal.SizeOf(ft);
});
return ret;
}
}
private object m_marshaledObj = null;
public unsafe IntPtr MarshalManagedToNative(object obj)
{
IntPtr nativeData = (IntPtr)0;
if (obj != null)
{
if (m_marshaledObj != null)
throw new ApplicationException("This instance has already marshaled a managed type");
m_marshaledObj = obj;
nativeData = Marshal.AllocHGlobal(GetNativeDataSize());
byte* pData = (byte*)nativeData;
int offset = 0;
ForEachField(
(FieldInfo fi) =>
{
int size = Marshal.SizeOf(fi.FieldType);
using (PinnedObject po = new PinnedObject(fi.GetValue(obj)))
{
MemCpy(pData + offset, po, size);
}
offset += size;
});
}
return nativeData;
}
public object MarshalNativeToManaged(IntPtr pNativeData)
{
if (m_marshaledObj != null)
m_marshaledObj = null;
unsafe
{
byte* pData = (byte*)pNativeData;
int offset = 0;
object res = new T();
ForEachField(
(FieldInfo fi) =>
{
int size = Marshal.SizeOf(fi.FieldType);
fi.SetValue(res, (object)(*((byte*)(pData + offset))));
offset += size;
});
return res;
}
}
#endregion
}
}
LayoutKind.Explicit would be your best bet for defining a specific memory layout. However, do not use LayoutKind.Explicit for structures that contain pointer-sized values such as true pointers, operating system handles or IntPtrs; this is just asking for mysterious trouble at runtime on random platforms.
In particular, LayoutKind.Explicit is a poor substitute for anonymous unions. If your target structure contains an anonymous union, convert it to a named union; you can safely represent a named union as a struct with LayoutKind.Explicit where all offsets are 0.
LayoutKind.Explicit and FieldOffsetAttribute will allow you to do anything you could do with the Pack property. These explicit layout attributes allow you to specify the exact byte position of each field in the struct (relative to the beginning of the struct's range of memory). The Pack property is used by the runtime to help determine the exact position of each field when using a sequential layout. The pack property has no other effect, so using explicit layout allows you to emulate the exact same behavior, albeit a bit more verbosely. If you don't think this solves your problem, perhaps you could post a bit more information about what you're trying to do or why you think you need to use the Pack property.
Edit: I just noticed the additional comment about trying to get the entire structure's size to 8 bytes. Have you tried using the StructLayoutAttribute.Size property? Unlike Pack, it is available in the Compact Framework.