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
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 4 Posters 815 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.
  • Marko StankeM Offline
    Marko StankeM Offline
    Marko Stanke
    wrote on 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();
    
    JonBJ 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on 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
      • Marko StankeM Marko Stanke

        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();
        
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #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
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 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
          • Marko StankeM Offline
            Marko StankeM Offline
            Marko Stanke
            wrote on 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).

            JonBJ 1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 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
              • Marko StankeM Marko Stanke

                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).

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #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
                • Marko StankeM Offline
                  Marko StankeM Offline
                  Marko Stanke
                  wrote on 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?

                  JonBJ 1 Reply Last reply
                  0
                  • Marko StankeM Marko Stanke

                    @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?

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on 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?

                    Marko StankeM 1 Reply Last reply
                    0
                    • JonBJ JonB

                      @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?

                      Marko StankeM Offline
                      Marko StankeM Offline
                      Marko Stanke
                      wrote on 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.

                      JonBJ 1 Reply Last reply
                      0
                      • Marko StankeM Marko Stanke

                        @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.

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on 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.

                        Marko StankeM 1 Reply Last reply
                        3
                        • JonBJ JonB

                          @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.

                          Marko StankeM Offline
                          Marko StankeM Offline
                          Marko Stanke
                          wrote on last edited by Marko Stanke
                          #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

                          • Login

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