Be careful, in c#
long
is 64 bits, in C/C++ it is 32 bits. And a
WORD
is 16 bits, not 32 bits.
Declare the structs like this:
[StructLayout(LayoutKind.Explicit, Pack = 1)]
public struct SYSTEMTIME
{
public short wYear;
public short wMonth;
public short wDayOfWeek;
public short wDay;
public short wHour;
public short wMinute;
public short wSecond;
public short wMilliseconds;
}
[StructLayout(LayoutKind.Explicit, Pack = 1]
struct THisEvent
{
uint RecordIndex;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
byte[] pCardID;
ushort RecordTypeID;
SYSTEMTIME RecordTime;
}
Try to declare the function like this:
[DllImport("CVIOAccessAPI.dll")]
private static extern int ReadRecordFor_V4XX_1(int Address, uint BeginIndex,
out byte ReadNumber, THisEvent[] pHisEvents, IntPtr ExEvents);
Declaring the function like that is OK to send the array
pHisEvents
in
readonly. If your C code modifies its content, then you should do something a little bit more complicated (I will explain if necessary).
I am not sure about the last parameter
ExEvents
: what do you want to do with this one?
Additionnaly, to help you with marshaling WIN32 functions and types, you may have a look to this usefull site:
http://www.pinvoke.net[
^]
--------------------------------------------------
Sending
pHisEvents
in read/write mode requires a little bit more work:
First, you must change the imported function prototype (use
IntPtr
instead of
THisEvent[]
):
[DllImport("CVIOAccessAPI.dll")]
private static extern int ReadRecordFor_V4XX_1(int Address, uint BeginIndex,
out byte ReadNumber, IntPtr pHisEvents, IntPtr ExEvents);
Then, you must marshal your array manually:
THisEvent[] pHisEvents = ...;
int sizeOfStruct = Marshal.SizeOf(typeof(THisEvent));
IntPtr ptr = Marshal.AllocHGlobal(pHisEvents.Length * sizeOfStruct);
for (int i = 0; i < pHisEvents.Length; i++)
{
IntPtr ptri = (IntPtr)((int)ptr + i * sizeOfStruct);
Marshal.StructureToPtr(pHisEvents[i], ptri, false);
}
ReadRecordFor_V4XX_1(..., ptr, ...);
for (int i = 0; i < pHisEvents.Length; i++)
{
IntPtr ptri = (IntPtr)((int)ptr + i * sizeOfStruct);
pHisEvents[i] = (THisEvent)Marshal.PtrToStructure(ptri, typeof(THisEvent));
}
Marshal.FreeHGlobal(ptr);
I hope this will work.