I have an unmanaged dll, up_control64.dll
, that contains a function
int __stdcall UP_GetProgList(int prog_type, int *sn_list, int count, int *count_returned)
I'm trying to access that function from my C# code, which should translate into the following P/Invoke call:
[DllImport(@"C:\Program Files (x86)\ASIX\UP\up_control64.dll")]
private static extern int UP_GetProgList(int prog_type, out int[] sn_list, int count, out int count_returned);
But when I call the function, I get an Access Violation.
[DllImport(@"C:\Program Files (x86)\ASIX\UP\up_control64.dll")]
private static extern int UP_GetProgList(int prog_type, out int[] sn_list, int count, out int count_returned);
const int PROG_FORTE = 2;
void Main()
{
var list = new int[20];
var count = 0;
var result = UP_GetProgList(PROG_FORTE, out list, 20, out count);
foreach (var sn in list)
{
Console.Out.WriteLine(sn);
}
}
What am I doing wrong?
Edit: here is the documentation on the function:
1.3.11 UP_GetProgList
The function returns list of available programmers, it returns the list only when no programmer is being used by the library. Calling of this function clears settings done by the UP_ProgConfig function.
Function definition:
int __stdcall UP_GetProgList(int prog_type, int *sn_list, int count, int *count_returned);
Parameters:
- prog_type - Selects programmer type in accordance with constants.
- sn_list - Array of integer, which returns the list of the serial numbers of the available FORTE programmers. The serial nubers are returned as 24bit values, same as they are listed in the UP software.
- count - Variable defining the number of the serial numbers to be read.
- count_returned - Variable returning number of serial numbers, which have been returned in sn_list.
Return values:
- ERR_NONE - The function has been successfully called.
- ERR_PROG_BUSY - A programmer is busy.
- ERR_DRIVER - The programmer driver error.
In sn_list the function returns list of available programmers and the count of the returned values is returned in count_returned.
Example:
int prog_list[20]; int prog_list_count; int FuncRes; FuncRes=UP_GetProgList(PROG_FORTE, prog_list, 20 , &prog_list_count);
Edit 2:
If I change the declaration to the following, it works:
[DllImport(@"C:\Program Files (x86)\ASIX\UP\up_control64.dll")]
private static extern int UP_GetProgList(int prog_type, out int sn_list, int count, out int count_returned);
But I can only get one value that way. How can I access the other values in the array, should there be any?
Changing the code to the following seems to work:
[DllImport(@"C:\Program Files (x86)\ASIX\UP\up_control64.dll", CallingConvention=CallingConvention.StdCall)]
private static extern int UP_GetProgList(int prog_type, IntPtr sn_list, int count, out int count_returned);
const int PROG_FORTE = 2;
void Main()
{
foreach (int sn in GetSerialNumbers())
{
Console.Out.WriteLine(sn);
}
}
int[] GetSerialNumbers()
{
IntPtr listPtr = IntPtr.Zero;
try
{
listPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(int)) * 20);
var result = UP_GetProgList(PROG_FORTE, listPtr, 20, out int countReturned);
var sns = new int[countReturned];
Marshal.Copy(listPtr, sns, 0, countReturned);
return sns;
}
finally
{
Marshal.FreeHGlobal(listPtr);
}
}