Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8530481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:15:23+00:00 2026-06-11T09:15:23+00:00

I keep getting an AccessViolationException when calling the following from an external C DLL:

  • 0

I keep getting an AccessViolationException when calling the following from an external C DLL:

short get_device_list(char ***device_list, int *number_of_devices);

I set up a DLLImport declaration as such:

[DLLImport("mydll.dll")]
static public extern short get_device_list([MarshalAs(UnmanagedType.LPArray)] ref string[] devices, ref int number_of_devices);

My C# application code:

{
string[] devices = new string[20];
int i = 0;
short ret = 0;
ret = get_device_list(ref devices, ref i); // I receive the AccessViolation Exception here
// devices[0] = "2255f796e958f7f31a7d2e6b833d2d426c634621" which is correct.
}

Although I receive the exception, the device array gets filled correctly with the 2 UUIDs of the devices connected (and also gets resized to size = 2; i is also 2;).

What is wrong?

PS: After a long research I also tried:

[DLLImport("mydll.dll")]
static public extern short get_device_list(ref IntPtr devices, ref int number_of_devices);

and

{
IntPtr devices = new IntPtr();
int i = 0;
short ret = 0;
ret = get_device_list(ref devices, ref i); // No AccessViolation Exception here
string b = Marshal.PtrToStringAuto(devices); // b = "歀ׄ", which is incorrect
}

but that did not help me.

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T09:15:24+00:00Added an answer on June 11, 2026 at 9:15 am
    [DLLImport("mydll.dll")]
    static public extern short get_device_list(out IntPtr devices, 
        out int number_of_devices);
    

    Is the best way to tackle this. The memory is allocated and owned on the native side of the interface. The trick is how to get at it. Something like this should work.

    static public string[] getDevices()
    {
        IntPtr devices;
        int deviceCount;
        short ret = get_device_list(out devices, out deviceCount);
        //need to test ret in case of error
    
        string[] result = new string[deviceCount];
        for (int i=0; i<deviceCount; i++)
        {
            IntPtr ptr = (IntPtr)Marshal.PtrToStructure(devices, typeof(IntPtr));
            result[i] = Marshal.PtrToStringAnsi(ptr);
            devices += IntPtr.Size;//move to next element of array
        }
        return result;
    }
    

    Your code was using PtrToStringAuto but that’s going to interpret the data as UTF-16 encoded. But your C++ code uses char* which is 8 bit ANSI. So you need PtrToStringAnsi. OK, there’s an assumption here that the encoding is not UTF-8, but that’s a detail I cannot provide. It’s easy enough to adapt this to UTF-8.

    You should also double check that the native code uses the stdcall calling convention and isn’t using cdecl.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I keep getting an AccessViolationException when calling the following from an external DLL: FILES_GetMemoryMapping(MapFile,
I keep getting this error on emulator: I copied this from a tutorial and
Keep getting The constructor SimpleCursorAdapter(MyProgram, int, Cursor, int) is undefined and am not sure
I keep getting some weird error in Processing, using the following tiny piece of
I keep getting the following error: * #1005 - Can't create table 'classorganizer.turma' (errno:
I keep getting E_INVALIDARG when calling RenderStream(). My program output is: SUCCESS - Initialized
I keep getting the following error after I put the required files in my
I keep getting the same error when I try to set an attribute but
I keep getting a 500 Internal Server Error when using the following Rewrite rule
I keep getting the following: The source control proxy 'tfs2010' is not responding, so

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.