473,408 Members | 2,888 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,408 software developers and data experts.

Dll returning null terminated string, how to handle this ?

Hi,

I have a DLL using stdcall and a function of it is returning a null
terminated string. How do I declare this in C# (VS2005) ?

I try things like:

[DllImport(dllName)]
public static extern [MarshalAs(UnmanagedType.LPStr)] string
Version();

But keep getting syntax errors, so clearly I miss something. I also tryed
char[] but this give me exception error.

--
rgds, Wilfried
http://www.mestdagh.biz
Sep 25 '06 #1
5 3025
Hi,

I tryed another approach, to just copy something to a string as argument,
but have also strange result.

char[] ver = new char[21];
ApiDll.Version(ver, 20);

And the dll:

public static class ApiDll
{
[DllImport(dllName)]
public static extern void Version([MarshalAs(UnmanagedType.LPArray)]
char[] c, int len);

In the DLL I see the value given with the call. There I copy the string to
the pointer, but in C# the result value is the same. It seems that a copy is
passed to the dll instead of the pointer.

In the DLL (in Delphi):

procedure Version(c: PChar; Len: integer): PChar; export; stdcall;
begin
c := StrPLCopy(c, 'hihihihi', 10);
end;

But the "hihihi" never visisble in C# calling program.

I tryed with the 'out' argument, but this gives me exception errors. So I
think I have to go back to the return value. But dont kno whow..;.

--
rgds, Wilfried
http://www.mestdagh.biz
Sep 25 '06 #2
A "return" pointer must be declared as an IntPtr like this:

public static extern IntPtr Version();

and the IntPtr must be explicitely marshaled by using
Marshal.PtrToStringXXX, where XXX depends on type of string (ansi or
unicode).

Willy.
"Wilfried Mestdagh" <Wi**************@discussions.microsoft.comwrote in
message news:C6**********************************@microsof t.com...
| Hi,
|
| I have a DLL using stdcall and a function of it is returning a null
| terminated string. How do I declare this in C# (VS2005) ?
|
| I try things like:
|
| [DllImport(dllName)]
| public static extern [MarshalAs(UnmanagedType.LPStr)] string
| Version();
|
| But keep getting syntax errors, so clearly I miss something. I also tryed
| char[] but this give me exception error.
|
| --
| rgds, Wilfried
| http://www.mestdagh.biz
Sep 25 '06 #3
How does this relate to your previous question?, this looks like a different
function.
Maybe it's time to read the Pinvoke stuff in MSDN, and to to take a look at
the samples too.

Willy.

"Wilfried Mestdagh" <Wi**************@discussions.microsoft.comwrote in
message news:72**********************************@microsof t.com...
| Hi,
|
| I tryed another approach, to just copy something to a string as argument,
| but have also strange result.
|
| char[] ver = new char[21];
| ApiDll.Version(ver, 20);
|
| And the dll:
|
| public static class ApiDll
| {
| [DllImport(dllName)]
| public static extern void
Version([MarshalAs(UnmanagedType.LPArray)]
| char[] c, int len);
|
| In the DLL I see the value given with the call. There I copy the string to
| the pointer, but in C# the result value is the same. It seems that a copy
is
| passed to the dll instead of the pointer.
|
| In the DLL (in Delphi):
|
| procedure Version(c: PChar; Len: integer): PChar; export; stdcall;
| begin
| c := StrPLCopy(c, 'hihihihi', 10);
| end;
|
| But the "hihihi" never visisble in C# calling program.
|
| I tryed with the 'out' argument, but this gives me exception errors. So I
| think I have to go back to the return value. But dont kno whow..;.
|
| --
| rgds, Wilfried
| http://www.mestdagh.biz
Sep 25 '06 #4
Hi,

Thank you it works. My second question was because I tryed another way to
get forward. It works but I wonder wy the original value is not changed.
Where have I written to ? I try to explain:

This is my call:
public string version()
{
char[] ver = new char[21];
string v = Marshal.PtrToStringAnsi(ApiDll.Version(ver, 20));
return v + " | " + new string(ver);
}

in v is copied the version witch is copy to it in the DLL. But original
value 'ver' is empty. But both point to same memory location. So where has
code in dll written to ?

[DllImport(dllName)]
public static extern IntPtr
Version([MarshalAs(UnmanagedType.LPArray)] char[] c, int len);

And code in DLL is simple:

function Version(c: PChar; Len: integer): PChar; export; stdcall;
begin
c := StrPLCopy(c, VersionInfo, Len);
Result := c;
end;

c is pointer same pointer as ver in C# calling program. Result is return
value and is same pointer. It is the IntPtr in C# calling program.

But in C# calling program ver is empty. So to witch pointer has DLL copy the
string ?

--
rgds, Wilfried
http://www.mestdagh.biz
Sep 25 '06 #5
Hi,

I found it. I declared a char[] where it has to be a byte[]. I still get
confused with these 16 bit chars...

now v and ver are same string and second question is solved too :)

--
rgds, Wilfried
http://www.mestdagh.biz
Sep 25 '06 #6

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

Similar topics

9
by: Bryan Bullard | last post by:
#include <cstring> #include <iostream> #include <string> std::string foo() { char buf; strcpy(buf, "this is a test.");
11
by: Justin Naidl | last post by:
class Foo { protected: char foo_stuff; public: char* get_foo_stuff(); } Given the above example. What I want to know is the "proper/standard" way
19
by: Rick | last post by:
Hi, I was wondering, can it be safely assumed that any function that returns a *char will return a NULL terminated string? Or does the function explicitly mention this always (and the ones that...
4
by: Barry | last post by:
Hi all! I support a rather large production EDI application with a number of C programs, and I ran across a very interesting problem. I have some code that used to work just fine for years, and...
35
by: David Mathog | last post by:
Every so often one of my fgets() based programs encounters an input file containing embedded nulls. fgets is happy to read these but the embedded nulls subsequently cause problems elsewhere in...
9
by: kiran.agashe | last post by:
Hi, Please refer program below: #include <string> #include <cstdio> using namespace std; const char* f(); main() {
12
by: semut | last post by:
Given that the string is of null terminated type. What could be the possible causes (by experience) the string to have no null terminated and cause buffer overflow later. I know it is quite broad,...
2
by: Beorne | last post by:
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: ...
13
by: Logan Lee | last post by:
Hi. I've written a small program to learn to write in C. But unfortunately the output is all jumbled up and not nice. /* read_file.c The whole point of this code is to read the entire content...
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: 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
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
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
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,...
0
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...

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.