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 3 3543
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::AllocH Global( nStrLen * sizeof(char));
GetString( (char*) ptrOut.ToPointe r(), &nStrLen);
Marshal::FreeCo TaskMem(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::String ToHGlobalAnsi(o utUser->ToString());
// I don't know if the pin is really needed, but it compiles
char __pin* pszUsername = (char*) ptrUsername.ToP ointer();
login( pszUsername, ...);
String * strUser = Marshal::PtrToS tringAnsi(ptrUs ername);
Marshal::FreeCo TaskMem(ptrUser name);
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::FreeCo TaskMem().
Any thoughts?
Sam
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::AllocH Global( nStrLen * sizeof(char));
GetString( (char*) ptrOut.ToPointe r(), &nStrLen);
Marshal::FreeCo TaskMem(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::String ToHGlobalAnsi(o utUser->ToString());
// I don't know if the pin is really needed, but it compiles
char __pin* pszUsername = (char*) ptrUsername.ToP ointer();
login( pszUsername, ...);
String * strUser = Marshal::PtrToS tringAnsi(ptrUs ername);
Marshal::FreeCo TaskMem(ptrUser name);
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::FreeCo TaskMem().
Any thoughts?
Sam
AllocHGlobal should be paired with FreeHGlobal not FreeCoTaskMem!
Willy.
"Sam Carleton" <sc************ **@miltonstreet .com> wrote in message
news:e5******** ********@tornad o.ohiordc.rr.co m... 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::AllocH Global( nStrLen * sizeof(char)); GetString( (char*) ptrOut.ToPointe r(), &nStrLen); Marshal::FreeCo TaskMem(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::String ToHGlobalAnsi(o utUser->ToString()); // I don't know if the pin is really needed, but it compiles char __pin* pszUsername = (char*) ptrUsername.ToP ointer();
login( pszUsername, ...);
String * strUser = Marshal::PtrToS tringAnsi(ptrUs ername);
Marshal::FreeCo TaskMem(ptrUser name);
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::FreeCo TaskMem().
Any thoughts?
Sam This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 this method
and have it set the instance to hold the right data.
>From what I've read it seems I should be able to pass C# objects to
managed C++ methods and it should just work; however, when I try it, my
C# instance comes out null. If I step...
|
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 null terminated character array that
is sent through the DllImport interface 'QtkSendMesg'. However, when I
receive this struct in the C++ DLL, the character array that I passed to it
is filled with random characters. What am I doing wrong when...
|
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 this method
and have it set the instance to hold the right data.
>From what I've read it seems I should be able to pass C# objects to
managed C++ methods and it should just work; however, when I try it, my
C# instance comes out null. If I step...
|
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
|
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;
functions pointers and GetProcAddress() is being used.
The API function definition looks like this:
int GetString( char* pszOutStr, int *nStrLen)
It looks like the prototyping of the function pointer is straight
| |
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 compile i get the
following error:
Attempted to read or write protected memory
the C function should not be manipulating the input arra, only reading
|
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), I am calling a managed C#
method(I use gcnew to instantiate the managed class). One of the parameters
of this C# method is a delegate. I need to pass the none static member
function as a delegate(function pointer) as a parameter in the managed C#...
|
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 to show the array data to the end user.
Can I do that?
How?
|
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 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....
|
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,...
|
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...
| |
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...
|
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,...
|
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...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| | |