473,748 Members | 6,418 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 GetCurrentConfi g(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
NotSupportedExc eption, 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 3883
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(C ONFIG_NAME_SZ);
SDCERR Result = GetCurrentConfi g(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 NotSupportedExc eption.
To convert the StringBuilder to a string, I am just using ToString - is this
correct?

Andy Baker

<pu****@googlem ail.comwrote in message
news:5b******** *************** ***********@f63 g2000hsf.google groups.com...
Have you tried passing a StringBuilder?

Jul 2 '08 #3
On Jul 2, 3:17*pm, "Andy Baker" <aba...@NOSPAMv anputer.comwrot e:
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 GetCurrentConfi g(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
NotSupportedExc eption, 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(CharS et = CharSet.Ansi)]
SDCERR GetCurrentConfi g(ref uint num, StringBuilder name);
Jul 2 '08 #4
Andy, a couple options:

1) Look at the MarshalAsAttrib ute 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::String ToHGlobalAnsi(s tr);;

try
{
SDCERR err = GetCurrentConfi g( (char*)ptr.ToPo inter() );
}
finally
{
if (ptr.ToPointer( )) Marshal::FreeHG lobal(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 GetCurrentConfi g(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
NotSupportedExc eption, 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("VPSu mmit.dll", CharSet = CharSet.Unicode "]
public static extern SDCERR GetCurrentConfi g(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.c omwrote in message
news:1f******** *************** ***********@w7g 2000hsa.googleg roups.com...
On Jul 2, 3:17 pm, "Andy Baker" <aba...@NOSPAMv anputer.comwrot e:
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 GetCurrentConfi g(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
NotSupportedExc eption, 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(CharS et = CharSet.Ansi)]
SDCERR GetCurrentConfi g(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 NotSupportedExc eption.
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.comw rote in message
news:9C******** *************** ***********@mic rosoft.com...
Andy, a couple options:

1) Look at the MarshalAsAttrib ute 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::String ToHGlobalAnsi(s tr);;

try
{
SDCERR err = GetCurrentConfi g( (char*)ptr.ToPo inter() );
}
finally
{
if (ptr.ToPointer( )) Marshal::FreeHG lobal(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 GetCurrentConfi g(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
NotSupportedEx ception, 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
12623
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
2003
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. bool CheckDigit(long part_num, char* record) {
4
49243
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 modify about five different strings. I thought that strings were passed by reference in C/C++ but when I pass the strings in they remain unaltered in the calling function. I did test the function being called and they are being althered there.
5
6698
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 & "<br>"%> Exp <%'Response.Write GrantID & "<br>"%>
39
7662
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 indicate: a) That I don't know enough b) Passing arguments by ref is bad
3
3793
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 a variant containing an array, and interop expects a parameter of type object if you are passing a variant (you are expected to cast it to the correct type in your code). I'd like to find a way of passing arrays so that you don't need to change...
0
1301
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 on the receiving end is the first character of each parameter. The function definition in the dll is like this: extern "C" BOOL PASCAL EXPORT MyFunction(char *param1, char *param2)
61
3146
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 with "" it is - correct? The system seems to handle a null the same as "".
4
5305
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 of struct */ #include <stdio.h>
0
8987
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8826
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9534
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9366
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9316
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9241
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8239
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6793
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
2
2777
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.