473,396 Members | 2,011 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.

URGENT: Local variables dissapear after unmanaged function call

Right after I call a function in an external dll, some of my local
variables, not used in the call (!!!) in any way, are 'null' right
after the call.

(Pseudo)code

[Dllimport ...]
public static ... calc(...)

StringBuilder sB = new StringBuilder("hello");
String tmp = "I'm on my own";

calc(ref tmp);
// sB = null here!

What's happening here!?

Jun 21 '07 #1
8 1281
Most probably you are using wrong CallingConvention - see description of
this enumeration in MSDN or SDK.

"Papa.Coen" <pa*******@gmail.comwrote in message
news:11*********************@n2g2000hse.googlegrou ps.com...
Right after I call a function in an external dll, some of my local
variables, not used in the call (!!!) in any way, are 'null' right
after the call.

(Pseudo)code

[Dllimport ...]
public static ... calc(...)

StringBuilder sB = new StringBuilder("hello");
String tmp = "I'm on my own";

calc(ref tmp);
// sB = null here!

What's happening here!?


Jun 21 '07 #2
On 21 jun, 13:21, "AlexS" <salexru200...@SPAMrogers.comPLEASEwrote:
Most probably you are using wrong CallingConvention - see description of
this enumeration in MSDN or SDK.

"Papa.Coen" <papa.c...@gmail.comwrote in message

news:11*********************@n2g2000hse.googlegrou ps.com...
Right after I call a function in an external dll, some of my local
variables, not used in the call (!!!) in any way, are 'null' right
after the call.
(Pseudo)code
[Dllimport ...]
public static ... calc(...)
StringBuilder sB = new StringBuilder("hello");
String tmp = "I'm on my own";
calc(ref tmp);
// sB = null here!
What's happening here!?
Both Cdecl & StdCall have the same effect.

Jun 21 '07 #3
Correction : the StringBuilder isn't null, but an invalid object
reference.

Jun 21 '07 #4
Papa.Coen <pa*******@gmail.comwrote:
Correction : the StringBuilder isn't null, but an invalid object
reference.
What exactly do you mean by this? What's the exact behaviour?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 21 '07 #5

"Papa.Coen" <pa*******@gmail.comwrote in message
news:11*********************@n2g2000hse.googlegrou ps.com...
Right after I call a function in an external dll, some of my local
variables, not used in the call (!!!) in any way, are 'null' right
after the call.

(Pseudo)code

[Dllimport ...]
public static ... calc(...)

StringBuilder sB = new StringBuilder("hello");
String tmp = "I'm on my own";

calc(ref tmp);
// sB = null here!

What's happening here!?
Looks like a classic buffer overrun. Were you supposed to pass a reference
to a String tracking handle? Probably not. So the function took the space
you gave it, just big enough for a tracking handle, and wrote beyond that.
>

Jun 21 '07 #6
On 21 jun, 20:22, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
>
Looks like a classic buffer overrun. Were you supposed to pass a reference
to a String tracking handle? Probably not. So the function took the space
you gave it, just big enough for a tracking handle, and wrote beyond that.

If that's the problem,then what is or might be the solution?
Is it even possible to map a c# string to a c++ std:string?

Jun 25 '07 #7
Let me tell a little bit more: the starting point is calling a C++
function from dotNET/C#

callMe(std::string* input, std::string* output, str:string* error)

When I tried using StringBuffer for the input (output & error seem to
work just fine as out IntPtr parameters) I got the above error. I've
tried a 'million' different ways to pass the input parameter to the C+
+ dll, but I just can't get it working!

Jun 25 '07 #8

"Papa.Coen" <pa*******@gmail.comwrote in message
news:11********************@g4g2000hsf.googlegroup s.com...
Let me tell a little bit more: the starting point is calling a C++
function from dotNET/C#

callMe(std::string* input, std::string* output, str:string* error)

When I tried using StringBuffer for the input (output & error seem to
work just fine as out IntPtr parameters) I got the above error. I've
tried a 'million' different ways to pass the input parameter to the C+
+ dll, but I just can't get it working!
P/invoke can't handle C++ objects. Not at all, don't even try.

C++/CLI interop can though, so if you write the bit of code calling the C++
functions in a C++/CLI ref class you can convert the .NET String into a C++
std::string and make the call. The C++/CLI ref class is a real .NET type
which can be used from C#.

Another concern is that you seem to have a C++ DLL with objects as
parameters of exported functions. This is badly broken, since different
compilers provide different definitions of std::string. Do you have the
source code of the C++ function? If so, don't make a separate DLL for it,
compile it and the C++/CLI ref class together into a "mixed-mode" assembly.
Jun 25 '07 #9

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

Similar topics

4
by: Rigs | last post by:
Hi, Does anyone happen to have a debug script that will display the variable name and value for all of the local variables on an .asp page? (not session nor application, as I already have...
10
by: riki | last post by:
Hi, i have a big problem...i'm using one jscript for resizing of all of my pics in popUp...in main html i'm having many little pics and clicking on them they open in popUp and resize to larger...
2
by: Oliver Corona | last post by:
I am wondering if anyone has any insights on the performance benefit (or detriment) of declaring local variables instead of referencing members. Is allocating memory for a new variable more...
0
by: Frustrated | last post by:
I have an application that we are porting from Unmanaged C++ to VB.NET that involves sockets. There are two pieces to the code. One piece is a one to one TCP call to a server which is working...
4
by: Rachel Suddeth | last post by:
What is the difference between a managed/unmanaged resource, and how do you tell which is which? I'm trying to understand how to write some Dispose() methods, and we are supposed to put code that...
6
by: Stephen Walch | last post by:
Our application environment consists of three basic layers: 1. Third-party unmanaged DLLs that were written before the CLR was invented and maintain a significant amount of information (including...
3
by: sunbeam | last post by:
Short Description of the Project: we developed a e-learning system for our students. each student has a unique username/password to view the modules he/she should view and nothing more. since we...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
1
by: danep2 | last post by:
Let me start by saying that this is more a question about principle than practice - with the speed of today's computers it's probably rarely an actual issue. Still I'd like to know... If I have...
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
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,...
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,...
0
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...

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.