473,396 Members | 1,834 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,396 software developers and data experts.

HOWTO: passing an [in,out] char* from Managed C++ to Unmanaged C.

I am writing Managed C++ code to call in to an Unmanaged C API.
The reason for using Managed C++ over C# is that the Unmanaged C
module is loaded via LoadLibrary(). DllImport cannot be used;
functions pointers and GetProcAddress() is being used.

The API function definition looks like this:

int GetString( [out] char* pszOutStr, [in,out] int *nStrLen)

It looks like the prototyping of the function pointer is straight
forward:

typedef int (*FPGetString)( char* pszOutStr, int *nStrLen);

The question is how do I get a char* with a given length to pass
into this function. Mind you this IS an Ansi call, not Unicode!

Sam
Nov 17 '05 #1
3 3534
On 2005-07-11, Sam Carleton wrote:
I am writing Managed C++ code to call in to an Unmanaged C API.
The reason for using Managed C++ over C# is that the Unmanaged C
module is loaded via LoadLibrary(). DllImport cannot be used;
functions pointers and GetProcAddress() is being used.

The API function definition looks like this:

int GetString( [out] char* pszOutStr, [in,out] int *nStrLen)

It looks like the prototyping of the function pointer is straight
forward:

typedef int (*FPGetString)( char* pszOutStr, int *nStrLen);

The question is how do I get a char* with a given length to pass
into this function. Mind you this IS an Ansi call, not Unicode!

Sam


Ok, looks like an [out] is simple:

int nStrLen = x;
IntPtr ptrOut = Marshal::AllocHGlobal( nStrLen * sizeof(char));
GetString( (char*) ptrOut.ToPointer(), &nStrLen);
Marshal::FreeCoTaskMem(ptrOut);

This is working fine for me. The problem I am having now is the
[in, out]. There is another method that one of the three
parameters which is an in/out is the username. The size of the
username buffer must the full length possible for a username in
this app. The API call might prompt the user and the user might
change the username, so the new username is returned in the same
string that was passed in. Crasy, but that is what I have to work
with. Here is my best guess, which is blowing up when I call Free
the memory:

StringBuilder * outUser = new StringBuilder( user, USER_LEN+1);
IntPtr ptrUsername = Marshal::StringToHGlobalAnsi(outUser->ToString());
// I don't know if the pin is really needed, but it compiles
char __pin* pszUsername = (char*) ptrUsername.ToPointer();

login( pszUsername, ...);

String * strUser = Marshal::PtrToStringAnsi(ptrUsername);

Marshal::FreeCoTaskMem(ptrUsername);

To tell the truth, the username is freed without any problems, it
is the parameter after it, which is also a [in, out] char* that
blows chunks (unhandled exception) when calling
Marshal::FreeCoTaskMem().

Any thoughts?

Sam
Nov 17 '05 #2
On 2005-07-11, Sam Carleton wrote:
I am writing Managed C++ code to call in to an Unmanaged C API.
The reason for using Managed C++ over C# is that the Unmanaged C
module is loaded via LoadLibrary(). DllImport cannot be used;
functions pointers and GetProcAddress() is being used.

The API function definition looks like this:

int GetString( [out] char* pszOutStr, [in,out] int *nStrLen)

It looks like the prototyping of the function pointer is straight
forward:

typedef int (*FPGetString)( char* pszOutStr, int *nStrLen);

The question is how do I get a char* with a given length to pass
into this function. Mind you this IS an Ansi call, not Unicode!

Sam


Ok, looks like an [out] is simple:

int nStrLen = x;
IntPtr ptrOut = Marshal::AllocHGlobal( nStrLen * sizeof(char));
GetString( (char*) ptrOut.ToPointer(), &nStrLen);
Marshal::FreeCoTaskMem(ptrOut);

This is working fine for me. The problem I am having now is the
[in, out]. There is another method that one of the three
parameters which is an in/out is the username. The size of the
username buffer must the full length possible for a username in
this app. The API call might prompt the user and the user might
change the username, so the new username is returned in the same
string that was passed in. Crasy, but that is what I have to work
with. Here is my best guess, which is blowing up when I call Free
the memory:

StringBuilder * outUser = new StringBuilder( user, USER_LEN+1);
IntPtr ptrUsername = Marshal::StringToHGlobalAnsi(outUser->ToString());
// I don't know if the pin is really needed, but it compiles
char __pin* pszUsername = (char*) ptrUsername.ToPointer();

