The typedef keyword. Functions, function pointers, and you. menu

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Cypher's Avatar Kynox's Sister's Pimp
    Reputation
    1358
    Join Date
    Apr 2006
    Posts
    5,368
    Thanks G/R
    0/6
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    The typedef keyword. Functions, function pointers, and you.

    Because a certain member of the forums (*cough* typedef *cough*) is a retard (apparently me using typdef on functions pointers is impossible and means I must have "shit for brains" -- his words) I figured other people must suffer from the same misconception so I will explain how to use a typedef with functions or function pointers.

    Note: These are all REAL WORLD examples from one of my projects, so typedef (the person) can't go "ZOMG THIS NO WORK" because I'm using it right now and it does.

    Functions:

    When writing a callback system for my console I needed choose a function prototype all the callbacks would follow, I chose the following:

    Code:
    void __cdecl CallbackName(const std::string& Params);
    In order to make writing the callback system easier on myself I used 'typedef' to create a type synonym for the callback prototype:
    Code:
    typedef void (__cdecl ConsoleCallback) (const std::string &);
    I could then use the type "ConsoleCallback" instead of typing out the ugly type definition I would have to otherwise. An important thing to note is typedef defines a TYPE SYNONYM, meaning the word actually is a type, it is NOT a 'define' that just copy-pastes using the preprocessor before the compiler takes over, the compiler sees "ConsoleCallback" as a type and treats it as such.

    You can then use the new type synonym as such:
    std::map<std::string,ConsoleCallback*> ConsoleCallbacks;

    Code:
    void __cdecl TestCallback(const std::string&)
    {
    	;
    }
    
    void RegisterConsoleCallback(const std::string& Name, ConsoleCallback* Callback)
    {
    	ConsoleCallbacks[Name] = Callback;
    }
    
    void RegisterCallbacks()
    {
    	RegisterConsoleCallback("test",&TestCallback);
    }
    
    std::string Submit( const char * /*pszArgs*/, CElement * pThis )
    {
    	std::string sString(pThis->GetString());
    	string Command = sString.substr(0, sString.find(" "));
    	string Args = sString.substr(sString.find(" ")+1,sString.size());
    	std::transform(Command.begin(), Command.end(), Command.begin(), tolower);
    	if (ConsoleCallbacks[Command])
    	{
    		ConsoleCallback* pMyCallback = ConsoleCallbacks[Command];
    		(*pMyCallback)(Args);
    	}
    	else
    	{
    		WriteOutput("Invalid command.");
    	}
    	pThis->SetString("");
    	return std::string();
    }

    Function Pointers:

    Typedefs are also useful when calling game function from an injected DLL in the form of a function pointer, this is great because you can avoid the use of inline ASM and have the compiler take care of the stack for you (and generate 'better' ASM because there are no __asm blocks that hinder the compiler's optimizer).

    Example (from my fishbot, modified, unrelated code removed):
    Code:
    typedef bool (__cdecl * tCastSpellById)(unsigned int SpellId, unsigned int Unk2, unsigned int Unk3, unsigned int Unk4);
    tCastSpellById oCastSpellById = 0;
    
    void SetupMiscFunctions()
    {
    	oCastSpellById = (tCastSpellById)GetRPMgr()->GetFindPattern()->GetAddress("CSpell::CastSpellById");
    }
    
    void CastSpell(unsigned int SpellId)
    {
    	oCastSpellById(SpellId,0,0,0);
    }
    You can then use the code to cast for example 'Fishing (Apprentice)' which has ID 7620.

    Hopefully now everyone can see you CAN use 'typedef' to define function and function pointer type synonyms.
    Last edited by Cypher; 01-07-2009 at 12:08 AM.

    The typedef keyword. Functions, function pointers, and you.
  2. #2
    Remus's Avatar Banned

    Reputation
    402
    Join Date
    Nov 2007
    Posts
    1,697
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lol nice read.. i understood it all =)

  3. #3
    Shynd's Avatar Contributor
    Reputation
    97
    Join Date
    May 2008
    Posts
    393
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm shit at C/++ and even I considered this within the realm of common knowledge.

  4. #4
    hypnodok's Avatar Member
    Reputation
    19
    Join Date
    Nov 2007
    Posts
    65
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    This looks like its an irc screenshot, is there a mmowned irc channel?

  5. #5
    Gamer's Avatar Active Member
    Reputation
    239
    Join Date
    Jan 2007
    Posts
    198
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by hypnodok View Post

    This looks like its an irc screenshot, is there a mmowned irc channel?
    Shoutbox possibly?

  6. #6
    lanman92's Avatar Active Member
    Reputation
    50
    Join Date
    Mar 2007
    Posts
    1,033
    Thanks G/R
    0/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, here's a semi-related question. Let's say I find GetObjByGUID in IDA. It says it has 3 arguments, but I'm thinking two of those are going to be used for the LONGLONG GUID. Which two is it? And what is the other param for?

    This was my first thought:

    Code:
    typedef DWORD (__cdecl * tGetObjByGuid)(__int64 GUID, unsigned int unk1);
    tGetObjByGUID oGetObjByGUID = 0x46D3C0;
    Last edited by lanman92; 12-31-2008 at 12:51 PM.

  7. #7
    Cypher's Avatar Kynox's Sister's Pimp
    Reputation
    1358
    Join Date
    Apr 2006
    Posts
    5,368
    Thanks G/R
    0/6
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It would probably be something like this:
    typedef CGObject_C* ( __cdecl* tGetObjectByGUID)(unsigned long GuidLow, unsigned long GuidHigh, int FilterMask);

    Orrrr. You could replace it with:
    typedef CGObject_C* ( __cdecl* tGetObjectByGUID)(WoWGUID FullGuid, int FilterMask);

    Code:
    struct WoWGUID
    {
    	union
    	{
    		__int64 FullGuid;
    		struct
    		{
    			unsigned long High;
    			unsigned long Low;
    		};
    		unsigned char Bytes[8]; // Used for packing/unpacking GUIDs in packets (CPacketManager::PackGuid, CPacketManager::UnpackGuid)
    	};
    };
    EDIT:

    The screenshot is from the MMOwned shoutbox.
    Last edited by Cypher; 12-31-2008 at 12:58 PM.

  8. #8
    lanman92's Avatar Active Member
    Reputation
    50
    Join Date
    Mar 2007
    Posts
    1,033
    Thanks G/R
    0/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, thanks Cypher. Should've gone to the wowdev page... Think they have that struct posted. What does the filter do?

  9. #9
    Cypher's Avatar Kynox's Sister's Pimp
    Reputation
    1358
    Join Date
    Apr 2006
    Posts
    5,368
    Thanks G/R
    0/6
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can filter objects by type (unit, player, gameobject, etc).

  10. #10
    lanman92's Avatar Active Member
    Reputation
    50
    Join Date
    Mar 2007
    Posts
    1,033
    Thanks G/R
    0/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But, if it only returns one object, what's the point of that?

  11. #11
    Shynd's Avatar Contributor
    Reputation
    97
    Join Date
    May 2008
    Posts
    393
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    He's talking about those received by packet-logging.

  12. #12
    lanman92's Avatar Active Member
    Reputation
    50
    Join Date
    Mar 2007
    Posts
    1,033
    Thanks G/R
    0/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh. Gotcha.

  13. #13
    Nesox's Avatar ★ Elder ★
    Reputation
    1280
    Join Date
    Mar 2007
    Posts
    1,238
    Thanks G/R
    0/3
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Happy new year u mofos, if you live in a gmt+1 zone!

  14. #14
    schlumpf's Avatar Retired Noggit Developer

    Reputation
    755
    Join Date
    Nov 2006
    Posts
    2,759
    Thanks G/R
    0/3
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And happy new year, if you live in GMT now.

  15. #15
    Cypher's Avatar Kynox's Sister's Pimp
    Reputation
    1358
    Join Date
    Apr 2006
    Posts
    5,368
    Thanks G/R
    0/6
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    GUIDs in packets are "packed" to reduce their size. When dealing with packed GUIDs its much easier to treat them as an array of bytes.

Page 1 of 2 12 LastLast

Similar Threads

  1. Ending the Bloodcurse and You
    By slothahoy in forum World of Warcraft Exploits
    Replies: 12
    Last Post: 05-10-2007, 09:21 AM
  2. Wyvern >>> Phoenix (And you don't need the ModelChange208.exe)
    By Soren93 in forum World of Warcraft Model Editing
    Replies: 17
    Last Post: 04-19-2007, 05:18 AM
  3. The Search Button and YOU
    By Finnster in forum World of Warcraft General
    Replies: 2
    Last Post: 10-23-2006, 06:45 PM
All times are GMT -5. The time now is 09:22 AM. Powered by vBulletin® Version 4.2.3
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Google Authenticator verification provided by Two-Factor Authentication (Free) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search