Skip to main content
gamedev.net
🔒 Locked

C++ casting error

Started by Rich76 18 replies 2.3k views
Original Post
Rich76
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); }
Cornstalks
Cornstalks
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.
Codeka
Codeka
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...
Driv3MeFar
Driv3MeFar
My eyes, they bleed. Tell your friend not to write code like that. Then slap him. Hard.
Rich76
Rich76
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.
Cornstalks
Cornstalks
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?
Rich76
Rich76
<>

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.
Antheus
Antheus
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?
Rich76
Rich76
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.


Chrono1081
Chrono1081
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.
phresnel
phresnel
Quote:
Original post by Rich76
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.


Appearances of "lol" in phrases that are not jokes or otherwise funny somehow make me nervous. But it might just be me.
Rich76
Rich76
Quote:
Original post by phresnel
Quote:
Original post by Rich76
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.


Appearances of "lol" in phrases that are not jokes or otherwise funny somehow make me nervous. But it might just be me.



Hmm, maybe you understood it differently. It's like hearing news Barney Fife became head of the FBI. But in any case, I'm sorry I made you feel nervous. :(
phresnel
phresnel
Quote:
Original post by Rich76
Quote:
Original post by phresnel
Quote:
Original post by Rich76
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.


Appearances of "lol" in phrases that are not jokes or otherwise funny somehow make me nervous. But it might just be me.



Hmm, maybe you understood it differently. It's like hearing news Barney Fife became head of the FBI. But in any case, I'm sorry I made you feel nervous. :(


Oh, I think it was my fault. Looks like I missed some of your other posts. Never post on gamedev when undercaffeinated, lol.

Seriously, often people post "lol's" and kiddiespeak all over the place, where it is really overdosed or unfitting, which in turn looks childish, but I am wrong here, I think :S

*facepalming, rating up*
Rich76
Rich76
Absolutely. I agree with you. I have been known to abuse the LOL and have said to myself, "that wasn't really funny". :)
Sik_the_hedgehog
Sik_the_hedgehog
Quote:
Original post by Chrono1081
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.

You don't need to be a beginner to get confused by that x_X (EDIT: I mean the whole casting in general, if you mean the (**), that's a cast to a pointer to a pointer... still no idea what is being done there though o_O)

Also how come the source code is gone? o_O That, and the only thing I can say is good luck, decompiling the game isn't exactly going to be an easy task. I know perfectly what I mean, I've done similar things already ._.'
Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.
DevFred
DevFred
Quote:
Original post by Rich76
if ( (*(int (**)(int))(*(DWORD *)pThread + 80))(pThread) )
{
v8 = (*(int (**)(int))(*(DWORD *)pThread + 84))(pThread);
}

You are three weeks late for April Fool's Day.
rip-off
rip-off
Quote:
Quote:
Quote:

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



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

EDIT: I mean the whole casting in general, if you mean the (**), that's a cast to a pointer to a pointer... still no idea what is being done there though o_O


Its not a cast to a pointer to a pointer. Look at the parentheses. It is part of the type. "int (**)(int)" is a pointer-to a pointer to a function that takes and returns an integer (or integer sized argument on this architecture).
int foo(int x){}// Typedefs make function pointers, and C++ in general, more manageable.typedef int (*FunctionPtr) (int);typedef FunctionPtr *FunctionPtrPtr;int main(){    // hard way    int (*ptr)(int) = foo;    // easy way    FunctionPtr ptr = foo;    // very hard way    int (**ptr_to_ptr)(int) = &ptr    // easier    FunctionPtrPtr ptr_to_ptr = &ptr}

I've used integers here, but given the argument passed to the function it is more likely the parameter is a pointer type. I imagine that including pointer parameters in the above would not have helped its already dubious clarity. [smile]
phresnel
phresnel
Quote:
Original post by DevFred
Quote:
Original post by Rich76
if ( (*(int (**)(int))(*(DWORD *)pThread + 80))(pThread) )
{
v8 = (*(int (**)(int))(*(DWORD *)pThread + 84))(pThread);
}

You are three weeks late for April Fool's Day.


You are 794 minutes and 31 seconds late for blaming.

Quote:
Original post by Rich76 -- Posted - 4/21/2009 9:43:02 PM
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.
Sik_the_hedgehog
Sik_the_hedgehog
Quote:
Original post by rip-off
Quote:
Quote:
Quote:

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



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

EDIT: I mean the whole casting in general, if you mean the (**), that's a cast to a pointer to a pointer... still no idea what is being done there though o_O


Its not a cast to a pointer to a pointer. Look at the parentheses. It is part of the type. "int (**)(int)" is a pointer-to a pointer to a function that takes and returns an integer (or integer sized argument on this architecture).
*** Source Snippet Removed ***
I've used integers here, but given the argument passed to the function it is more likely the parameter is a pointer type. I imagine that including pointer parameters in the above would not have helped its already dubious clarity. [smile]

Oops, I've only taken into account the (**) which is what he mentioned and not the whole thing... Like I said, that thing is so ridicuously complex that I didn't even bother to try to understand it x_X
Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.