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 9212833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:36:54+00:00 2026-06-18T01:36:54+00:00

I am trying to call a Win32 DLL from some C#. The DLL function

  • 0

I am trying to call a Win32 DLL from some C#. The DLL function is declared as follows:

extern "C" __declspec(dllexport) UINT foo(TCHAR** list[], int& listSize, TCHAR* error);

In C#, I declare the external function like so:

[DllImport("foo.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Auto)]
static extern uint foo(StringBuilder[] list, ref int listSize, StringBuilder error);

My problem has to do with the list parameter, which is an array of StringBuilder classes.

Some test code where I call the DLL function is as follows:
(Note: I realise that there are boundary checks missing, etc. At this stage, I am just trying to get things working).

// allocate a buffer for a possible error message being returned
StringBuilder error = new StringBuilder(1000);

// allocate buffers for the list of strings returned
StringBuilder[] list = new StringBuilder[10];
for (int i = 0; i < 10; i++)
{
    list[i] = new StringBuilder(1000);
}

int listSize = list.Length;

uint result = foo(list, ref listSize, error);

Passing the StringBuilder as a buffer for the error parameter, works fine. I can set its contents in the DLL as follows:

_tcscpy(error, _T("Something went wrong");

When the C# call to foo returns, the string error variable has the text we set it to in the DLL.

But, I cannot work out how to marshal the StringBuilder[] list parameter.

In the C++, I am able to set the contents of the list array:

_tcscpy((*list)[0], _T("First item")); 
// and so on...

And, even though there is no crash that occurrs due to any possible problematic marshalling, the list in the C#, when the call to foo() returns, does not contain any of the strings we set in the C++.

I’m guessing that the problem is with the DllImport and how I’ve declared the list parameter should be marshalled.

By the way, the example DLL has a matching function which one would then call to deallocate the “TCHAR** list” returned by the example foo shown above.

  • 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-18T01:36:55+00:00Added an answer on June 18, 2026 at 1:36 am

    OK, I have found a solution to my question. It looks like the answer lay in IntPtr. The C function created and returned an array of string in “TCHAR** list[]”.

    First, I changed the declaration in C# to:

    [DllImport("foo.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Auto)]
    static extern uint foo(out IntPtr[] list, ref int listSize, StringBuilder error);
    

    Then, when the call to the DLL returned, I needed to extract the strings from the array:

    string[] returnedList = new string[listSize];
    IntPtr[] ptrArray = new IntPtr[listSize];
    
    Marshal.Copy(list, ptrArray, 0, listSize);
    
    for (int i = 0; i < listSize; i++)
    {
        returnedList[i] = Marshal.PtrToStringAuto(ptrArray[i]);
    }
    

    And that resulted in getting back the array of strings from the C DLL.

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

Sidebar

Related Questions

Hi i'm trying to call the CreateFileA win32 function from a pyx file (cython
I'm trying to call a function present in one class from another class by
I am trying to call c# function from javascript the code i have tried
I'm trying to call CoCreateInstance(...) via a 64-bit Java library: org.eclipse.swt.internal.ole.win32.COM. The DLL I'm
I'm trying to call the Win32 version of SetWindowPos from within an ATL class
I am trying to call the PrintDlgW Win32 API from C# via P/Invoke. With
I'm trying to draw vertical text in win32 GDI api. I call CreateFont() with
Trying to call a SAP SOAP Web Service from a generated sudzc app shows
Im trying to call a method that will add or subtract 1 from a
I am trying to call some functions whenever the mouse is moved wherever it

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.