Access Violation calling unmanaged dll

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)ASIXUPup_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)ASIXUPup_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)ASIXUPup_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?

6

Changing the code to the following seems to work:

[DllImport(@"C:Program Files (x86)ASIXUPup_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);
    }
}

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật