473,243 Members | 1,613 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,243 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 3858
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: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.