Click here to Skip to main content
16,016,738 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

Once again I ran into troubles.

I have MFC class with static function.

And I'm having troubles using the char arrays or better said "Any function that requires string input". This static function is also __declspec(naked).

I never had problems with it before. It was stand-alone non-member function. But now I rewrote my code and here goes the troubles:

char chSomeBuffer[500]; // Don't mind the size or whatever, it's just a sample
sprintf_s(chSomeBuffer, 20, "%d", 50);
AfxMessageBox(chSomeBuffer); 

// MessageBox displays some unreadable bullc**p (about 3 chars size).

And when I tried add chSomeBuffer in to my ListControl it displayed also some c**p.

But direct string input works.
AfxMessageBox("WEE"); //Works correctly. 


What's the problem please.

Thank you for your answers.
Posted
Updated 11-Aug-10 9:58am
v3
Comments
Niklas L 11-Aug-10 15:02pm    
Regarding the sample buffer: did you tell us the truth, the whole truth, and nothing but the truth? Did you declare it as automatic as you did here, or is it allocated on the heap?
Richard MacCutchan 11-Aug-10 15:53pm    
I suspect the code posted above is not the actual code from your application. You mentioned a dll in your answer to Niklas, where exactly does that come into the above four lines of code? Are you sue you are not trying to display an ANSI string as Unicode?
Im2N00By 11-Aug-10 16:50pm    
The dll is mine. Hooks function which takes some data. Looks like: EXECUTE REPLACED CODE (by jump), DO MY STUFF, RETURN BACK. I was calculating size of input data. sprintf_s with nSize as argument resulted into bullcrap (In debugger it created a string "50" and yet I got a bullcrap from MsgBox). Then I tried putting just random combinations with sprintf_s and StringCbPrintf (I wasn't even working with input data).

I already fixed the problem anyways. I removed the naked spec and removed prologue. Only interesting fact is, that the function was working perfectly till I made created it as a class member.

Thank you anyways guys. You tried to help me and I appreciate that

The code seems right. What does your debugger give you stepping through the above code?
 
Share this answer
 
Comments
Im2N00By 11-Aug-10 14:22pm    
That's the problem. It's plugin dll loaded by application protected against debugging. So it looks like I will have to use some debugger like Olly with anti anti-debugging tricks.
I'm glad you were able to fix the problem, OP.

In the future, I would recommend using tsprintf_s and TCHAR for your function and your data type, respectively. This way, your array will typedef to a CHAR for an ANSI project and a WCHAR for Unicode, and this makes your code not only more portable but less error-prone.
 
Share this answer
 
Comments
Im2N00By 12-Aug-10 12:41pm    
I was using them. But then the function got fishy, so I switched to regular char just to test it. I didn't fix it 100% after all. Sometimes it crashes and that's undesired behavior. I will create new direct hook... the prologue is required to run without crash, but it also causes the char problems (But only when the function is member of class) - All-though I specified the call as __stdcall.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900