• Advertisement
Sign in to follow this  

C++ casting error

This topic is 3230 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
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.

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
Absolutely. I agree with you. I have been known to abuse the LOL and have said to myself, "that wasn't really funny". :)

Share this post


Link to post
Share on other sites
Quote:
Original post by Rich76
Absolutely. I agree with you. I have been known to abuse the LOL and have said to myself, "that wasn't really funny". :)


Heh :D

Share this post


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

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

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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

  • Advertisement