I try to write following struct to a memory mapped file, but I still have problem with the array (writing throws exception that the struct can not contain reference)
[StructLayout(LayoutKind.Explicit)]
struct IndexEntry {
[FieldOffset(0)]
public byte key;
[FieldOffset(4)]
public int lastValueIdx;
[FieldOffset(8)]
[MarshalAs(UnmanagedType.ByValArray, SizeConst = Constants.PART_ENTRY_SIZE)]
public long[] values;
}
I use this calling for writing:
UnmanagedMemoryAccessor.Write<IndexEntry>(0, ref entry);
Can you please tell me, what am I doing wrong? Thx
The solution of this is using the fixed size array and unsafe code. So the struct should look like this:
[StructLayout(LayoutKind.Explicit)]
unsafe struct IndexEntry {
[FieldOffset(0)]
public byte key;
[FieldOffset(1)]
public int lastValueIdx;
[FieldOffset(5)]
[MarshalAs(UnmanagedType.ByValArray, SizeConst = Constants.PART_ENTRY_SIZE)]
public fixed long values[Constants.PART_ENTRY_SIZE];
}
Note that the program (or a single project containing that struct) must be compiled with the "/unasfe" option and the array must be then accessed like this:
fixed(long* arr = this.values) {
// Setting value
arr[index] = value;
}
unsafe {
// Getting value
x = obj.values[index];
}
Then the UnmanagedMemoryAccessor.Write<T>(...) and UnmanagedMemoryAccessor.Read<T>(...) functions work perfectly.
Related
I am facing the following issue.
I have a C++ API passed to C# through DLL. Here is how the api looks like:
header.h
extern "C" __declspec( dllexport) bool f(CustomClass* myclass);
where:
struct CustomClass
{
double* grades;
int* ages;
char name[16];
}
So I intend to call this f from C# and face difficulties to allocate the pointers:
To be more precise, I can allocate the CustomClass* but cannot the underlying types:
struct CustomClass
{
public unsafe char* name;
public unsafe double* grades;
public unsafe int* ages;
};
fixed(CustomClass* instances = new CustomClass[5]) // this is fine
{
instances[0].grades = new double[10];// no good, , cannot implicitly convert from double[] to double*
fixed(instances[0].grades = new double[10]) // no good, instances is a variable but used as a type
{
}
}
Can anyone help to understand how to allocate a pointer inside a class?
Or maybe any other workaround to satisfy the C++ DLL api?
You need to define the struct using fixed-size arrays. Fill in the necessary size for each
struct CustomClass
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
double[] grades = new double[10];
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
int[] ages = new int[10];
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
char[] name = new char[16];
}
The struct define in C++ is:
typedef struct _DEVICE_PATH_LIST_
{
int NumDevice;
TCHAR *devicePath[DEVICE_PATH_LIST_SIZE];
} DEVICE_PATH_LIST;
and the C++ function's definition is:
LandiWin_iGetDevicePathList( int iDeviceType, DEVICE_PATH_LIST * devicePathList);
Then I want to call that method, so I need to define a struct that can represent the DEVICE_PATH_LIST. Here is my definition in C#:
[StructLayout(LayoutKind.Sequential)]
public unsafe struct DevicePathType
{
public int NumDevice;
[MarshalAs(UnmanagedType.ByValArray,SizeConst=32)]
public char*[] devicePath;
}
but it's not right, I also tried all other type but all failed. please help me define a right struct.
The code of C++ like:
typedef struct _DEVICE_PATH_LIST_
{
int NumDevice;
TCHAR *devicePath[DEVICE_PATH_LIST_SIZE];
} DEVICE_PATH_LIST;
The code of C# need to be like:
[StructLayout(LayoutKind.Sequential)]
public struct DevicePathType
{
public int NumDevice;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
public IntPtr[] devicePath;
}
I am building a managed wrapper in C# around the native Windows Biometric Framework, which is used to access biometric sensors like fingerprint sensors.
I have problems getting this method to work with P/Invoke: WinBioIdentify
HRESULT WINAPI WinBioIdentify(
_In_ WINBIO_SESSION_HANDLE SessionHandle,
_Out_opt_ WINBIO_UNIT_ID *UnitId,
_Out_opt_ WINBIO_IDENTITY *Identity,
_Out_opt_ WINBIO_BIOMETRIC_SUBTYPE *SubFactor,
_Out_opt_ WINBIO_REJECT_DETAIL *RejectDetail
);
The problem is the WINBIO_IDENTITY struct because it contains a union:
typedef struct _WINBIO_IDENTITY {
WINBIO_IDENTITY_TYPE Type;
union {
ULONG Null;
ULONG Wildcard;
GUID TemplateGuid;
struct {
ULONG Size;
UCHAR Data[SECURITY_MAX_SID_SIZE]; // the constant is 68
} AccountSid;
} Value;
} WINBIO_IDENTITY;
Here is what I tried:
[StructLayout(LayoutKind.Explicit, Size = 76)]
public struct WinBioIdentity
{
[FieldOffset(0)]
public WinBioIdentityType Type;
[FieldOffset(4)]
public int Null;
[FieldOffset(4)]
public int Wildcard;
[FieldOffset(4)]
public Guid TemplateGuid;
[FieldOffset(4)]
public int AccountSidSize;
[FieldOffset(8)]
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 68)]
public byte[] AccountSid;
}
[DllImport("winbio.dll", EntryPoint = "WinBioIdentify")]
private extern static WinBioErrorCode Identify(
WinBioSessionHandle sessionHandle,
out int unitId,
out WinBioIdentity identity,
out WinBioBiometricSubType subFactor,
out WinBioRejectDetail rejectDetail);
public static int Identify(
WinBioSessionHandle sessionHandle,
out WinBioIdentity identity,
out WinBioBiometricSubType subFactor,
out WinBioRejectDetail rejectDetail)
{
int unitId;
var code = Identify(sessionHandle, out unitId, out identity, out subFactor, out rejectDetail);
WinBioException.ThrowOnError(code, "WinBioIdentify failed");
return unitId;
}
In this form it crashes with a TypeLoadException complaining that the WinBioIdentity struct contains a misaligned field at offset 8. If I leave out that last field it works, but then the most important data is missing, of course.
Any help how to handle this case is very much appreciated.
The Guid in this structure is the trouble-maker. It is 16 bytes long and therefore overlaps the byte[]. The CLR disallows this kind of overlap, it prevents the garbage collector from identifying the array object reference reliably. Very important to know whether or not the array needs to be collected. The GC has no way to find out if the structure contains the Guid or the array.
You must give up on byte[] and substitute it with a value type so the GC does not have to deal with a possibly broken object reference. The C# language has the fixed keyword to declare such kind of value types:
[StructLayout(LayoutKind.Explicit)]
unsafe public struct WinBioIdentity {
//...
[FieldOffset(8)]
public fixed byte AccountSid[68];
}
Note the required unsafe keyword. Project > Properties > Build > Allow unsafe code option. It is in fact pretty unsafe, you'll want to put an assert in your code before you start using the struct so you can be sure no memory corruption can occur:
System.Diagnostics.Debug.Assert(Marshal.SizeOf(typeof(WinBioIdentity)) == 76);
I want to call the DhcpGetClientInfo API from C# but I have a question on conversion of this C struct to C#:
typedef struct _DHCP_CLIENT_SEARCH_INFO {
DHCP_SEARCH_INFO_TYPE SearchType;
union {
DHCP_IP_ADDRESS ClientIpAddress;
DHCP_CLIENT_UID ClientHardwareAddress;
LPWSTR ClientName;
} SearchInfo;
} DHCP_SEARCH_INFO, *LPDHCP_SEARCH_INFO;
I think the Correct conversion is this:
[StructLayout(LayoutKind.Explicit, Size=12)]
public struct DHCP_SEARCH_INFO
{
[FieldOffset(0)]
public DHCP_SEARCH_INFO_TYPE SearchType;
[FieldOffset(4)]
public DHCP_IP_ADDRESS ClientIpAddress;
[FieldOffset(4)]
public DHCP_BINARY_DATA ClientHardwareAddress;
[FieldOffset(4), MarshalAs(UnmanagedType.LPWStr)]
public string ClientName;
};
But that gives an System.TypeLoadException: Additional information: Could not load type 'Dhcpsapi.DHCP_SEARCH_INFO' from assembly 'ConsoleApplication3, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' because it contains an object field at offset 4 that is incorrectly aligned or overlapped by a non-object field.
This is the conversion of the other types in case you want to compile:
public enum DHCP_SEARCH_INFO_TYPE : uint
{
DhcpClientIpAddress = 0,
DhcpClientHardwareAddress = 1,
DhcpClientName = 2
};
[StructLayout(LayoutKind.Sequential)]
public struct DHCP_BINARY_DATA
{
public uint DataLength;
public IntPtr Data;
};
[StructLayout(LayoutKind.Sequential)]
public struct DHCP_IP_ADDRESS
{
public UInt32 IPAddress;
}
EDIT:
I verified sizeof and offsets in C:
#pragma comment(lib,"Dhcpsapi.lib")
int _tmain(int argc, _TCHAR* argv[])
{
DHCP_SEARCH_INFO si;
printf("sizeof(DHCP_SEARCH_INFO)=%d\n", sizeof(DHCP_SEARCH_INFO));
printf("ClientIpAddress offset=%d\n", (PBYTE)&si.SearchInfo.ClientIpAddress - (PBYTE)&si);
printf("ClientHardwareAddress offset=%d\n", (PBYTE)&si.SearchInfo.ClientHardwareAddress - (PBYTE)&si);
printf("ClientName offset=%d\n", (PBYTE)&si.SearchInfo.ClientName - (PBYTE)&si);
return 0;
}
Output is:
sizeof(DHCP_SEARCH_INFO)=12
ClientIpAddress offset=4
ClientHardwareAddress offset=4
ClientName offset=4
EDIT:
Based on Camford's answer I declared the struct as below. Using sizeof should make it correct for x64 as well.
[StructLayout(LayoutKind.Explicit, Size=12)]
public struct DHCP_SEARCH_INFO
{
[FieldOffset(0)]
public DHCP_SEARCH_INFO_TYPE SearchType;
[FieldOffset(sizeof(DHCP_SEARCH_INFO_TYPE))]
public DHCP_IP_ADDRESS ClientIpAddress;
[FieldOffset(sizeof(DHCP_SEARCH_INFO_TYPE))]
public IntPtr ClientName;
[FieldOffset(sizeof(DHCP_SEARCH_INFO_TYPE))]
public DHCP_BINARY_DATA ClientHardwareAddress;
};
The way you are simulating the union is correct as far as I can tell. The exception you are getting is likely related to thestring object in your struct. I tried to build your code in a test project. With string in the struct, I get the same exception as you do. With an IntPtr replacing the string, I don't get any exceptions. Whether the call to DhcpGetClientInfo is going to work or not, I have no idea. You can use Marshal.StringToHGlobalUni to get an IntPtr for your string.
[StructLayout(LayoutKind.Explicit)]
public struct SearchInfo
{
[FieldOffset(0)]
public DHCP_IP_ADDRESS ClientIpAddress;
[FieldOffset(0)]
public DHCP_BINARY_DATA ClientHardwareAddress;
[FieldOffset(0)]
public IntPtr ClientName; //LPWSTR
}
[StructLayout(LayoutKind.Sequential)]
public struct DHCP_SEARCH_INFO
{
public DHCP_SEARCH_INFO_TYPE SearchType;
public SearchInfo SearchInfo;
}
Edit: I guess this means that simulating a union in C# has similar requirement to the union in C++. In C++ you can only have POD types in a union. In C# you can probably only have struct types.
Updated: Thanks to DavidHeffernan for pointing out a better way of laying out structs with unions inside. You can read his explanation below.
I believe that the best thing to do is to add get/set methods to... guess what... get/set the correct bits in a variable. I believe that you'll have to wrap that too
Something like:
[StructLayout(LayoutKind.Sequential)]
public struct DHCP_SEARCH_INFO
{
public DHCP_SEARCH_INFO_TYPE SearchType;
public ulong Complex;
};
public struct DHCP_SEARCH_INFO_WRAP
{
public DHCP_SEARCH_INFO_TYPE SearchType;
private ulong Complex;
public DHCP_IP_ADDRESS ClientIpAddress
{
get
{
return BitConverter.ToInt32(BitConverter.GetBytes(Complex),0);
}
set
{
byte[] orig = BitConverter.GetBytes(Complex);
byte[] chng = BitConverter.GetBytes(value);
Array.Copy(chng,0,orig,0,4);
}
}
public DHCP_SEARCH_INFO_WRAP(ref DHCP_SEARCH_INFO var)
{
this.SearchType = var.SearchType;
this.Complex = var.Complex;
}
};
I wrote this directly here and didn't test it.
EDIT:
Camford enlightened me that you can have unions in C#, I was wrong. But as it seem that you can only emulate the primitive types I stand by my solution
I have a "C" struct which is defined as:
typedef unsigned char tUI8;
typedef struct
{
tUI8 Mode;
tUI8 Data[16];
} TestStruct;
And a function which takes a pointer to this structure and fill the data:
void FillTest(tUI8 Mode, TestStruct *s);
To PInvoke to this function, I wrote the C# code as:
[StructLayout(LayoutKind.Sequential)]
struct TestStruct
{
public byte Mode;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
public byte[] Data;
}
static class NativeTest
{
[DllImport("Native.dll")]
public static extern void FillTest(byte mode, ref TestStruct s);
}
This works but I suspect that during the PInvoke marshaller is copying the struct during call and return instead of pinning it. I can say that because even if I don't initialize the struct, it works fine.
TestStruct s;
//here s.Data == null!
NativeTest.FillTest(10, ref s); //<<< I expected an error here
//here s.Data points to a valid byte[] of length 16
Console.WriteLine(BitConverter.ToString(s.Data));
My question is how can I define a PInvoke signature, using either struct or class, which avoid copying the data during marshalling?
I suspect you want a fixed size buffer, which will inline the data in your struct:
[StructLayout(LayoutKind.Sequential)]
unsafe struct TestStruct
{
public byte Mode;
public fixed byte Data[16];
}
You should now be able to pass that by reference directly to your unmanaged code. (You'll also need to explicitly allow unsafe code.)
I don't know what attributes you'll then need to use for marshalling, if any, but it's worth a try...
This can be done without using unsafe code.
By marshalling as ByValArray
[StructLayout(LayoutKind.Sequential)]
struct TestStruct
{
public byte Mode;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
public byte[] Data;
}