473,394 Members | 1,865 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,394 software developers and data experts.

DLL Marshalling : How do I treat a char FAR * HUGE * FAR * in C# ?

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 XcDatabaseCodes
(
HWND hwnd,
char FAR * pszDatabase,
char FAR * HUGE * FAR * pszItemCategories
)

The char FAR * HUGE * FAR * parameter is the address to return huge array of
null terminated strings i.e. a list

Here is my C# marshalling definition for this API :

[DllImport("Xenoc32.dll")]
public static extern int XcDatabaseCodes(
string hWindowHandle ,
string pszDatabase,
ref string [] pszItemCategories );

and here is my API call from C#

const string pszDatabase = "\\\\NETWORK\\DATABASE";
string [] pszItemCategories = {};
int shReturnCode = XcDatabaseCodes(
null,
pszDatabase,
ref pszItemCategories );

The call returns ok but the trouble is I only get one item back.

How should I declare and pass pszItemCategories to get the list.

Nov 17 '05 #1
1 2668
Reynardine,

The P/Invoke layer doesn't know how to marsal arrays back to the managed
world. The reason for this is that it doesn't know what the size of the
array is (after all, it's only a pointer).

Because of this, you will have to marshal it as an IntPtr (or a ref
IntPtr) and then dereference the data yourself (you can use the methods on
the Marshal class to do this).

Also, are you supplying the memory where the strings are written to? If
not, then you need to make sure you dispose of the memory allocated by the
function correctly (whomever wrote the API should have provided a function
to do so).

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Reynardine" <Re********@discussions.microsoft.com> wrote in message
news:E2**********************************@microsof t.com...
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 XcDatabaseCodes
(
HWND hwnd,
char FAR * pszDatabase,
char FAR * HUGE * FAR * pszItemCategories
)

The char FAR * HUGE * FAR * parameter is the address to return huge array
of
null terminated strings i.e. a list

Here is my C# marshalling definition for this API :

[DllImport("Xenoc32.dll")]
public static extern int XcDatabaseCodes(
string hWindowHandle ,
string pszDatabase,
ref string [] pszItemCategories );

and here is my API call from C#

const string pszDatabase = "\\\\NETWORK\\DATABASE";
string [] pszItemCategories = {};
int shReturnCode = XcDatabaseCodes(
null,
pszDatabase,
ref pszItemCategories );

The call returns ok but the trouble is I only get one item back.

How should I declare and pass pszItemCategories to get the list.

Nov 17 '05 #2

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

Similar topics

4
by: Animesh | last post by:
Hi All, I don't know whethher this is possible or not. This is the result of a bad design problem. Here I go; I have a structure like this: typedef struct _s_index_entry { char *doc_id;...
1
by: halise | last post by:
hello, i am trying to wrap an unmanaged C++ code to managed one so that i can use it within c#, and i am using IJW - "It Just Works" - method for that. In the code, there is a function which...
1
by: Betty | last post by:
When marshalling a char array in a structure from C to .NET, I get the following error: System.ArgumentException: Type could not be marshaled because the length of an embedded array instance does...
3
by: Simon | last post by:
Hi all, I am trying to call a function from a dll and it has the following prototype. int foo(int *argc, char ***argv) or int foo(int *argc, char *argv) I am having problems that I assume...
1
by: | last post by:
Hi, Is there any good links for datatype interop? I need to pass some structure pointers into an unmanaged method and return char* etc but having some problems in my C++/CLI proxy class. I...
2
by: BartMan | last post by:
Greetings, When working with managed c++, do you have to do anything special when going from simple types from managed to unmanaged and vice versa. Or is marshalling handled automatically for...
3
by: awk | last post by:
Hi All I have a com dll written in VB6 (it's a User Function Library for my crystal reports - this allows me to write custom functions for Crystal which can be applied in Crystal formulas - none...
2
by: RJ Lohan | last post by:
Howdy, I have a legacy DLL for which I have a problem marshalling a parameter type of char**. The function header (C++) is as so; extern "C" __declspec(dllexport) int __stdcall...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.