Jump to content
  • Advertisement
Sign in to follow this  
Rich76

C++ casting error

This topic is 3321 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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); }

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
<<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.

Share this post


Link to post
Share on other sites
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();
// or
int something(CWinThread * p);

The if statement is:

if (pThread->something()) {
v8 = pThread->something_else();
}
// or
if (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?

Share this post


Link to post
Share on other sites
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.


Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!