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

Pinvoke question: importing char* returning functions.

I have to call a c++ library funtion returning a string with the
following signature:
char *get_identifier();

Usually when I have to marshal a function with a char* output
parameter I do:
[DllImport("library.dll", CharSet = CharSet.Ansi,)]
static extern int get_identifier2([MarshalAs(UnmanagedType.LPStr)]
StringBuilder Ack_Msg, int msg_len);

But in this case the char* string is returned as output.
I've tried
[DllImport("library.dll", CharSet = CharSet.Ansi,)]
static extern [MarshalAs(UnmanagedType.LPStr)] StringBuilder
get_identifier();
but this does throw an error.

How I could do to state explicitly that the returning char string is
an ansi (LPStr) string?

And how could I find the size of the returned string?

Thanks.

Jun 5 '07 #1
2 4928

"Beorne" <ma*******@gmail.comwrote in message
news:11**********************@k79g2000hse.googlegr oups.com...
>I have to call a c++ library funtion returning a string with the
following signature:
char *get_identifier();

Usually when I have to marshal a function with a char* output
parameter I do:
[DllImport("library.dll", CharSet = CharSet.Ansi,)]
static extern int get_identifier2([MarshalAs(UnmanagedType.LPStr)]
StringBuilder Ack_Msg, int msg_len);

But in this case the char* string is returned as output.
I've tried
[DllImport("library.dll", CharSet = CharSet.Ansi,)]
static extern [MarshalAs(UnmanagedType.LPStr)] StringBuilder
get_identifier();
but this does throw an error.
You have to specify [return: ...] in your attribute I believe, but continue
reading.
>
How I could do to state explicitly that the returning char string is
an ansi (LPStr) string?
IMO, you *must* use pointers to deal with this. My preference is C++/CLI of
course, but C# should work too. Declare the return value as an IntPtr, use
the Marshal class to retrieve your data. P/invoke's built-in marshalling
won't work, because it doesn't give you access to the pointer, which you
need to pass to the appropriate deallocation function after you are done
using it.
>
And how could I find the size of the returned string?
lstrlen, from kernel32.dll, for example
http://www.pinvoke.net/default.aspx/...2/lstrlen.html
>
Thanks.

Jun 5 '07 #2
"Beorne" <ma*******@gmail.comwrote in message
news:11**********************@k79g2000hse.googlegr oups.com...
>I have to call a c++ library funtion returning a string with the
following signature:
char *get_identifier();

Usually when I have to marshal a function with a char* output
parameter I do:
[DllImport("library.dll", CharSet = CharSet.Ansi,)]
static extern int get_identifier2([MarshalAs(UnmanagedType.LPStr)]
StringBuilder Ack_Msg, int msg_len);

But in this case the char* string is returned as output.
I've tried
[DllImport("library.dll", CharSet = CharSet.Ansi,)]
static extern [MarshalAs(UnmanagedType.LPStr)] StringBuilder
get_identifier();
but this does throw an error.

How I could do to state explicitly that the returning char string is
an ansi (LPStr) string?

And how could I find the size of the returned string?

Thanks.


Declare the return type as IntPtr in C#.
The char* must point to a 'null' terminated char sequence, so there is no
need to know the length, the interop layer will copy the char array up to
and possibly including the terminating 'null' character when marshaling.
You have to pass the returned pointer to Marshal.PtrToStringAnsi in order to
marshal the char array to a managed string.

IntPtr ptr = F();
string s = Marshal.PtrToStringAnsi(ptr);

Willy.

Jun 5 '07 #3

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

Similar topics

4
by: Asif | last post by:
Hi there, I have been trying to understand the behaviour of char (*pfn)(null) for a couple of days. can some body help me understand the behaviour of char (*pfn)(null) in Visual C++ environment?...
11
by: kazack | last post by:
I am under the the impression that a variable is what is stored in a memory address and a pointer is the memory address of the variable? I was looking to use a function that does not return a...
5
by: Carlos Guzmán Álvarez | last post by:
Hello: I'm trying to execute a function of a unmanaged dll using PInvoke, i have definied the function as: public static extern int isc_dsql_prepare( int status_vector, ref int...
3
by: John Smith | last post by:
I wrote some code in C in a dll which I would like to call from C#. However I'm stuck because of the strongly typed behavior of C# which makes limitations. Here are the prototypes for two...
5
by: Stefan Buchman | last post by:
Hi All, I'm attempting to use ldap_explode_dn API call which returns a character array. I've been trying to Marshal this value but am unable to get anything intelligible from it. I should...
2
by: brian_harris | last post by:
I have a 3rd party unmanged .dll. This has functions that will look up information and fill a C string passed to it with data. I want to use this data in .NET classes after it has been filled and...
10
by: ptq2238 | last post by:
Hi, Tried this code to assist my understanding of strings and functions but I'm not sure why the errors are occurring and hope someone can shed some light to my learning. #include <stdio.h>...
1
by: eastlands | last post by:
I need to use an unmanaged c++ dll which uses structs that contain callbacks and also functions. I have included the appropriate c++ definitials and my c# translations below. I first defined the...
5
by: =?Utf-8?B?SmVzc2ljYQ==?= | last post by:
Hello, I have a pInvoke question. This is the C function that is exported from one of the C dll, extern __declspec(dllexport) IM_RET_CODE ST_import (IM_MODE mode, char *filename,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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:
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
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...

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.