468,720 Members | 1,585 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,720 developers. It's quick & easy.

Passing strings to C++ DLL

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

Jul 2 '08 #1
6 3590
Have you tried passing a StringBuilder?
Jul 2 '08 #2
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

<pu****@googlemail.comwrote in message
news:5b**********************************@f63g2000 hsf.googlegroups.com...
Have you tried passing a StringBuilder?

Jul 2 '08 #3
On Jul 2, 3:17*pm, "Andy Baker" <aba...@NOSPAMvanputer.comwrote:
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.
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);
Jul 2 '08 #4
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" wrote:
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

Jul 2 '08 #5
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" <in****@gmail.comwrote in message
news:1f**********************************@w7g2000h sa.googlegroups.com...
On Jul 2, 3:17 pm, "Andy Baker" <aba...@NOSPAMvanputer.comwrote:
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.
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);
Jul 3 '08 #6
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" <KH@discussions.microsoft.comwrote in message
news:9C**********************************@microsof t.com...
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" wrote:
>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


Jul 3 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by harry | last post: by
2 posts views Thread by muser | last post: by
4 posts views Thread by Suddn | last post: by
reply views Thread by Dave Cullen | last post: by
4 posts views Thread by arnuld | last post: by
1 post views Thread by Oskars | last post: by
9 posts views Thread by bryonone | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.