Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PELib understanding problem. Visitor classes.

Posted on 2007-04-03
6
Medium Priority
?
319 Views
Last Modified: 2008-02-01
Hello,

im trying to use "PELib" (www.pelib.com) in one of my C++ projects but have a problem with understanding some parts of the code. I was working with the FileDump3 example and it includes the following lines of code:

      DumpPeHeaderVisitor v1;
      pef->visit(v1);

What is this visitor used for, why do i need it? Can someone explain that to me? I think there is a visitor base class that is extended in DumpPeHeaderVisitor but i dont understand what the whole purpose of this visitor is.

Thanks.
0
Comment
Question by:b3n_
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 18848807
Check out the FileDump example on their site :

        http://www.pelib.com/examples/FileDump.rar

What the program does is dump the contents of a PeFile.

A PeFile has the visit() method which has to be passed a PeFileVisitor object :

        http://www.pelib.com/documentation/classPeLib_1_1PeFileVisitor.html

This class has 2 virtual callback methods (one for 32bit and one for 64bit). So, you have to derive a class from PeFileVisitor, and implement those callback methods. In this example, they derived DumpPeHeaderVisitor like this :

        class DumpPeHeaderVisitor : public PeLib::PeFileVisitor
        {
        public:
            virtual void callback(PeLib::PeFile32 &file) {dumpPeHeader<32>(file);}
            virtual void callback(PeLib::PeFile64 &file) {dumpPeHeader<64>(file);}
        };

with :

        template<int bits>
        void dumpPeHeader(PeLib::PeFile& pef)
        {
            // <SNIP>
        }

So, the implementation of the callback methods dump the contents of the header in the PeFile.

So : the visitor is needed to specify what and how you want to use the PeFile.

Does that explain it for you ?
0
 

Author Comment

by:b3n_
ID: 18849267
Thanks for your explanation, its getting clearer now. But what exactly does a(this) callback do and why are templates used for those methods? Is there a difference between the 32 and 64 bit file?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18849403
>> But what exactly does a(this) callback do

In this case, you tell what you want to do in the callback() function, then you pass that function to the PeFile object, which will execute that callback function on the file. So, you're not directly working on the file, but you're providing the code to do what you want with the file. That's why they named it a callback, because it indirectly does what you want to do.
Compare it to someone that wants something done, but he can't do it himself. So, he describes what he wants to do on a piece of paper, and passes that piece of paper to another person. This other person will then do whatever is on that paper.


>> and why are templates used for those methods?

The dumpPeHeader() function uses a template to avoid having to write the same code for the 32bit and 64bit versions. This way, the same function is used for both the 32bit and 64bit versions, and the choice is made by choosing the correct templated value.


>> Is there a difference between the 32 and 64 bit file?

I assume so, otherwise they wouldn't have made the distinction in their PeFile class ...
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:b3n_
ID: 18849465
That makes sense, so by looking at the code:
       
       dumpMzHeader(pef);
      
      DumpPeHeaderVisitor v1;
      pef->visit(v1);
      
      dumpExportDirectory(pef);
      
      DumpImpDirVisitor v2;
      pef->visit(v2);
      
      dumpResourceDirectory(pef);
      
      dumpRelocationsDirectory(pef);
      
      DumpTlsDirVisitor v3;
      pef->visit(v3);
      
      dumpBoundImportDirectory(pef);
      dumpIatDirectory(pef);
      dumpComHeaderDirectory(pef);

The methods that use a visitor have a different output depending on the filetype (either 32 or 64 bit) and the ones without a visitor callback have the same output on both kind of files? Thats how i understand the code now...
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
ID: 18849649
>> The methods that use a visitor have a different output depending on the filetype (either 32 or 64 bit)

Yes. But they also have different output depending on how you implemented those callback methods.

Notice that in the example code, three different visitor classes are used :

    DumpPeHeaderVisitor : to dump the header
    DumpImpDirVisitor : to dump the import directory
    DumpTlsDirVisitor : to dump the TLS directory

Depending on which of these three is passed to the visit() method, the output will be different.
On top of that : for each of these three visitor classes, two separate callback methods are defined - one that will be used if the file is 32bits, and the other if the file is 64bits.


>> and the ones without a visitor callback have the same output on both kind of files?

I assume you mean function calls like this :

    dumpResourceDirectory(pef);

These functions operate directly on the file without passing through the visit() method. So, indeed, no distinction is made for 32bit or 64bit files, unless the function does so explicitly.
0
 

Author Comment

by:b3n_
ID: 18850172
thank you very much for the explanation.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question