Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Write-EventLog/New-EventLog shell calls through QProcess not working
QtWS25 Last Chance

Write-EventLog/New-EventLog shell calls through QProcess not working

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 4 Posters 794 Views
  • 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.
  • M Offline
    M Offline
    Marko Stanke
    wrote on 7 Dec 2020, 18:08 last edited by
    #1

    The idea is to write to Windows event logs through shell commands. (I'm using Windows 10)

    This simple call from the shell is working perfectly fine: New-EventLog -LogName Application -Source "Test"

    But when trying to call it by using QProcess, nothing happens. It seems like the call isn't even made.

    QString exec = "New-EventLog";
    QStringList params;
    
    params << "-LogName" << "Application";
    params << "-Source" << "Test";
    p->start(exec, params);
    p->waitForFinished();
    qDebug() << p->readAllStandardOutput();
    
    J 1 Reply Last reply 7 Dec 2020, 18:17
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 7 Dec 2020, 18:16 last edited by
      #2

      Hi and welcome to devnet,

      You should add error checking like verifying the status code and stderr.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • M Marko Stanke
        7 Dec 2020, 18:08

        The idea is to write to Windows event logs through shell commands. (I'm using Windows 10)

        This simple call from the shell is working perfectly fine: New-EventLog -LogName Application -Source "Test"

        But when trying to call it by using QProcess, nothing happens. It seems like the call isn't even made.

        QString exec = "New-EventLog";
        QStringList params;
        
        params << "-LogName" << "Application";
        params << "-Source" << "Test";
        p->start(exec, params);
        p->waitForFinished();
        qDebug() << p->readAllStandardOutput();
        
        J Online
        J Online
        JonB
        wrote on 7 Dec 2020, 18:17 last edited by JonB 12 Jul 2020, 18:26
        #3

        @Marko-Stanke
        New-EventLog is not a runnable Windows command, e.g. try it from a Command Prompt. It's probably a PowerShell thing? So you have to invoke it that way, via PowerShell executable.

        This simple call from the shell is working perfectly fine: New-EventLog -LogName Application -Source "Test"

        And what shell would that be from?

        Personally I would not write to the Windows Event Log via a process/PowerShell. I would do it inside your own program's code. Either search for a Qt way to do it (edit I searched, there isn't), or use Windows native calls.

        1 Reply Last reply
        2
        • C Online
          C Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 7 Dec 2020, 18:18 last edited by
          #4

          @JonB said in Write-EventLog/New-EventLog shell calls through QProcess not working:

          It's probably a PowerShell thing?

          Yes

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Marko Stanke
            wrote on 7 Dec 2020, 18:36 last edited by
            #5

            Thank you for the welcome. And thank you for all the useful information. I didn't know that it was a PowerShell-only thing, with all the help of you I managed to find a workaround that works.

            #ifdef _WIN32
                std::string cmd = "powershell New-EventLog -LogName Application -Source 'Test'";
                system(cmd.c_str());
            #endif
            

            I already tried searching for a solution to do it inside my own program's code, the thing is I had some issues with using <system.dll> and <mscorlib.dll>. And the things I found didn't help me much (It's probably my lack of experience).

            J 1 Reply Last reply 7 Dec 2020, 18:54
            0
            • C Online
              C Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 7 Dec 2020, 18:53 last edited by
              #6

              And why do you now use system() instead QProcess?

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              0
              • M Marko Stanke
                7 Dec 2020, 18:36

                Thank you for the welcome. And thank you for all the useful information. I didn't know that it was a PowerShell-only thing, with all the help of you I managed to find a workaround that works.

                #ifdef _WIN32
                    std::string cmd = "powershell New-EventLog -LogName Application -Source 'Test'";
                    system(cmd.c_str());
                #endif
                

                I already tried searching for a solution to do it inside my own program's code, the thing is I had some issues with using <system.dll> and <mscorlib.dll>. And the things I found didn't help me much (It's probably my lack of experience).

                J Online
                J Online
                JonB
                wrote on 7 Dec 2020, 18:54 last edited by JonB 12 Jul 2020, 18:55
                #7

                @Marko-Stanke
                That works. You chose to move over to system(). You could also have done this as per your original with QProcess, just make "powershell" be your exec and "New-EventLog" be the first of your params.

                1 Reply Last reply
                3
                • M Offline
                  M Offline
                  Marko Stanke
                  wrote on 7 Dec 2020, 19:05 last edited by
                  #8

                  @Christian-Ehrlicher because I need to implement it in a way without providing the absolute/relative path to the Powershell.

                  I'm guessing ( As I said I'm not too experienced ) that system() is reading the powershell path from the system environment variables and QProcess not.

                  Is there a way to run this command through QProcess without providing the absolute/relative path?

                  J 1 Reply Last reply 7 Dec 2020, 19:14
                  0
                  • M Marko Stanke
                    7 Dec 2020, 19:05

                    @Christian-Ehrlicher because I need to implement it in a way without providing the absolute/relative path to the Powershell.

                    I'm guessing ( As I said I'm not too experienced ) that system() is reading the powershell path from the system environment variables and QProcess not.

                    Is there a way to run this command through QProcess without providing the absolute/relative path?

                    J Online
                    J Online
                    JonB
                    wrote on 7 Dec 2020, 19:14 last edited by
                    #9

                    @Marko-Stanke
                    If you can pass powershell as the first word to system(), you should be able to pass that as the executable to your p->start(). Have you actually tried doing so?

                    M 1 Reply Last reply 7 Dec 2020, 19:30
                    0
                    • J JonB
                      7 Dec 2020, 19:14

                      @Marko-Stanke
                      If you can pass powershell as the first word to system(), you should be able to pass that as the executable to your p->start(). Have you actually tried doing so?

                      M Offline
                      M Offline
                      Marko Stanke
                      wrote on 7 Dec 2020, 19:30 last edited by
                      #10

                      @JonB Yes, it works. Not receiving any output made me think that it doesn't.
                      This code should return an error as this -Source doesn't exist, but I'm receiving no output.

                      QString exec = "powershell";
                      QStringList params;
                      
                      params << "Write-EventLog";
                      params << "-LogName" << "Application";
                      params << "-Source" << "Test";
                      p->start(exec, params);
                      p->waitForFinished();
                      if (!p->waitForFinished())
                          qDebug() << "Error output:" << p->errorString();
                      else
                          qDebug() << "Output:" << p->readAll();
                      

                      Note: For now i'm using waitForFinished() for simplicity of representing the code.

                      J 1 Reply Last reply 7 Dec 2020, 22:14
                      0
                      • M Marko Stanke
                        7 Dec 2020, 19:30

                        @JonB Yes, it works. Not receiving any output made me think that it doesn't.
                        This code should return an error as this -Source doesn't exist, but I'm receiving no output.

                        QString exec = "powershell";
                        QStringList params;
                        
                        params << "Write-EventLog";
                        params << "-LogName" << "Application";
                        params << "-Source" << "Test";
                        p->start(exec, params);
                        p->waitForFinished();
                        if (!p->waitForFinished())
                            qDebug() << "Error output:" << p->errorString();
                        else
                            qDebug() << "Output:" << p->readAll();
                        

                        Note: For now i'm using waitForFinished() for simplicity of representing the code.

                        J Online
                        J Online
                        JonB
                        wrote on 7 Dec 2020, 22:14 last edited by
                        #11

                        @Marko-Stanke
                        The "error" you are talking about is not an error running the process. It is just output it will produce. You need to fetch both p->readAllStandardOutput() and p->readAllStandardError(), it's probably in the latter.

                        M 1 Reply Last reply 8 Dec 2020, 22:47
                        3
                        • J JonB
                          7 Dec 2020, 22:14

                          @Marko-Stanke
                          The "error" you are talking about is not an error running the process. It is just output it will produce. You need to fetch both p->readAllStandardOutput() and p->readAllStandardError(), it's probably in the latter.

                          M Offline
                          M Offline
                          Marko Stanke
                          wrote on 8 Dec 2020, 22:47 last edited by Marko Stanke 12 Aug 2020, 22:54
                          #12

                          @JonB that's it! Thanks, works perfect now.
                          To sum up, this is the working code. "New-EventLog" is the same only with less params (LogName, Source needed only).

                          p = new QProcess();
                          QString exec = "powershell";
                          QStringList params;
                          
                          params << "Write-EventLog";
                          params << "-LogName" << " 'Application' ";
                          params << "-Source" << " 'Test' ";
                          params << "-EventID" << "3001";
                          params << "-EntryType" << "Warning";
                          params << "-Message" << " 'Custom message' ";
                          params << "-RawData"  << "10,20";
                          
                          p->start(exec, params);
                          p->waitForFinished();
                          if (!p->waitForFinished())
                              qDebug() << "Error output:" << p->readAllStandardError();
                          else
                              qDebug() << "Output:" << p->readAllStandardOutput();
                          1 Reply Last reply
                          0

                          4/12

                          7 Dec 2020, 18:18

                          topic:navigator.unread, 8
                          • Login

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