473,385 Members | 1,958 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,385 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 3533
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...
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,...

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.