Groups | Blog | Home
all groups > dotnet framework > february 2006 >

dotnet framework : DllImport Char* and string


Light
2/6/2006 5:24:40 PM
Yes, I do. You may have to set the Char type in the DllImport to
probably Ansi. Try that. If anyone reads this and gets an example for
char** and also for bools in a C++ struct then would like to see an
example of that. I think I know how to do it though.
Curtis
http://www.ghostclip.com
The Premier Help System For Developers
Gnic
2/6/2006 6:48:37 PM
Hi,

I have a (Complied) C Library that needs to be called using c# code, some of
the function require char* parameter(in and out)

Here is the problem,

There is a function that expect an input char* parameter

for example
int AddString(const char* someStr)

And then there is another function to retrieve the value I pass in using the
above function
for example
int GetString(char* rntStr)

In C#

[DllImport(......)]
Int32 AddString(string someStr);

[DllImport(..)]
Int32 GetString(string rntString);

So when I input "aaaa" into someStr and get the return by calling GetString,
the rntString variable only return the first character ("a") and the length
of the string is 1.

Anyone know what's wrong with my code?
It the AssString declaration problem or the GetString declaration problem
(or both)?

thanks

Gasnic

Gnic
2/7/2006 12:00:00 AM
I have tried to declare the function like this

Int32 AddString([MarshalAs(UnmanagedType.AnsiBStr) string someStr)

Int32 GetString([MarshalAs(UnmanagedType.AnsiBStr) ref string rntStr)

But I got the same thing.

I also tried UnmanagedType.LPStr and UnmanagedType.LPTStr, but both doesn't
work.

Actually should I use string as the parameter type?

thanks

Gnic


[quoted text, click to view]

Mattias Sjögren
2/7/2006 12:00:00 AM
[quoted text, click to view]

For output string parameters you should use StringBuilder as the
parameter type.

Also make sure that if you set the CharSet property in the DllImport
attribute, it should be set to Ansi.


Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
James Park
2/7/2006 12:44:58 AM
[quoted text, click to view]

Doesn't C# default to CharSet.Ansi?

AddThis Social Bookmark Button