C++ casting error

Started by
18 comments, last by Sik_the_hedgehog 14 years, 11 months ago
I'm a C# programmer, however, I need to figure out how to fix a C++ conditional statement for a friend. C++ conditional statements look a bit confusing to me. Can you cast the following variable as an int in a conditional statement: CWinThread* pThread = AfxGetThread(); int v8; if ( (*(int (**)(int))(*(DWORD *)pThread + 80))(pThread) ) { v8 = (*(int (**)(int))(*(DWORD *)pThread + 84))(pThread); }
Advertisement
For the love of all things holy, please don't let him write code like that! It's utterly unreadable. I mean, what is he trying to do? You can always force a cast in C++ (though it might lead to undefined behavior), but pThread appears twice in that "cast"... which it shouldn't. What is he trying to do? Perhaps there are better alternatives.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
What the...?

It looks like it's casting pThread + 80 to a function pointer that takes an int as an argument and return an int, and if that function returns non-zero, it sets v8 to the returned value of the function at address pThread + 84.

That is wrong, wrong, wrong! You just shouldn't be doing that.

First of all, this is not going to work on a 64-bit system. Second of all, it looks like he's trying to call functions defined in CWinThread which are not public. They're not public for a reason: if somebody compiles this code with a different version of MFC, it's quite likely that it's going to crash! If he upgrades his own version of MFC, it's going to crash.

Etc...

Tell us what he really want to do...
My eyes, they bleed. Tell your friend not to write code like that. Then slap him. Hard.
LOL, sorry guys.

It's not his fault. He's not a programmer. This is decompiled source code. pThread is the only name change of the decompilation.

Our original source code is gone.
Quote:Original post by Rich76
It's not his fault. He's not a programmer.

If he wrote it, it's his fault. If he didn't write it... well it's still pretty crappy.

Quote:Original post by Rich76
This is decompiled source code. pThread is the only name change of the decompilation.

And exactly what program are you decompiling? And what are you trying to do?
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
<<If he wrote it, it's his fault. If he didn't write it... well it's still pretty crappy.>>

No, he did not write it. For your information, the senior programmer now works for Yahoo!, lol. I don't think the decompilation gives him justice, though.

They are trying to decompile and fix a game with a few bugs, since the source code is gone forever.
Quote:Original post by Rich76

if ( (*(int (**)(int))(*(DWORD *)pThread + 80))(pThread) )
{
v8 = (*(int (**)(int))(*(DWORD *)pThread + 84))(pThread);
}


This is a function call. The function has signature:
int CWinThread::something();// orint something(CWinThread * p);

The if statement is:
if (pThread->something()) {  v8 = pThread->something_else();}// orif (pThread.fp_something_at_80(pThread)) {  v8 = pThread.fp_something_else_at_84(pThread);}
The rest of casts and offsets is almost certainly offset into v-table, or casting a (member) function pointer that is stored as member of CWinThread. I personally am not in the mood for deciphering which.

Quote:I need to figure out how to fix a C++ conditional statement for a friend.


And, dare I ask, what exactly needs fixing?
Thank you Antheus. :)

I think right now, they are trying to decompile and then compile back into a working game (isometric game).

Some of the bugs are:

1. No sound for Vista users.

2. Players often go into walls where they can't be killed, but can still kill other players.

3. Players often warp around the map

4. They want additional data to be sent to the database

5. etc.


Quote:Original post by Rich76
I'm a C# programmer, however, I need to figure out how to fix a C++ conditional statement for a friend. C++ conditional statements look a bit confusing to me.

Can you cast the following variable as an int in a conditional statement:

CWinThread* pThread = AfxGetThread();


int v8;

if ( (*(int (**)(int))(*(DWORD *)pThread + 80))(pThread) )
{
v8 = (*(int (**)(int))(*(DWORD *)pThread + 84))(pThread);
}


Best...code...evar...:P

Man I can't decifer that. The (**) confuses me but I'm also kind of a beginner.

This topic is closed to new replies.

Advertisement