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

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 3867
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: harry | last post by:
I have 2 multi-dim arrays double subTotals = null; String rowTitles = null; I want to pass them to a function that initialises & populates them like so - loadData( rowTitles, subTotals);
2
by: muser | last post by:
How can I pass the parameter " long part_num, into a case statement. Case statement follows the function CheckDigit. i.e. CheckDigit( something, temp1 ); Thank you for your help in advance. ...
4
by: Suddn | last post by:
Help me get my mind around passing string types to a function. I need to have the function modify the string types and get them back. Normaly I would just return the modified string but I need to...
5
by: Jack | last post by:
Hi, I need to pass multple variables in a link in order to go to a asp page with the two varables. The following are the values of the variables using response.write: <%'Response.Write Mypage...
39
by: Mike MacSween | last post by:
Just spent a happy 10 mins trying to understand a function I wrote sometime ago. Then remembered that arguments are passed by reference, by default. Does the fact that this slowed me down...
3
by: Mark | last post by:
Hi From what I understand, you can pass arrays from classic ASP to .NET using interop, but you have to change the type of the.NET parameter to object. This seems to be because classic ASP passes...
0
by: Dave Cullen | last post by:
I have a dll written in VC6 that I need to send arguments to from a call in VB.NET. I'm having trouble passing data arguments between them. So far I've only tried passing strings, and all I get...
61
by: academic | last post by:
When I declare a reference variable I initialize it to Nothing. Now I'm wondering if that best for String variables - is "" better? With Nothing I assume no memory is set aside nor GC'ed But...
4
by: arnuld | last post by:
I am passing an array of struct to a function to print its value. First I am getting Segfaults and weired values. 2nd, is there any elegant way to do this ? /* Learning how to use an array...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.