[C++] GetModuleBase incompability menu

User Tag List

Page 2 of 2 FirstFirst 12
Results 16 to 22 of 22
  1. #16
    flo8464's Avatar Active Member
    Reputation
    30
    Join Date
    Apr 2009
    Posts
    434
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by lanman92 View Post
    How would you use a template like that in c++? I know in C# you use Type.Typecode, what is the equal in c++?
    You mean Cyphers example?

    Here's the code rewritten with a template:

    Code:
    template <typename CharT>
    std::basic_string<CharT> ReadString(std::uintptr_t address)
    {
      std::basic_string<CharT> temp;
    
      CharT current;
      do
      {
        SIZE_T bytesRead;
        BOOL ec = ReadProcessMemory(  hProcess,
                                      reinterpret_cast<LPCVOID>(address),
                                      static_cast<LPVOID>(&current),
                                      sizeof(current),
                                      &bytesRead);
        if(!ec || bytesRead != sizeof(current))
          throw std::runtime_error("Error reading string");
    
        if(current)
          temp.push_back(current);
    
        address += sizeof(current);
      }
      while(current);
    
      return temp;
    }
    And use it like that:

    auto string8 = ReadString<char>(0xDEADBEEF); // Read a string with a character size of 8bit, ASCII or UTF8.
    auto string16 = ReadString<char16_t>(0xDEADBEEF); // 16bit, probably UTF16.
    auto string32 = ReadString<char32_t>(0xDEADBEEF); // 32bit, probably UTF32.
    UTFX are only hints, std::basic_string<> doesn't care about encoding and is more or less a dynamic array offering common string operations.
    Hey, it compiles! Ship it!

    [C++] GetModuleBase incompability
  2. #17
    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, I see. What if you needed to do an operation dependant upon the type of character? Would you need to do template specialization for that?

  3. #18
    suicidity's Avatar Contributor
    Reputation
    207
    Join Date
    Oct 2006
    Posts
    1,439
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Give an example where you would need to do that, so we can answer you. Extremely broad questions gets no answer.


  4. #19
    Bananenbrot's Avatar Contributor
    Reputation
    153
    Join Date
    Nov 2009
    Posts
    384
    Thanks G/R
    1/3
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't get exactly what you mean, but if you refer to the typical typecode switch case found in most implementations of a generic Read<T> method in C#, you don't do anything similar in C++.
    For a Read<T> method, the generic part is just some syntactic sugar to avoid casting to and from object.


    C++ templates are a whole different thing (disregarding the angle bracket parameters). Templates are evaluated at compile time, whereas generics are not (only some simple generic parameter constraint checks like new()).
    You can write template meta programs which are completely evaluated at compile time and therefore are only a constant in code. The type parameters are only constrained by a well defined interface which is called a concept.

    See InputIterator - C++ Reference. This is one of the simplest concepts, the input iterator.
    Notice that InputIterator is not an actual type but an interface definition. One example of a type 'modeling' this concept is a const pointer type.
    So everywhere you need to pass a type modelling InputIterator, you can pass a const pointer to something.
    But you could also pass less trivial iterator types, like istream_iterator - C++ Reference.
    This is where templating shines: you can reuse code in an extreme and efficient way, because it's evaluated at compile time.
    In C# you would implement interfaces instead of modeling concepts and you could argue, that you seldom notice a performance hit by doing so.
    But if you really hit a bottleneck, you have to duplicate code and tailor it to fit your needs. C++ is much drier in that sense.

    I notice that I'm loosing track...


    TLDR:
    You would call it like ReadString<char> or ReadString<wchar_t>. This is an example of templating it for character type:
    Code:
    template <typename CharT> 
    std::basic_string<CharT> ReadString(std::uint_ptr address)
    {
    std::basic_string<CharT> temp; CharT current; do {
    SIZE_T bytesRead; BOOL ec = ReadProcessMemory(
    hProcess,
    reinterpret_cast<LPCVOID>(address), static_cast<LPVOID>(&current), sizeof(CharT), &bytesRead);
    if (!ec || bytesRead != sizeof(CharT))
    throw std::runtime_error("Error reading string");
    if (current)
    temp.push_back(current);
    address += sizeof(CharT);
    } while(current); return temp;
    }
    Essentially I substituted char for CharT...
    Of course, this method fails for reading UTF-8. This is where char_traits may help out(?).
    Last edited by Bananenbrot; 08-05-2011 at 01:20 PM. Reason: flo8464 ↓

  5. #20
    flo8464's Avatar Active Member
    Reputation
    30
    Join Date
    Apr 2009
    Posts
    434
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @Bananenbrot: You don't increment address by sizeof(CharT), so verything except 8bit data types will fail.
    Hey, it compiles! Ship it!

  6. #21
    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)
    Well, most of your post was over my head, banenbrot, but the part about the TypeCode switch was what I meant. Thank you for the
    clarification.

  7. #22
    Bananenbrot's Avatar Contributor
    Reputation
    153
    Join Date
    Nov 2009
    Posts
    384
    Thanks G/R
    1/3
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by flo8464 View Post
    @Bananenbrot: You don't increment address by sizeof(CharT), so verything except 8bit data types will fail.
    Yeah, thank you... While copy and pasting the code I was thinking about it but forgot it.

Page 2 of 2 FirstFirst 12
All times are GMT -5. The time now is 10:10 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