login( pszUsername, ...);

String * strUser = Marshal::PtrToStringAnsi(ptrUsername);

Marshal::FreeCoTaskMem(ptrUsername);

To tell the truth, the username is freed without any problems, it
is the parameter after it, which is also a [in, out] char* that
blows chunks (unhandled exception) when calling
Marshal::FreeCoTaskMem().

Any thoughts?

Sam
Nov 17 '05 #3
AllocHGlobal should be paired with FreeHGlobal not FreeCoTaskMem!

Willy.

"Sam Carleton" <sc**************@miltonstreet.com> wrote in message
news:e5****************@tornado.ohiordc.rr.com...
On 2005-07-11, Sam Carleton wrote:
I am writing Managed C++ code to call in to an Unmanaged C API.
The reason for using Managed C++ over C# is that the Unmanaged C
module is loaded via LoadLibrary(). DllImport cannot be used;
functions pointers and GetProcAddress() is being used.

The API function definition looks like this:

int GetString( [out] char* pszOutStr, [in,out] int *nStrLen)

It looks like the prototyping of the function pointer is straight
forward:

typedef int (*FPGetString)( char* pszOutStr, int *nStrLen);

The question is how do I get a char* with a given length to pass
into this function. Mind you this IS an Ansi call, not Unicode!

Sam


Ok, looks like an [out] is simple:

int nStrLen = x;
IntPtr ptrOut = Marshal::AllocHGlobal( nStrLen * sizeof(char));
GetString( (char*) ptrOut.ToPointer(), &nStrLen);
Marshal::FreeCoTaskMem(ptrOut);

This is working fine for me. The problem I am having now is the
[in, out]. There is another method that one of the three
parameters which is an in/out is the username. The size of the
username buffer must the full length possible for a username in
this app. The API call might prompt the user and the user might
change the username, so the new username is returned in the same
string that was passed in. Crasy, but that is what I have to work
with. Here is my best guess, which is blowing up when I call Free
the memory:

StringBuilder * outUser = new StringBuilder( user, USER_LEN+1);
IntPtr ptrUsername = Marshal::StringToHGlobalAnsi(outUser->ToString());
// I don't know if the pin is really needed, but it compiles
char __pin* pszUsername = (char*) ptrUsername.ToPointer();

login( pszUsername, ...);

String * strUser = Marshal::PtrToStringAnsi(ptrUsername);

Marshal::FreeCoTaskMem(ptrUsername);

To tell the truth, the username is freed without any problems, it
is the parameter after it, which is also a [in, out] char* that
blows chunks (unhandled exception) when calling
Marshal::FreeCoTaskMem().

Any thoughts?

Sam

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: lolomgwtf | last post by:
I have a managed C++ method that wraps unmanaged code and creates a managed object holding data retrieved form an unmanged one. I want create an instance of this managed class in C#, pass it to...
2
by: Chris | last post by:
Hi, I seem to be having a problem I can't quite figure out. Currently, I have the following code (below). In the SendMsg function, I create a 'Qtkmsg' which converts the string 'text' into a...
2
by: lolomgwtf | last post by:
I have a managed C++ method that wraps unmanaged code and creates a managed object holding data retrieved form an unmanged one. I want create an instance of this managed class in C#, pass it to...
0
by: Ken Durden | last post by:
A little more info. I have a unmanaged C++ DLL, compiled without /CLR. Inside a Managed C++ DLL, I have the following class: MarshalWrapper.h ---- #ifdef _MANAGED __nogc #endif
0
by: Sam Carleton | last post by:
I am writing Managed C++ code to call in to an Unmanaged C API. The reason for using Managed C++ over C# is that the Unmanaged C module is loaded via LoadLibrary(). DllImport cannot be used;...
17
by: mr.resistor | last post by:
hey i am having a few problems calling a C DLL from C#. i am using a simple function that takes an array of floats and an integer as an input, but i cannot seem to get it to work. when i try to...
12
by: Haxan | last post by:
Hi, I have my main application class(unmanaged) that has a none static member function that I need to pass as a delegate to managed C# method. In one of the methods of this class(unmamanged),...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
6
by: Andy Baker | last post by:
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
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
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...
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,...

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.