Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Passing strings to C++ DLL

0 views
Skip to first unread message

Andy Baker

unread,
Jul 2, 2008, 7:17:45 AM7/2/08
to
I am attempting to write a .NET wrapper for a C++ DLL file, but am having
problems with passing strings as parameters. How should I be writing my C#
function call when the C header file is definined as taking a char * as an
argument? For example the C++ header says
SDCERR GetCurrentConfig(DWORD *num, char *name);
I am using Uint for the *num parameter, which returns the correct value
but for *name, I always get back a string of 6 squares. I have tried using
string, StringBuilder and char arrays, and passing by ref and not, but
always get the same result.
Connected to this I am also having problems with passing structures for
C++ functions - the structures contain strings as well. Whenever I call a
function that requires a structure as a parameter, I always get a
NotSupportedException, does this just mean that I have defined my structure
incorrectly? Do I have to initailise the structure beforehand (have tried
this but still didn't work)?
I am using Visual Studio 2005, and if it makes a difference it is for a
Compact Framework device. Thanks in advance for any help.

Andy Baker


pug...@googlemail.com

unread,
Jul 2, 2008, 7:44:08 AM7/2/08
to
Have you tried passing a StringBuilder?

Andy Baker

unread,
Jul 2, 2008, 8:26:42 AM7/2/08
to
Yes, I tried StringBuilder. From what I read on the net, that's what I
thought I should be doing. The code I am using is as follows:-
uint Number = 0;
StringBuilder Name = new StringBuilder(CONFIG_NAME_SZ);
SDCERR Result = GetCurrentConfig(ref Number, Name);
Result contains a valid value, as does Number, but Name just contains
rubbish. If I pass Name with the ref keyword I get a NotSupportedException.
To convert the StringBuilder to a string, I am just using ToString - is this
correct?

Andy Baker

<pug...@googlemail.com> wrote in message
news:5b2bd884-532d-456a...@f63g2000hsf.googlegroups.com...

Pavel Minaev

unread,
Jul 2, 2008, 9:14:26 AM7/2/08
to

Can you please show your P/Invoke declaration for this function?
Perhaps you've forgotten CharSet?
Try this also:

[DllImport(CharSet = CharSet.Ansi)]
SDCERR GetCurrentConfig(ref uint num, StringBuilder name);

KH

unread,
Jul 2, 2008, 9:45:00 AM7/2/08
to
Andy, a couple options:

1) Look at the MarshalAsAttribute which tells .NET how to translate its
unicode string between it and unmanaged code; the attribute is applied to the
argument, here's the example from MSDN:

void MyMethod( [MarshalAs(LPStr)] string s);

2) The other option is to write the wrapper in managed C++; if you go this
route your method calls will look something like this:

void MyMethod(String^ s)
{
IntPtr ptr = Marshal::StringToHGlobalAnsi(str);;

try
{
SDCERR err = GetCurrentConfig( (char*)ptr.ToPointer() );
}
finally
{
if (ptr.ToPointer()) Marshal::FreeHGlobal(ptr);
}
}

HTH - KH

Andy Baker

unread,
Jul 3, 2008, 5:28:20 AM7/3/08
to
My P/Invoke declaration is as follows:
[DllImport("VPSummit.dll", CharSet = CharSet.Unicode"]
public static extern SDCERR GetCurrentConfig(ref unit num, StringBuilder
name);
I have also tried CharSet.Auto - CharSet.Ansi is not a option for the
Compact Framework (I think)

"Pavel Minaev" <int...@gmail.com> wrote in message
news:1f2323cd-7b69-47a4...@w7g2000hsa.googlegroups.com...

Andy Baker

unread,
Jul 3, 2008, 6:24:01 AM7/3/08
to
Thanks for the advice. I have been looking at the MarshalAs attribute in
relation to passing structures, but hadn't tried it in the DllImport
declaration. However, I am obviously doing something wrong as every time I
try it I am getting a NotSupportedException.
As I understand even less about C++ that I do about C# (which isn't
alot!) I don't think the managed C++ route is the one to go down. I think I
need someone more experienced to do this job for me! Thanks again.

Andy Baker

"KH" <K...@discussions.microsoft.com> wrote in message
news:9C5B61AE-FAD6-496B...@microsoft.com...

0 new messages