C++ unmanaged DLL in c# - c#

I've been asked to intergrate webcam ZoneTrigger in my project. The SDK provided in the site is C++ also the sample. I've been able to get few functions to work. the place i've been stuck is a function where char* is a parameter passed. I did a lot of digging and have come to know that MarshalAs has to be used...
the function i'd like to import from
c++ code
//header file
struct ZT_TRIG_STRUCT
{
int aSize; //STRUCT size
int CameraIndex;
int SpotIndex;
int SpotType;
char SpotName[32];
DWORD Dummy[16];
};
typedef int (WINAPI *f_ZT_EnumerateHotSpots)(int SpotIndex, char *Name, int *SpotType);
/*
You application can call this functions to retrieve information about spots by iterating the SpotIndex param.
Name is a pointer to a 32 byte buffer supplied by your application.
SpotType is a pointer to an 32 bit integer in your application.
Return value:
0 = Success, the Name and SpotType have been initialized
1 = Error, we have lost communication with Zone Trigger
2 = Enumaration is over, all spots have been enumerated. Your application should increment SpotIndex until this function returns 2.
*/
//code
//Enumerate current spots in Zone Trigger
int i=0;
char SpotName[32];
int SpotType;
check = ZT_EnumerateHotSpots(i, SpotName, &SpotType);
while (check == 0)
{
float sensitivity = ZT_GetSensitivity(i);
printf("Zone Trigger spot: %s Sensitivity: %0.1f%%\r\n", SpotName, sensitivity*100);
check = ZT_EnumerateHotSpots(++i, SpotName, &SpotType);
}
So when converting to c#
[DllImport("ZTcom.dll")]
private static extern int ZT_EnumerateHotSpots(int i, [MarshalAs(UnmanagedType.LPWStr)]ref string SpotName, ref int SpotType);
public unsafe struct ZT_TRIG_STRUCT
{
public int aSize; //STRUCT size
public int CameraIndex;
public int SpotIndex;
public int SpotType;
public string SpotName ;
//[MarshalAs(UnmanagedType.LPStr, SizeConst = 256)] string SpotName;
// public IntPtr SpotName;
}
//In code
int i = 0;
string SpotName;
int SpotType;
check = ZT_EnumerateHotSpots(i, ref SpotName, ref SpotType);
the above line gives:
Exception of type 'System.ExecutionEngineException' was thrown.
ERROR.
i know that the problem is in SpotName,Its a byte[32], if i dont use marshalAs,instead i use just char it works n gives the first char..
the code where it works fine n gives first char is below
public unsafe struct ZT_TRIG_STRUCT
{
public int aSize; //STRUCT size
public int CameraIndex;
public int SpotIndex;
public int SpotType;
public char SpotName ;
}
[DllImport("ZTcom.dll")]
private static extern int ZT_EnumerateHotSpots(int i, ref char SpotName, ref int SpotType);
public char SpotName;
int i = 0;
check = ZT_EnumerateHotSpots(i, ref SpotName, ref SpotType);
while (check == 0)
{
float sensitivity = ZT_GetSensitivity(i);
textBox1.Text = textBox1.Text + "\r\n" +"Zone Trigger spot: " + SpotName + " Sensitivity: " + (sensitivity * 100).ToString();
check = ZT_EnumerateHotSpots(++i, ref SpotName, ref SpotType);
}
but if i put char[] it gives
Method's type signature is not Interop compatible. ERROR
i've run out of options here.... where have i gone wrong? should i use MarshalAs or char[]???
please help.....
thanks in Advance....

I can't see where you are using ZT_TRIG_STRUCT, but that should be declared like this:
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct ZT_TRIG_STRUCT
{
public int aSize;
public int CameraIndex;
public int SpotIndex;
public int SpotType;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]
public string SpotName;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=16)]
public uint[] Dummy;
}
The other problem you have is in the declaration of ZT_EnumerateHotSpots. That should be:
[DllImport("ZTcom.dll", CharSet=CharSet.Ansi)]
private static extern int ZT_EnumerateHotSpots(
int SpotIndex,
StringBuilder SpotName,
out int SpotType
);
As I read it, SpotName is actually an out parameter, i.e. you supply a buffer and ZT_EnumerateHotSpots writes to it.
You then call this like so
int SpotIndex = 0;
StringBuilder SpotName = new StringBuilder(32);
int SpotType;
int result = ZT_EnumerateHotSpots(SpotIndex, SpotName, out SpotType);

