totally WIERD error.

Started by
4 comments, last by MJP 15 years, 8 months ago
ok .. so i think perhaps ive had one too many tonight or something, or my eyes are playing tricks. But here goes. This is the line that is acting very strangely (c++, visual studio 2005) std::string s1 = "hello"; std::string s2 = "World"; lol.. yeah you read that right. that is SERIOUSLY the problem code.... for SOME reason, the string 's1' is coming out containing the string 'urcehello' and s2 is coming out with random chars i havent even seen before. ive stepped through the 2 lines n its the same every time. same story if i use the constructor directly std::string s1("hello"); std::string s2("World"); Im including string properly.. i just REALLY dont get it. I mean this is BASIC. lol. what the heck??
Advertisement
What do you mean by "coming out"? Are you using the string somewhere? In other words, are you printing it with cout or showing it with a message box?

Keep in mind that if you're looking at the string value through the debugger, it may not look right if you're compiling in Release with optimizations.
''Keep in mind that if you're looking at the string value through the debugger, it may not look right if you're compiling in Release with optimizations.''

lol glad you said that cos i literally only JUST noticed that it works on on debug, and is strange on release so must be optimizations. yeah i meant 'coming out' in the debugger.

Only problem is that when i now (knowing the c++ app is technically working in debug) try to use the functions to obtain certain strings i need for my c# app through a c++ dll, im still gettin all the strange strings ..... n ive def rebuilt the dll....

Well if you need to verify the string on the C++ side before you marshal it over to your C# app, you can just put it up in a dialog box and it will display the correct string.

How are you handling the marshaling? Are you just exporting a C function from your DLL and then p/invoking? Or are you doing something more fancy?
''How are you handling the marshaling? Are you just exporting a C function from your DLL and then p/invoking? Or are you doing something more fancy?''


currently i have a function in my C++ app defined something like so...

DLL_EXPORT const char* getEntityName()
{
return EntityManager->getEntity()->getName().c_str();
}



then in my c# app i have

[DllImport("EntityManager.dll")]
public static extern IntPtr GetEntityName();

and i use it in the c# app like so:

string name = Marshal.PtrToStringAnsi(GetEntityName());



Its very strange because one name came out half right.. about midway through my entities but then all the rest are messed up!.
I'm not sure if it's related to your problem or not, but if you declare your managed function as returning type String then it should be automatically converted/marshaled for you without having to call PtrToStringAnsi.

This topic is closed to new replies.

Advertisement