473,402 Members | 2,064 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Marshalling an array of strings allocated in C to C#

Rob
I have a function exposed from a C DLL which takes a pointer to an
array of strings as a param which then allocates that array of strings
and returns.

I'm using P/Invoke to call this function from C#, but I've been messing
with it for too long now and haven't got it working exactly as I'd
like.

Here's the C function prototype, which will fill in its parameters:
void GetStrings(LPTSTR **aStrings,int& iStrings);

And here's the C# version:
(Note that this is just the latest one I've been messing with - I've
tried dozens of combinations of different types - string[],
StringBuilder[], IntPtr[], with and without MarshalAs attributes...)
[DllImport("SomeDLL.dll")]
private static extern void GetStrings(ref IntPtr aStrings,ref int
iStrings);

So with this latest iteration I can get it to work, but it's not
'right'. Here's how I'm doing it:
int iStrings= 0;
IntPtr aStrings= IntPtr.Zero;
GetNames(ref aStrings,ref iStrings);
for (int index = 0;index < iStrings;++index)
{
IntPtr ptr = Marshal.ReadIntPtr(aStrings,4 * index);
string returnedString = Marshal.PtrToStringAuto(ptr);
}

Notice the hideous way of reading the IntPtrs using a hard-coded
offset?

The reason I've resorted to this is because all attempts at returning
an actual array of any type have failed. Well, not flat-out failed,
but they always return an array with exactly one element. I've seen it
written in other places that this is because the CLR has no way of
knowing how much data to marshal, so it just does the first piece.

So my question is, how can I do this a bit more correctly? How can I
return an actual array and have it come back with more than one
element?

At this point I'd actually even settle for a method that's more safe
than the one above, which feels very very hackish to me, so I'm
thinking surely there's a 'better' way.

Any help is appreciated,
Thanks.

Nov 29 '05 #1
2 1937
>At this point I'd actually even settle for a method that's more safe
than the one above, which feels very very hackish to me, so I'm
thinking surely there's a 'better' way.


There's nothing terribly wrong with your current solution. The only
change I's suggest is to replace the hardcoded 4 with IntPtr.Size.

In .NET 2.0 there's also a new Marshal.Copy overload that would let
you retrieve the entire array as an IntPtr[] in one call.

Other than that, I don't think you can make it much better without
using unsafe code.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 29 '05 #2
Rob
Thanks Mattias, if this is the best approach there seems to be then I'm
happy with it. Replacing the 4 with IntPtr.Size makes me feel better
about this method, thanks for pointing that one out.

Dec 1 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where...
8
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
4
by: Simon Mansfield | last post by:
Hey everyone, I have created a struct called employee, as follows: struct employee { int pid; char name; char job; int hourlyRate; struct employee *next; }
1
by: Reynardine | last post by:
I am calling a C/C++ DLL from C# and I am marshalling the parameters to the API call by doing a type conversion for each parameter. For example, here is my C++ API method : short int XENO_API...
3
by: David Mathog | last post by:
This one is driving me slightly batty. The code in question is buried deep in somebody else's massive package but it boils down to this, two pointers are declared, the first is: char **resname...
6
by: alex323 | last post by:
Hey everybody. I am coding a function that takes a string as an argument (char *msg) and breaks it up into pieces. The result will be stored in (char ***fragments) which is passed as an argument....
6
by: Ananas | last post by:
Hi, My native C++ function creates a dynamic array. I'm marshalling it to managed code and got to delete after. How to make it: c++ code: void CreateArrayInside( pTestStruct &TestStruct,...
1
by: Keaven Pineau | last post by:
Hello All, I am very new to C# since my job consist to design/develop CE drivers but a client wants to use my C API into a .NET envrironment. I have 2 functions I would like to get help with. ...
2
by: calenlas | last post by:
Hi all, I'm taking my first steps into C# <--C++ DLL Interop and unfortunately I've run into (what seems to be) a very complicated case as my first task. Perhaps someone here can help me. I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.