DllIport and char**

  • Thread starter news.broadpark.no
  • Start date
N

news.broadpark.no

I have a problem interfacing a DLL written in Ada. In C++ I do as follows:

typedef void(*pfGetItems)( char** ppsc, int num );

.....

pfGetItems GetItems = (pfGetItems)::GetProcAddress(hModule, "GetItems");


next I create the char array:

char** pszBuf=new char*[num];

for (int i=0; i<num; i++)

pszBuf=new char[100];



next I call the function and the result is stored in pszBuf. I have trouble
doing this in C#. I have tried the following:



[DllImport(@"Te.dll")]

private static extern void GetItems(ref StringBuilder[]Items, int num);



But, I can't make it work. Any tips on how to do this?



Best regards,

Eirik
 
J

Jeroen Mostert

news.broadpark.no said:
I have a problem interfacing a DLL written in Ada.

Ada? That's your problem right there!

Sorry, too cheap.
In C++ I do as follows:

typedef void(*pfGetItems)( char** ppsc, int num );
The calling convention is not clear. If this is really the declaration, that
means the function uses the C calling convention. (Most exported functions
in Windows use the stdcall calling convention.)
....

pfGetItems GetItems = (pfGetItems)::GetProcAddress(hModule, "GetItems");


next I create the char array:

char** pszBuf=new char*[num];

for (int i=0; i<num; i++)

pszBuf=new char[100];



next I call the function and the result is stored in pszBuf. I have
trouble doing this in C#. I have tried the following:



[DllImport(@"Te.dll")]

private static extern void GetItems(ref StringBuilder[]Items, int num);

It's a little confusing, but StringBuilder only works for single strings.
This should work, if what you've shown so far is accurate:

[DllImport("Te.dll", CallingConvention = CallingConvention.Cdecl, CharSet =
CharSet.Ansi)]
static extern void GetItems([In, Out] string[] items, int num);

You call it like this:

string[] items = new string[<capacity>];
for (int i = 0; i != <capacity>, ++i) {
items = new string('\0', 100);
}
GetItems(items, items.Length);
/* use items here */

It's important that the strings be initialized to a sufficient number of
characters, otherwise GetItems() will overfill. If you need to pass values
in, make sure to pad those to the expected size.
 
N

news.broadpark.no

Jeroen Mostert said:
Ada? That's your problem right there!

He he.....I agree with you, but we're talking heavy legacy here...
..... It's a little confusing, but StringBuilder only works for single
strings. This should work, if what you've shown so far is accurate:

[DllImport("Te.dll", CallingConvention = CallingConvention.Cdecl, CharSet
= CharSet.Ansi)]
static extern void GetItems([In, Out] string[] items, int num);

You call it like this:

string[] items = new string[<capacity>];
for (int i = 0; i != <capacity>, ++i) {
items = new string('\0', 100);
}
GetItems(items, items.Length);
/* use items here */
...


Works fine. Thank you!!!

Eirik
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top