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

  • SEARCH
  • Home
  • 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 1007031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:33:19+00:00 2026-05-16T08:33:19+00:00

I am having a really hard time getting this marshalling down. I have umanaged

  • 0

I am having a really hard time getting this marshalling down.

I have umanaged code that looks like this:

WORD HLP_GetDeviceNames (LPSTR *DevNames, WORD Max_Len, WORD Max_Num)

Just FYI I did not write this unmanaged code but must use it.

Returns: WORD indicating an error.

DevNames: Pointer to an array of char arrays. Basically an array of strings that will be modified and returned back to me!

Max_Len: Length of each string (I am told this must be 256)

Max_Num: Length of array. I am using another Invoke call that is working that tells me number of devices so i know exactly how many strings to send.

I have used P/Invoke interop signatureToolkit to figure alot of this out but also read a bunch to get even further. Where I am now is here:

[DllImport("UsbMeasLib.dll")]
public static extern ushort HLP_GetDeviceNames([MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPStr)] ref StringBuilder[] DevNames, ushort Max_Len, ushort Max_Num);

I call my code like this:

StringBuilder[] DevNames = new StringBuilder[deviceCount];
     for(int i = 0; i< deviceCount; i++)
     {
           DevNames[i] = new StringBuilder().Append(' ', 256);
     }

     HachUsbMeasLib.HLP_GetDeviceNames(ref DevNames, 256, Convert.ToUInt16(DevNames.Count())); 

I am using string builder array because I need the unmanaged code to modify the string builder so that it can return the new string since string is unmutable.

When I run the code, My array is unmodified!

I’m not really sure what is going on but I think it has something to do with CLR telling unmanaged code to not modify my array in place but instead creates a new reference(pointer). Even if this is the case, I dont know how to fix it.

Thanks for any insight anybody can offer!

  • 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-05-16T08:33:19+00:00Added an answer on May 16, 2026 at 8:33 am

    I figured this one out. Thanks to anybody who replied.

    I found out how it works. I simply supply the memory space but I have to let the marshaling know that I expect in and out with this object so it allows the unmanaged code to modify the allocated space.

    I did it like this:

    [DllImport("UsbMeasLib.dll")]
    private static extern ushort HLP_GetDeviceNames([In, Out, MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPStr)] string[] DevNames, ushort Max_Len, ushort Max_Num);
    

    I use string instead of string builder because the unmanaged code will simply replace the string which is ok. I am getting the array pointer back, not modified strings. the managed code is just changing an array of pointers to point to new string objects (I think).

    int numDev = HLP_GetNumDevices();
    
    
    string[] names = new string[numDev];
    
    for (int i = 0; i < names.Length; i++)
    {
        names[i] = new StringBuilder().Append(' ', 256).ToString();
    }
    
    ushort errorCode = HLP_GetDeviceNames(names, 256, Convert.ToUInt16(numDev));
    

    I allocate memory for the unamanged code then let the unmanaged code chane the strings there.

    This works but I dont know if I have any potential memory leaks or other potential problems.

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

Sidebar

Related Questions

I am having a really hard time getting this to work. I have two
I have been having a really hard time debugging this error since the past
I'm having a really hard time writing this query and getting it to work
I'm having a really hard time with this one, EDIT: I'm putting this edit
This is pretty simple question, but I'm having really hard time with it. I
I'm having a really hard time finding any examples that are close to what
I am having a really hard time trying to get this to work. All
I'm having a really hard time debugging some LINQ to SQL code because there
I'm having a really hard time getting my head around using REST-style URLS. Can
Got a problem that I'm having a hard time getting started on. Not a

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.