c++ - [SOLVED]capture working directory of external process[Qt/WinAPI] -


how wait-then-capture working directory of external process (which possibly not running yet) inside qt/windows code?

i did similar times ago, , here relevant part. show how works.

waitforprocess class search exe every refresh milliseconds, passes arguments start.

if use snippet, , start notepad.exe, show path in console. can emit signal or whatever once have information.

main.cpp

#include <qapplication> #include "waitforprocess.h"   int main(int argc, char *argv[]) {     qapplication a(argc, argv);      waitforprocess wfp;     wfp.start("notepad.exe", 1000);       return a.exec(); } 

waitforprocess.h

#ifndef waitforprocess_h #define waitforprocess_h  #include <qobject> #include <qstring> #include <qtimer>  class waitforprocess : public qobject {     q_object public:     explicit waitforprocess(qobject *parent = 0);     ~waitforprocess();  signals:  public slots:      void start(const qstring& processname, int refresh);  private slots:      void ontimeout();  private:      qstring _processname;     qtimer* _timer;      bool findwin32process(); };  #endif // waitforprocess_h 

waitforprocess.cpp

#include "waitforprocess.h"  #include <windows.h> #include <tlhelp32.h> #include <qfileinfo> #include <qdebug>  waitforprocess::waitforprocess(qobject *parent) : qobject(parent) {     _timer = new qtimer(this);     connect(_timer, signal(timeout()), this, slot(ontimeout())); }  waitforprocess::~waitforprocess() {  }  void waitforprocess::ontimeout() {     if(findwin32process()) {         _timer->stop();     } }  void waitforprocess::start(const qstring& processname, int refresh) {     _processname = processname;     _timer->start(refresh); }  std::string utf8_encode(const std::wstring &wstr) {     int size_needed = widechartomultibyte(cp_utf8, 0, &wstr[0], (int)wstr.size(), null, 0, null, null);     std::string strto( size_needed, 0 );     widechartomultibyte (cp_utf8, 0, &wstr[0], (int)wstr.size(), &strto[0], size_needed, null, null);     return strto; }  bool waitforprocess::findwin32process() {     qstring otherprocess;      handle hprocesssnap;     processentry32 pe32;     hprocesssnap = createtoolhelp32snapshot(th32cs_snapprocess, 0);      if (hprocesssnap == invalid_handle_value)     {         return false;     }      pe32.dwsize = sizeof(processentry32);      if (!process32first(hprocesssnap, &pe32))     {         // clean snapshot object         closehandle(hprocesssnap);         return false;     }      // loop through running processes looking process         {         otherprocess = qstring(utf8_encode(std::wstring(pe32.szexefile)).c_str());         if (_processname.compare(otherprocess)==0)         {             // find path             handle hprocess = openprocess(process_query_information, false, pe32.th32processid);              wchar_t lpexename[1024];             dword lpdwsize = 1024;              if(hprocess != null)             {                 queryfullprocessimagename(hprocess, 0, lpexename, &lpdwsize);                 qstring fullpath = qstring(utf8_encode(std::wstring(lpexename)).c_str());                  // here i've found path of exe                  qfileinfo info(fullpath);                 qstring path = info.path();                 qstring name = info.filename();                  qdebug() << "found: " << name;                 qdebug() << "path: " << path;             }              // clean snapshot object             closehandle(hprocess);             closehandle(hprocesssnap);              return true;         }     } while(process32next(hprocesssnap, &pe32));      // clean snapshot object     closehandle(hprocesssnap);      return false; } 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -