Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Adding items to a list
Forum Updated to NodeBB v4.3 + New Features

Adding items to a list

Scheduled Pinned Locked Moved C++ Gurus
35 Posts 5 Posters 21.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    loladiro
    wrote on last edited by
    #18

    did you #include <QString>?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      Monkey666
      wrote on last edited by
      #19

      That fixed that but now I have another problem /sigh.

      I have declared std::list<ProcInfo> procList; inside a header file, and that header file is included in the main cpp file which I am doing this all in, however I get error:

      error: multiple definition of `procList'

      There is definitely only 1 definition and it's inside the h file so I'm a little confused, maybe c++ isn't for me :(

      1 Reply Last reply
      0
      • L Offline
        L Offline
        loladiro
        wrote on last edited by
        #20

        There's not much we can do without seeing the header file ...

        1 Reply Last reply
        0
        • M Offline
          M Offline
          Monkey666
          wrote on last edited by
          #21

          proc.h

          @
          #include <windows.h>
          #include <list>
          #include <QString>

          #ifndef PROC_H
          #define PROC_H

          #endif // PROC_H

          class ProcInfo
          {
          public:
          HANDLE Proc;
          DWORD Base;
          int ID;
          operator QString() const
          {
          return QString::number(ID);
          }
          };

          std::list< ProcInfo > procList;

          bool createProcessList();
          DWORD GetModuleBase(LPSTR lpModuleName, DWORD dwProcessId);
          @

          proc.cpp

          @
          #include <windows.h>
          #include <iostream>
          #include <string>
          #include <tlhelp32.h>
          #include "proc.h"
          #include "misc.h"

          using namespace std;

          bool createProcessList()
          {
          PROCESSENTRY32 entry;
          entry.dwSize = sizeof(PROCESSENTRY32);

          HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
          
          if (Process32First(snapshot, &entry;) == TRUE)
          {
              while (Process32Next(snapshot, &entry;) == TRUE)
              {
                  char *procName;
                  strcpy_wc(procName, entry.szExeFile);
                  if (stricmp(procName, "pol") == 0)
                  {
                      ProcInfo *p = new ProcInfo();
                      p->Proc = OpenProcess(PROCESS_ALL_ACCESS,FALSE,entry.th32ProcessID);
                      p->ID = entry.th32ProcessID;
                      p->Base = GetModuleBase("test.dll", entry.th32ProcessID);
                      procList.push_back(*p);
                  }
              }
              if (procList.empty())
                 return false;
              return true;
          }
          
          CloseHandle(snapshot);
          return 0;
          

          }

          DWORD GetModuleBase(LPSTR lpModuleName, DWORD dwProcessId)
          {
          MODULEENTRY32 lpModuleEntry = {0};
          HANDLE hSnapShot = CreateToolhelp32Snapshot( TH32CS_SNAPMODULE, dwProcessId );
          if(!hSnapShot)
          return NULL;
          lpModuleEntry.dwSize = sizeof(lpModuleEntry);
          BOOL bModule = Module32First( hSnapShot, &lpModuleEntry; );
          while(bModule)
          {
          char *foundModule;
          strcpy_wc(foundModule, lpModuleEntry.szModule);
          if(!strcmp( foundModule, lpModuleName ) )
          {
          CloseHandle( hSnapShot );
          return (DWORD)lpModuleEntry.modBaseAddr;
          }
          bModule = Module32Next( hSnapShot, &lpModuleEntry; );
          }
          CloseHandle( hSnapShot );
          return NULL;
          }
          @

          That error points to line 11 in proc.cpp.

          1 Reply Last reply
          0
          • L Offline
            L Offline
            loladiro
            wrote on last edited by
            #22

            Well, my first comment is: fix your "include guards":http://en.wikipedia.org/wiki/Include_guard

            1 Reply Last reply
            0
            • M Offline
              M Offline
              Monkey666
              wrote on last edited by
              #23

              I'll be honest, I don't really get it :/

              1 Reply Last reply
              0
              • L Offline
                L Offline
                loladiro
                wrote on last edited by
                #24

                Yes! Let me explain. In C (and C++) an #include directive will include the file regardless of whether it has been included before. You can litterally compare this to copy'n'past-ing the header file into the source code whenever there is an #include directive. The include guards are used to make sure every header file is only included once. E.g. if your header file would usually be
                @
                class Dummy
                {};
                @
                you'd have to use
                @
                #ifndef DUMMY_H //i.e. if dummy.h has never been included before
                #define DUMMY_H //remember that it has been included

                class Dummy
                {};

                #endif //endif after all statements
                @
                In your case the include guards have to enclose all of your statements (the header files not necessarily because they have their own include guards, but it's usually best to have the include guards be the first and last satements in your header files)

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  Monkey666
                  wrote on last edited by
                  #25

                  Ok so if I understand you it should be:

                  @
                  #include <windows.h>
                  #include <list>
                  #include <QString>

                  #ifndef PROC_H
                  #define PROC_H

                  class ProcInfo
                  {
                  public:
                  HANDLE Proc;
                  DWORD Base;
                  int ID;
                  operator QString() const
                  {
                  return QString::number(ID);
                  }
                  };

                  std::list<ProcInfo> procList;

                  bool createProcessList();
                  DWORD GetModuleBase(LPSTR lpModuleName, DWORD dwProcessId);

                  #endif // PROC_H@

                  But I still get the same error.

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    loladiro
                    wrote on last edited by
                    #26

                    In the compiler output window there should be information about where the compiler thinks that procList has been declared (something like "previously declared here: " )

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      Monkey666
                      wrote on last edited by
                      #27

                      c:\QtSDK\Desktop\Qt\4.7.3\mingw\include\QtCore\qatomic_i386.h:125: error: first defined here

                      1 Reply Last reply
                      0
                      • L Offline
                        L Offline
                        loladiro
                        wrote on last edited by
                        #28

                        Hmm... try to use
                        @ inline operator QString() const
                        {
                        return QString::number(ID);
                        }
                        @

                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          Monkey666
                          wrote on last edited by
                          #29

                          Same error :( The error is on procList anyways not ProcInfo, it doesn't make alot of sense to me :/

                          1 Reply Last reply
                          0
                          • C Offline
                            C Offline
                            cincirin
                            wrote on last edited by
                            #30

                            If you wish procList in header, you need to declare it in cpp file, and define in header as
                            @extern std::list< ProcInfo > procList@

                            1 Reply Last reply
                            0
                            • D Offline
                              D Offline
                              dangelog
                              wrote on last edited by
                              #31

                              Guys. It's not a "try and guess" process. He defined a global variable inside a header. As soon as that header gets included in two or more translation units, the compiler will output multiple definitions of the same name. And the linker will complain. Put that definition in ONE translation unit and you'll be ok.

                              Software Engineer
                              KDAB (UK) Ltd., a KDAB Group company

                              1 Reply Last reply
                              0
                              • M Offline
                                M Offline
                                Monkey666
                                wrote on last edited by
                                #32

                                Works thanks :) But now I have other unrelated questions, should I post in a separate thread?

                                1 Reply Last reply
                                0
                                • V Offline
                                  V Offline
                                  vsorokin
                                  wrote on last edited by
                                  #33

                                  Yep, You do. One theme - one thread.

                                  --
                                  Vasiliy

                                  1 Reply Last reply
                                  0
                                  • M Offline
                                    M Offline
                                    Monkey666
                                    wrote on last edited by
                                    #34

                                    I have another question about this.

                                    The function currently working is:

                                    @
                                    class ProcInfo
                                    {
                                    public:
                                    HANDLE Proc;
                                    DWORD Base;
                                    std::string WindowName;
                                    int ID;
                                    inline operator QString() const
                                    {
                                    return QString::number(ID);
                                    }
                                    };
                                    @

                                    How would I mane the QString method return the following format:

                                    "(" + ID + ") " + WindowName

                                    So the output would end up something like:

                                    (432) NotePad

                                    1 Reply Last reply
                                    0
                                    • C Offline
                                      C Offline
                                      cincirin
                                      wrote on last edited by
                                      #35

                                      @
                                      return QString('(') + QString::number(ID) + ')' + WindowName.c_str()
                                      @
                                      Also have a look at "More Efficient String Construction":http://doc.qt.nokia.com/latest/qstring.html#more-efficient-string-construction

                                      1 Reply Last reply
                                      0

                                      • Login

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • Users
                                      • Groups
                                      • Search
                                      • Get Qt Extensions
                                      • Unsolved