473,666 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("libr ary.dll", CharSet = CharSet.Ansi,)]
static extern int get_identifier2 ([MarshalAs(Unman agedType.LPStr)]
StringBuilder Ack_Msg, int msg_len);

But in this case the char* string is returned as output.
I've tried
[DllImport("libr ary.dll", CharSet = CharSet.Ansi,)]
static extern [MarshalAs(Unman agedType.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 4945

"Beorne" <ma*******@gmai l.comwrote in message
news:11******** **************@ k79g2000hse.goo glegroups.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("libr ary.dll", CharSet = CharSet.Ansi,)]
static extern int get_identifier2 ([MarshalAs(Unman agedType.LPStr)]
StringBuilder Ack_Msg, int msg_len);

But in this case the char* string is returned as output.
I've tried
[DllImport("libr ary.dll", CharSet = CharSet.Ansi,)]
static extern [MarshalAs(Unman agedType.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*******@gmai l.comwrote in message
news:11******** **************@ k79g2000hse.goo glegroups.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("libr ary.dll", CharSet = CharSet.Ansi,)]
static extern int get_identifier2 ([MarshalAs(Unman agedType.LPStr)]
StringBuilder Ack_Msg, int msg_len);

But in this case the char* string is returned as output.
I've tried
[DllImport("libr ary.dll", CharSet = CharSet.Ansi,)]
static extern [MarshalAs(Unman agedType.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.PtrToSt ringAnsi in order to
marshal the char array to a managed string.

IntPtr ptr = F();
string s = Marshal.PtrToSt ringAnsi(ptr);

Willy.

Jun 5 '07 #3

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

Similar topics

4
2797
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? The question is why this is legal char *ptr; char (*pfn)(null); ptr = pfn
11
2564
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 value void whatever(whatever) and not have a variable global but in main and change the value in the void whatever function. using namespace std; void whatever(int);
5
2518
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 trans_handle, ref int stmt_handle,
3
2670
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 functions which I have trouble mapping: int _SetOption(int nOption, void *pSetting); void *_GetDataField(int nType, int *npLength);
5
3689
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 be getting an array of strings representing a DN.
2
1880
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 I am programming in C++. I believe that I should use pinvoke, but I see a problem that I can't figure how to get around. I don't think I can declare the string I want as a String class since when I pass it to unmanged function it is not yet...
10
4602
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> #include <string.h> #include <ctype.h> #define STRINGSIZE 10
1
6640
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 parameters as c# structs, but when I tried the call (to RtcInitialize) I got the following error: MarshalDirectiveException Method's type signature is not PInvoke compatible. I then changed the structs to classes but now I get the following...
5
1787
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, ST_TYPES **st_type, ST_ERROR **st_error);
0
8440
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8355
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8638
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7381
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6191
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4365
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2769
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1769
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.