Related

How to send the structure pointer to DeviceIoControl API in WINCE7 C#.net

I am working on WIN CE platform, developing windows forms in C#.Net.
The DeviceIoControl API is working fine with the parameters (mentioned below) in c++ console application.
PNIC_STATISTICS structure in nuiouser.h
global declarations :
TCHAR PCI1_NAME[] = _T("PCI\\ManiXX1");
TCHAR *AUB_NAME = NULL;
AUB_NAME = PCI1_NAME;
pNICStat = (PNIC_STATISTICS)malloc(sizeof(NIC_STATISTICS)) ;
pNICStat->ptcDeviceName = AUB_NAME ; //wchar_t* ptcDeviceName
DeviceIoControl( hUB94Port, //void*
IOCTL_NDISUIO_NIC_STATISTICS,
pNICStat, //PNIC_STATISTICS
0,
pNICStat, //PNIC_STATISTICS
sizeof(NIC_STATISTICS),
&dwReturnedBytes,
NULL
);
<==============================================================================>
But I'm getting problems in implementing the same with C#.Net CF for WIN-CE7
My WIN-CE Code is as follows:
Modified Structure in C#:
[StructLayout(LayoutKind.Sequential,CharSet = CharSet.Unicode)]
public struct __NIC_STAT
{
ulong Size; // Of this structure.
public Char[] ptcDeviceName; // The device name to be queried..
ulong DeviceState; // DEVICE_STATE_XXX above
ulong DeviceState; // DEVICE_STATE_XXX above
ulong MediaType; // NdisMediumXXX
ulong MediaState; // MEDIA_STATE_XXX above
ulong PhysicalMediaType;
ulong LinkSpeed; // In 100bits/s. 10Mb/s = 100000
UInt64 PacketsSent;
UInt64 PacketsReceived;
ulong InitTime; // In milliseconds
ulong ConnectTime; // In seconds
UInt64 BytesSent; // 0 - Unknown (or not supported)
UInt64 BytesReceived; // 0 - Unknown (or not supported)
UInt64 DirectedBytesReceived;
UInt64 DirectedPacketsReceived;
ulong PacketsReceiveErrors;
ulong PacketsSendErrors;
ulong ResetCount;
ulong MediaSenseConnectCount;
ulong MediaSenseDisconnectCount;
} ;
From this Structure I am just filling ptcDeviceName and trying to send.
__NIC_STAT NIC_STAT = new __NIC_STAT();
Char[] toBytes = {'P','C','I','\\','M','a','n','i','X','X','1','\0'}
NIC_STAT.ptcDeviceName = toBytes; //public Char[] ptcDeviceName; in structure
// __NIC_STAT this is the same structure as
//in nuiouser.h
int sz = Marshal.SizeOf(NIC_STAT.GetType());//sometimes Getting exception here
intptr ptr = Marshal.AllocHGlobal(sz);
Marshal.StructureToPtr((__NIC_STAT)NIC_STAT, ptr, false);
unsafe
{
DeviceIoControl(hFileHandle,
IOCTL_NDISUIO_NIC_STATISTICS,
ref ptr,
0,
ref ptr,
sz,
ref dwReturnedBytes,
0);
}//unsafe
It's corresponding prototype
[DllImport("coredll.dll", CharSet = CharSet.Auto, SetLastError = true)]
unsafe public static extern bool DeviceIoControl(
int hDevice,
int dwIoControlCode,
ref intptr InBuffer,
int nInBufferSize,
ref intptr OutBuffer,
int nOutputBufferSize,
ref int pBytesReturned,
int pOverlapped
);
In Win-CE DeviceIoControl() is getting failed, with exception and not displaying any error codes. and sometimes getting error code as 87 (INVALID PARAMETERS).
I feel ptcDeviceName is creating the problem or may be because of allocating memory for pointer (ptr) ?
In Console application we are sending ptcDeviceName as Wchar_t* but in WIN-CE so I used
public Char[] ptcDeviceName;
Can anybody tell me where I am doing wrong.?
You have a couple problems going on here.
First is that you seem to think a ulong is 32-bits in C#, but it's not. It'64 bits, so your struct is totally mapped wrong.
Second, I'm sure you need to be setting the struct Size member before passing it to the call.
Third, that ptcDeviceName member is a pointer to a wide character string. That means that in the struct itself it's 4 bytes. I'd likely make it an IntPtr. You then need to separately allocate the string, pin it, and put the pointer to it into that member slot. Since `StringToHGlobal doesn't exist in the CF, it would look something like this:
public struct __NIC_STAT
{
public uint Size;
public IntPtr ptcDeviceName;
public uint DeviceState;
public uint DeviceState;
public uint MediaType;
public uint MediaState;
public uint PhysicalMediaType;
public uint LinkSpeed;
public ulong PacketsSent;
public ulong PacketsReceived;
public uint InitTime;
public uint ConnectTime;
public ulong BytesSent;
public ulong BytesReceived;
public ulong DirectedBytesReceived;
public ulong DirectedPacketsReceived;
public uint PacketsReceiveErrors;
public uint PacketsSendErrors;
public uint ResetCount;
public uint MediaSenseConnectCount;
public uint MediaSenseDisconnectCount;
};
....
var myStruct = new __NIC_STAT();
myStruct.Size = (15 * 4) + (6 * 8);
var name = "PCI\\Manixx1\0";
var nameBytes = Encoding.Unicode.GetBytes(name);
myStruct.ptcDeviceName = Marshal.AllocHGlobal(nameBytes.Length);
try
{
Marshal.Copy(nameBytes, 0, myStruct.ptcDeviceName, nameBytes.Length);
// make the IOCTL call, a-la
NativeMethods.DeviceIoControl(...., ref myStruct, ....);
}
finally
{
Marshal.FreeHGlobal(myStruct.ptcDeviceName);
}

C# call C++ DLL passing pointer-to-pointer argument

Could you guys please help me solve the following issue?
I have a C++ function dll, and it will be called by another C# application.
One of the functions I needed is as follow:
struct DataStruct
{
unsigned char* data;
int len;
};
DLLAPI int API_ReadFile(const wchar_t* filename, DataStruct** outData);
I wrote the following code in C#:
class CS_DataStruct
{
public byte[] data;
public int len;
}
[DllImport("ReadFile.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
private static extern int API_ReadFile([MarshalAs(UnmanagedType.LPWStr)]string filename, ref CS_DataStruct data);
Unfortunately, the above code is not working... I guess that is due to the C++ func takes a pointer-to-pointer of DataStruct, while I just passed a reference of CS_DataStruct in.
May I know how can I pass a pointer-to-pointer to the C++ func? If it is not possible, is there any workaround? (the C++ API is fixed, so changing API to pointer is not possible)
Edit:
Memory of DataStruct will be allocated by c++ function. Before that, I have no idea how large the data array should be.
(Thanks for the comments below)
I used the following test implementation:
int API_ReadFile(const wchar_t* filename, DataStruct** outData)
{
*outData = new DataStruct();
(*outData)->data = (unsigned char*)_strdup("hello");
(*outData)->len = 5;
return 0;
}
void API_Free(DataStruct** pp)
{
free((*pp)->data);
delete *pp;
*pp = NULL;
}
The C# code to access those functions are as follows:
[StructLayout(LayoutKind.Sequential)]
struct DataStruct
{
public IntPtr data;
public int len;
};
[DllImport("ReadFile.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
unsafe private static extern int API_ReadFile([MarshalAs(UnmanagedType.LPWStr)]string filename, DataStruct** outData);
[DllImport("ReadFile.dll", CallingConvention = CallingConvention.Cdecl)]
unsafe private static extern void API_Free(DataStruct** handle);
unsafe static int ReadFile(string filename, out byte[] buffer)
{
DataStruct* outData;
int result = API_ReadFile(filename, &outData);
buffer = new byte[outData->len];
Marshal.Copy((IntPtr)outData->data, buffer, 0, outData->len);
API_Free(&outData);
return result;
}
static void Main(string[] args)
{
byte[] buffer;
ReadFile("test.txt", out buffer);
foreach (byte ch in buffer)
{
Console.Write("{0} ", ch);
}
Console.Write("\n");
}
The data is now transferred to buffer safely, and there should be no memory leaks. I wish it would help.
It isn't necessary to use unsafe to pass a pointer to an array from a DLL. Here is an example (see the 'results' parameter). The key is to use the ref attribute. It also shows how to pass several other types of data.
As defined in C++/C:
#ifdef __cplusplus
extern "C" {
#endif
#ifdef BUILDING_DLL
#define DLLCALL __declspec(dllexport)
#else
#define DLLCALL __declspec(dllimport)
#endif
static const int DataLength = 10;
static const int StrLen = 16;
static const int MaxResults = 30;
enum Status { on = 0, off = 1 };
struct Result {
char name[StrLen]; //!< Up to StrLen-1 char null-terminated name
float location;
Status status;
};
/**
* Analyze Data
* #param data [in] array of doubles
* #param dataLength [in] number of floats in data
* #param weight [in]
* #param status [in] enum with data status
* #param results [out] array of MaxResults (pre-allocated) DLLResult structs.
* Up to MaxResults results will be returned.
* #param nResults [out] the actual number of results being returned.
*/
void DLLCALL __stdcall analyzeData(
const double *data, int dataLength, float weight, Status status, Result **results, int *nResults);
#ifdef __cplusplus
}
#endif
As used in C#:
private const int DataLength = 10;
private const int StrLen = 16;
private const int MaxThreatPeaks = 30;
public enum Status { on = 0, off = 1 };
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct Result
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = StrLen)] public string name; //!< Up to StrLen-1 char null-terminated name
public float location;
public Status status;
}
[DllImport("dllname.dll", CallingConvention = CallingConvention.StdCall, EntryPoint = "analyzeData#32")] // "#32" is only used in the 32-bit version.
public static extern void analyzeData(
double[] data,
int dataLength,
float weight,
Status status,
[MarshalAs(UnmanagedType.LPArray, SizeConst = MaxResults)] ref Result[] results,
out int nResults
);
Without the extern "C" part, the C++ compiler would mangle the export name in a compiler dependent way. I noticed that the EntryPoint / Exported function name matches the function name exactly in a 64-bit DLL, but has an appended '#32' (the number may vary) when compiled into a 32-bit DLL. Run dumpbin /exports dllname.dll to find the exported name for sure. In some cases you may also need to use the DLLImport parameter ExactSpelling = true. Note that this function is declared __stdcall. If it were not specified, it would be __cdecl and you'd need CallingConvention.Cdecl.
Here is how it might be used in C#:
Status status = Status.on;
double[] data = { -0.034, -0.05, -0.039, -0.034, -0.057, -0.084, -0.105, -0.146, -0.174, -0.167};
Result[] results = new Result[MaxResults];
int nResults = -1; // just to see that it changes (input value is ignored)
analyzeData(data, DataLength, 1.0f, status, ref results, out nResults);
If you do call native code, make sure your structs are alligned in the memory. CLR does not guarantee alignment unless you push it.
Try
[StructLayout(LayoutKind.Explicit)]
struct DataStruct
{
string data;
int len;
};
More info:
http://www.developerfusion.com/article/84519/mastering-structs-in-c/

Last arguments appear corrupted when calling native function

Working on a C# wrapper for a native (C) library. I have the following function prototype in the native library:
typedef struct _NativeObj * NativeObj;
typedef struct AnotherNativeObj * AnotherNative;
__declspec(dllimport) NativeObj createNativeObj (
AnotherNative * anotherNative,
FirstCallback firstCallback,
void * firstOpaque,
SecondCallback secondCallback,
void * secondOpaque,
ThirdCallback thirdCallback,
void * thirdOpaque,
const char * firstString,
const char * secondString,
const char * thirdString,
time_t timeout,
char * fourthString,
int firstInt,
int secondInt,
int thirdInt,
int fourthInt,
char * fifthString,
int fifthInt,
char * sixthString);
This is the declaration in the C# code:
public delegate int ThirdCallbackDelegate(...);
public const uint NO_TIMEOUT = 0;
private uint timeout = NO_TIMEOUT;
private string fourthString;
private uint firstInt = 0;
private bool secondInt = false;
private bool thirdInt = true;
private bool fourthInt = true;
private string fifthString;
private bool fifthInt = false;
public string sixthString { get; set; }
[DllImport("path\\to.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern IntPtr createNativeObj(
IntPtr anotherNative,
FirstCallbackDelegate firstCallback,
IntPtr firstOpaque,
SecondCallbackDelegate secondCallback,
IntPtr secondOpaque,
ThirdCallbackDelegate thirdCallback,
IntPtr thirdOpaque,
string firstString,
string secondString,
string thirdString,
int timeout,
string fourthString,
int firstInt,
int secondInt,
int thirdInt,
int fourthInt,
string fifthString,
int fifthInt,
string sixthString);
And the logic behind the parameters:
IntPtr myOpaque = createNativeObj(IntPtr.Zero,
null,
IntPtr.Zero,
null,
IntPtr.Zero,
thirdCallbackDelegate,
IntPtr.Zero,
firstString,
secondString,
thirdString,
(int)timeout,
fourthString,
(int)firstInt,
Convert.ToInt32(secondInt),
Convert.ToInt32(thirdInt),
Convert.ToInt32(fourthInt),
fifthString,
Convert.ToInt32(fifthInt),
sixthString);
At runtime, right at the native function's start, the values for the arguments after timeout are corrupted.
On Windows, using MS tools, and assuming that you did not define _USE_32BIT_TIME_T, the time_t type is 8 bytes wide. Which means you need to declare it as long in your C# p/invoke code to match.
I suspect that your native library isn't using the __cdecl calling convention but something like __stdcall. In general it's best to not take any chances and enforce a calling convention at the native library level instead of letting the compiler or the project options determine it. Try this:
[DllImport("path\\to.dll", CallingConvention=CallingConvention.StdCall)]

Marshaling structs with strings and arrays from managed to unmanaged code

I have a simple C# data structure with a string, an int and a vector of ints:
class MyManagedClass
{
public string m_Str;
int m_Int;
int[] m_IntArray;
}
The equivalent in C++ is:
struct myUnmanagedStruct
{
char* m_Str;
UINT m_Int;
UINT* m_IntArray;
}
I have an unmanaged function that creates an array of myUnmanagedStruct structs. What is the best way to write a managed wrapper that correctly marshals the data such that what is created on the unmanaged side is correctly passed back to the managed side? (i.e. I want to make an array of MyManagedClass objects from an array of MyUnmanagedStructs)
Note:
a) A string is created in the unmanaged struct
b) A vector of ints is created in the unmanaged struct
My best attempt so far is:
On the managed side:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack=1)]
public class MyManagedClass
{
public MyManagedClass()
{
m_IntArray = new int[4];
}
public String m_Str;
public int m_Int;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public int[] m_IntArray;
}
[StructLayout(LayoutKind.Sequential)]
public struct UnsafeLCEArray
{
public int m_Int;
public IntPtr m_CharBuf;
public IntPtr m_IntBuf;
}
public unsafe class LibWrap
{
// Declares managed prototypes for the unmanaged function.
[DllImport("mydll.dll", EntryPoint = "GetUnmanagedStructs")]
public static extern voidGetUnmanagedStructs(out int size, out IntPtr outArray);
}
On the unmanaged side:
typedef struct _MyUnmanagedStruct
{
char* m_Str;
UINT m_Int;
UINT* m_IntArray;
} MyUnmanagedStruct;
typedef struct _UNSAFELCEARRAY
{
char* strBuf;
UINT intBuf;
UINT* intArrayBuf;
} UNSAFELCEARRAY;
extern "C" __declspec(dllexport) void GetUnmanagedStructs( int* pSize, UNSAFELCEARRAY** ppStruct )
{
const int cArraySize = 5;
*pSize = cArraySize;
int numBytes = cArraySize * sizeof( MyUnmanagedStruct);
*ppStruct = (UNSAFELCEARRAY*)CoTaskMemAlloc(numBytes);
UNSAFELCEARRAY* pCurStruct = *ppStruct;
char* typenamebuffer;
char* numBuffer;
int var = 999;
for( int i = 0; i < cArraySize; i++, pCurStruct++ )
{
pCurStruct->intBuf = i+1;
typenamebuffer = (char*)CoTaskMemAlloc( 8 );
memcpy_s(typenamebuffer, 8, "bufABCD", 8);
pCurStruct->strBuf = typenamebuffer;
numBuffer = (char*)CoTaskMemAlloc( 16 );
++var;
memcpy_s(numBuffer, 4, &var, 4);
++var;
memcpy_s(numBuffer+4, 4, &var, 4);
++var;
memcpy_s(numBuffer+8, 4, &var, 4);
++var;
memcpy_s(numBuffer+12, 4, &var, 4);
pCurStruct->intArrayBuf = (UINT*)numBuffer;
}
}
Everything works if I remove the vector of ints from the managed and unmanaged objects, but with the code above the array of ints is uninitialized on return. I use the function below to generate MyManagedClasses from MyUnmanagedStructs
int size;
IntPtr outArray;
LibWrap.GetUnmanagedStructs(out size, out outArray);
manArray = new MyManagedClass[size];
IntPtr current = outArray;
for (int i = 0; i < size; i++)
{
manArray[i] = new MyManagedClass();
Marshal.PtrToStructure(current, manArray[i]);
Marshal.DestroyStructure(current, typeof(sb_LCE));
int numBytes = Marshal.SizeOf(manArray[i]);
current = (IntPtr)((long)current + numBytes);
}
Marshal.FreeCoTaskMem(outArray);
Forgive the lengthy explanation and the fact the unmanaged strut is being filled with dummy values. This is just for illustration.

passing array of structs from c# to regular dll

I have a regular dll with the following function exported.
extern "C" __declspec(dllexport) int FindNearestStuff(double _latitude, double _longitude , LocationStruct * locations[])
LocationStruct is very simple
struct LocationStruct
{
long positionIndex;
long item;
};
I'm tryign to call it from c# using
[DllImport("myclever.dll", CharSet = CharSet.None)]
private static extern int FindNearestStuff(double _latitude, double _longitude,
ref LocationStruct [] locations);
It's all cool and funky and I can step into the dll function from the debugger.
Inside the dll the LocationStruct array is populated correctly and all is very good.
The problem I have is when it returns back from the dll, the LocationStruct array is not coming back with the data - just empty values...
What am I missing?
thanks so much for your help - you certainly put me onthe right direction and i really appreciate your assistance!
This is the solution which seems to work for me;
[DllImport("myclever.dll", CharSet = CharSet.None)]
private static extern int FindNearestStuff(double _latitude, double _longitude, IntPtr locations);
public static int FindNearestStuff(double _latitude, double _longitude, LocationStruct[] locations)
{
int returnValue = -1;
LocationStruct temp;
temp.roadIndex = 1;
temp.tdist = 1;
int iStructSize = Marshal.SizeOf(temp);
try
{
IntPtr locationsPtr = IntPtr.Zero;
IntPtr buffer = Marshal.AllocHGlobal(iStructSize * 10);
FindNearestRoads(_latitude, _longitude, buffer);
for (int i = 0; i < 10; i++)
{
IntPtr ptr = new IntPtr(buffer.ToInt32() + iStructSize * i);
locations[i] = (LocationStruct)Marshal.PtrToStructure(ptr, typeof(LocationStruct));
}
returnValue = 0;
}
catch
{
}
return returnValue;
}
I'm not sure that you will be able to do this automatically since C# has no way of knowing how many items are returned in the locations variable (I'm assuming that the return value of FindNearestStuff is the number of entries in locations.)
You will have to manually marshal your data using the Marshall class and a process like this:
[DllImport("myclever.dll", CharSet = CharSet.None)]
private static extern int FindNearestStuff(double _latitude, double _longitude,
out IntPtr locations);
public static LocationStruct[] FindNearestStuff(double latitude, double longitude) {
IntPtr locationsPtr = IntPtr.Zero;
int numLocations = FindNearestStuff(latitude, longitude, out locationsPtr);
LocationsStruct[] locations = new LocationsStruct[numLocations];
for (int i = 0; i < numLocations; i++) {
// locationsPtr is a pointer to the struct, so read the value
// at locationPtr which will be the address of the struct and
// then manually marshal the struct from that address
locaitonsStruct[i] = (LocationStruct)Marshal.PtrToStructure(
Marshal.ReadIntPtr(locationsPtr), typeof(LocationsStruct));
// Move to the location pointer to the next address of a
// pointer to a struct
locationsPtr += IntPtr.Size;
}
return locations;
}
I haven't actually tried this so caveat emptor.

Categories