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. Send a text to textEdit from a non qt cpp file
Qt 6.11 is out! See what's new in the release blog

Send a text to textEdit from a non qt cpp file

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 3 Posters 946 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.
  • JonBJ JonB

    @saeid0034
    SetPrivilege/CreateRemoteThread have nothing to do with getting text from anywhere.

    This is 100% native Windows code, no relation to Qt, and no idea what kind of help you are looking for anyway. Why you need to be using these native Windows functions and threads at all I have no idea; if you really need threads why not use QThreads in a Qt program?

    S Offline
    S Offline
    saeid0034
    wrote on last edited by
    #3

    @JonB
    sorry my bad, about CreateRemoteThread i suppose to Write CreateRemoteThread_Type1 . it name of a function in a cpp file, I use it for some things
    alse here is code for SetPrivilege

    #include "injector.h"
    
    bool SetPrivilege(HANDLE hToken, LPCTSTR Privilege, BOOL bEnablePrivilege) {
    
    	TOKEN_PRIVILEGES tp;
    	LUID luid;
    	TOKEN_PRIVILEGES tpPrevious;
    	DWORD cbPrevious = sizeof(TOKEN_PRIVILEGES);
    
    	if (!LookupPrivilegeValue(NULL, Privilege, &luid)) return false;
    
    	// First pass. get current privilege settings
    	tp.PrivilegeCount = 1;
    	tp.Privileges[0].Luid = luid;
    	tp.Privileges[0].Attributes = 0;
    
    	AdjustTokenPrivileges(
    		hToken,
    		FALSE,
    		&tp,
    		sizeof(TOKEN_PRIVILEGES),
    		&tpPrevious,
    		&cbPrevious);
    
    	if (GetLastError() != ERROR_SUCCESS) return false;
    
    	// second pass. set privileges based on previous settings
    
    	tpPrevious.PrivilegeCount = 1;
    	tpPrevious.Privileges[0].Luid = luid;
    
    	if (bEnablePrivilege) {
    		tpPrevious.Privileges[0].Attributes |= (SE_PRIVILEGE_ENABLED);
    	}
    	else {
    		tpPrevious.Privileges[0].Attributes ^= (SE_PRIVILEGE_ENABLED & tpPrevious.Privileges[0].Attributes);
    	}
    
    	AdjustTokenPrivileges(
    		hToken,
    		FALSE,
    		&tpPrevious,
    		cbPrevious,
    		NULL,
    		NULL
    	);
    
    
    	if (GetLastError() != ERROR_SUCCESS) return false;
    
    	return true;
    }
    
    
    void DisplayError(LPCSTR szAPI) {
    
    	LPTSTR MessageBuffer;
    	DWORD dwBufferLength;
    
    	fprintf(stderr, "%s() error!\n", szAPI);
    
    	if (dwBufferLength = FormatMessage(
    		FORMAT_MESSAGE_ALLOCATE_BUFFER |
    		FORMAT_MESSAGE_FROM_SYSTEM,
    		NULL,
    		GetLastError(),
    		GetSystemDefaultLangID(),
    		(LPTSTR)&MessageBuffer,
    		0,
    		NULL
    	)) {
    
    		DWORD dwBytesWritten;
    
    		// Output message string on stterr
    		WriteFile(
    			GetStdHandle(STD_ERROR_HANDLE),
    			MessageBuffer,
    			dwBufferLength,
    			&dwBytesWritten,
    			NULL
    		);
    
    		// Free the buffer alllocated by the system
    		LocalFree(MessageBuffer);
    	}
    
    }
    
    int EscalatePrivilege() {
    
    	HANDLE hToken;
    	int dwRetVal = RTN_OK;
    
    	if (!OpenThreadToken(GetCurrentThread(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, FALSE, &hToken)) {
    
    		if (GetLastError() == ERROR_NO_TOKEN) {
    			if (!ImpersonateSelf(SecurityImpersonation)) return RTN_ERROR;
    
    			if (!OpenThreadToken(GetCurrentThread(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, FALSE, &hToken)) {
    				DisplayError("OpenThreadToken");
    				return RTN_ERROR;
    			}
    
    		}
    		else {
    			return RTN_ERROR;
    		}
    	}
    
    	// Enable SetPrivilege()
    
    	if (!SetPrivilege(hToken, SE_DEBUG_NAME, TRUE)) {
    
    		DisplayError("Set Privilege");
    
    		//close token handle
    		CloseHandle(hToken);
    
    		//indicate failure
    		return RTN_ERROR;
    	}
    
    	return dwRetVal;
    }
    

    I want to know how can i send some text from these files to my mainw ui

    JonBJ 1 Reply Last reply
    0
    • S saeid0034

      @JonB
      sorry my bad, about CreateRemoteThread i suppose to Write CreateRemoteThread_Type1 . it name of a function in a cpp file, I use it for some things
      alse here is code for SetPrivilege

      #include "injector.h"
      
      bool SetPrivilege(HANDLE hToken, LPCTSTR Privilege, BOOL bEnablePrivilege) {
      
      	TOKEN_PRIVILEGES tp;
      	LUID luid;
      	TOKEN_PRIVILEGES tpPrevious;
      	DWORD cbPrevious = sizeof(TOKEN_PRIVILEGES);
      
      	if (!LookupPrivilegeValue(NULL, Privilege, &luid)) return false;
      
      	// First pass. get current privilege settings
      	tp.PrivilegeCount = 1;
      	tp.Privileges[0].Luid = luid;
      	tp.Privileges[0].Attributes = 0;
      
      	AdjustTokenPrivileges(
      		hToken,
      		FALSE,
      		&tp,
      		sizeof(TOKEN_PRIVILEGES),
      		&tpPrevious,
      		&cbPrevious);
      
      	if (GetLastError() != ERROR_SUCCESS) return false;
      
      	// second pass. set privileges based on previous settings
      
      	tpPrevious.PrivilegeCount = 1;
      	tpPrevious.Privileges[0].Luid = luid;
      
      	if (bEnablePrivilege) {
      		tpPrevious.Privileges[0].Attributes |= (SE_PRIVILEGE_ENABLED);
      	}
      	else {
      		tpPrevious.Privileges[0].Attributes ^= (SE_PRIVILEGE_ENABLED & tpPrevious.Privileges[0].Attributes);
      	}
      
      	AdjustTokenPrivileges(
      		hToken,
      		FALSE,
      		&tpPrevious,
      		cbPrevious,
      		NULL,
      		NULL
      	);
      
      
      	if (GetLastError() != ERROR_SUCCESS) return false;
      
      	return true;
      }
      
      
      void DisplayError(LPCSTR szAPI) {
      
      	LPTSTR MessageBuffer;
      	DWORD dwBufferLength;
      
      	fprintf(stderr, "%s() error!\n", szAPI);
      
      	if (dwBufferLength = FormatMessage(
      		FORMAT_MESSAGE_ALLOCATE_BUFFER |
      		FORMAT_MESSAGE_FROM_SYSTEM,
      		NULL,
      		GetLastError(),
      		GetSystemDefaultLangID(),
      		(LPTSTR)&MessageBuffer,
      		0,
      		NULL
      	)) {
      
      		DWORD dwBytesWritten;
      
      		// Output message string on stterr
      		WriteFile(
      			GetStdHandle(STD_ERROR_HANDLE),
      			MessageBuffer,
      			dwBufferLength,
      			&dwBytesWritten,
      			NULL
      		);
      
      		// Free the buffer alllocated by the system
      		LocalFree(MessageBuffer);
      	}
      
      }
      
      int EscalatePrivilege() {
      
      	HANDLE hToken;
      	int dwRetVal = RTN_OK;
      
      	if (!OpenThreadToken(GetCurrentThread(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, FALSE, &hToken)) {
      
      		if (GetLastError() == ERROR_NO_TOKEN) {
      			if (!ImpersonateSelf(SecurityImpersonation)) return RTN_ERROR;
      
      			if (!OpenThreadToken(GetCurrentThread(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, FALSE, &hToken)) {
      				DisplayError("OpenThreadToken");
      				return RTN_ERROR;
      			}
      
      		}
      		else {
      			return RTN_ERROR;
      		}
      	}
      
      	// Enable SetPrivilege()
      
      	if (!SetPrivilege(hToken, SE_DEBUG_NAME, TRUE)) {
      
      		DisplayError("Set Privilege");
      
      		//close token handle
      		CloseHandle(hToken);
      
      		//indicate failure
      		return RTN_ERROR;
      	}
      
      	return dwRetVal;
      }
      

      I want to know how can i send some text from these files to my mainw ui

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #4

      @saeid0034
      There are no files here in code. Do you mean send something from these .cpp files??

      Would you be prepared to include QObject stuff here so that you can send signals back to your Qt code?

      It's hard to say, maybe somebody else knows what you are asking because I am struggling.

      As I said, this code has nothing at all to do with Qt, and I have no idea how or why you're using it your Qt app....

      S 1 Reply Last reply
      1
      • JonBJ JonB

        @saeid0034
        There are no files here in code. Do you mean send something from these .cpp files??

        Would you be prepared to include QObject stuff here so that you can send signals back to your Qt code?

        It's hard to say, maybe somebody else knows what you are asking because I am struggling.

        As I said, this code has nothing at all to do with Qt, and I have no idea how or why you're using it your Qt app....

        S Offline
        S Offline
        saeid0034
        wrote on last edited by saeid0034
        #5

        @JonB
        I know I cant explain my problem right and im sorry about that
        the only thing I want is send a text from a cpp file that include my codes to mainwindow ui
        I tried this, but its wont work
        in CreateRemoteThread_Type1 header

        #pragma once
        
        #include <QObject>
        
        class CreateRemoteThread_c: public QObject
        {
        	Q_OBJECT
        
        public:
        	CreateRemoteThread_c(QObject *parent = Q_NULLPTR);
        	void SendMSG(int error, QString str_2, QString str);
        
        signals:
        	void updateUI(int error, QString str_2, QString str);
        };
        

        in in CreateRemoteThread_Type1 cpp file

        #include "Injector.h"
        #include "CreateRemoteThread_Type1.h"
        
        CreateRemoteThread_c::CreateRemoteThread_c(QObject *parent)
        	: QObject(parent)
        {
        }
        
        void CreateRemoteThread_c::SendMSG(int error, QString str_2, QString str)
        {
        	emit updateUI(error, str_2, str);
        }
        
        bool CreateRemoteThread_Type1(LPCSTR ID, HANDLE hProcess)
        {
        	CreateRemoteThread_c Sender;
        	Sender.SendMSG(0, "string", "string 2");
        
        }
        

        and after that I include CreateRemoteThread_Type1.h in my mainwindow header
        also Im created a slot in mainwindow header and created it in cpp file too

        private slots:
            void AppendToTextEdit(int error, QString str_2, QString str);
        

        then connect updateUI signal to AppendToTextEdit slot in mainwindow::mainwindow(QWidget *parent) in my mainwindow.cpp

        CreateRemoteThread_c* MsgSender = new CreateRemoteThread_c;
        
        	QObject::connect(MsgSender, &CreateRemoteThread_c::updateUI, this, &mainwindow::AppendToTextEdit);
        

        but slot never called
        sorry for long text, can you please tell me what is my problem?

        JonBJ 1 Reply Last reply
        0
        • S saeid0034

          @JonB
          I know I cant explain my problem right and im sorry about that
          the only thing I want is send a text from a cpp file that include my codes to mainwindow ui
          I tried this, but its wont work
          in CreateRemoteThread_Type1 header

          #pragma once
          
          #include <QObject>
          
          class CreateRemoteThread_c: public QObject
          {
          	Q_OBJECT
          
          public:
          	CreateRemoteThread_c(QObject *parent = Q_NULLPTR);
          	void SendMSG(int error, QString str_2, QString str);
          
          signals:
          	void updateUI(int error, QString str_2, QString str);
          };
          

          in in CreateRemoteThread_Type1 cpp file

          #include "Injector.h"
          #include "CreateRemoteThread_Type1.h"
          
          CreateRemoteThread_c::CreateRemoteThread_c(QObject *parent)
          	: QObject(parent)
          {
          }
          
          void CreateRemoteThread_c::SendMSG(int error, QString str_2, QString str)
          {
          	emit updateUI(error, str_2, str);
          }
          
          bool CreateRemoteThread_Type1(LPCSTR ID, HANDLE hProcess)
          {
          	CreateRemoteThread_c Sender;
          	Sender.SendMSG(0, "string", "string 2");
          
          }
          

          and after that I include CreateRemoteThread_Type1.h in my mainwindow header
          also Im created a slot in mainwindow header and created it in cpp file too

          private slots:
              void AppendToTextEdit(int error, QString str_2, QString str);
          

          then connect updateUI signal to AppendToTextEdit slot in mainwindow::mainwindow(QWidget *parent) in my mainwindow.cpp

          CreateRemoteThread_c* MsgSender = new CreateRemoteThread_c;
          
          	QObject::connect(MsgSender, &CreateRemoteThread_c::updateUI, this, &mainwindow::AppendToTextEdit);
          

          but slot never called
          sorry for long text, can you please tell me what is my problem?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #6

          @saeid0034
          Glancing through your code, it looks reasonable. As ever, while developing use qDebug() messages to verify your signal is being emitted, received etc.

          S 1 Reply Last reply
          1
          • JonBJ JonB

            @saeid0034
            Glancing through your code, it looks reasonable. As ever, while developing use qDebug() messages to verify your signal is being emitted, received etc.

            S Offline
            S Offline
            saeid0034
            wrote on last edited by saeid0034
            #7

            @JonB said in Send a text to textEdit from a non qt cpp file:

            Glancing through your code, it looks reasonable.

            as you said I check the code with qDebug()
            SendMSG function called but seems like signal doesn't emit
            I dont know what is my problem
            edit
            yes, I tested connect with lambda and tried to show a massage when signal received, but nothing happened.

            JonBJ 1 Reply Last reply
            0
            • S saeid0034

              @JonB said in Send a text to textEdit from a non qt cpp file:

              Glancing through your code, it looks reasonable.

              as you said I check the code with qDebug()
              SendMSG function called but seems like signal doesn't emit
              I dont know what is my problem
              edit
              yes, I tested connect with lambda and tried to show a massage when signal received, but nothing happened.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #8

              @saeid0034
              Look, I'm not sure [actually, I am sure], but: in CreateRemoteThread_Type1 you have a CreateRemoteThread_c Sender; local variable which is the signal sender. Meanwhile in the connect() you have a new CreateRemoteThread_c object, MsgSender. What is going on here? The connection must be from the object which sends the signal, and I think you have two completely separate objects here??

              S 1 Reply Last reply
              1
              • JonBJ JonB

                @saeid0034
                Look, I'm not sure [actually, I am sure], but: in CreateRemoteThread_Type1 you have a CreateRemoteThread_c Sender; local variable which is the signal sender. Meanwhile in the connect() you have a new CreateRemoteThread_c object, MsgSender. What is going on here? The connection must be from the object which sends the signal, and I think you have two completely separate objects here??

                S Offline
                S Offline
                saeid0034
                wrote on last edited by saeid0034
                #9

                @JonB
                oh, yes you right, but one question how can I use one object for both mainwindow and CreateRemoteThread_Type1?

                JonBJ 1 Reply Last reply
                0
                • S saeid0034

                  @JonB
                  oh, yes you right, but one question how can I use one object for both mainwindow and CreateRemoteThread_Type1?

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #10

                  @saeid0034
                  I knew you were going to ask that next! :) I'm afraid, that's your problem! I don't know enough about how your CreateRemoteThread_c code/object(s) fit together with your main window code. Do you create CreateRemoteThread_c instances more than once? If there really is only one you could use a singleton pattern. Or, main window can create it, I don't know what your non-class function CreateRemoteThread_Type1 is about, why you needed that.

                  Only you know how your code relates. Whatever, the instance that does the emit of the signal must be the same instance as main window specifies in its connect() statement, that is what is vital.

                  S 1 Reply Last reply
                  1
                  • JonBJ JonB

                    @saeid0034
                    I knew you were going to ask that next! :) I'm afraid, that's your problem! I don't know enough about how your CreateRemoteThread_c code/object(s) fit together with your main window code. Do you create CreateRemoteThread_c instances more than once? If there really is only one you could use a singleton pattern. Or, main window can create it, I don't know what your non-class function CreateRemoteThread_Type1 is about, why you needed that.

                    Only you know how your code relates. Whatever, the instance that does the emit of the signal must be the same instance as main window specifies in its connect() statement, that is what is vital.

                    S Offline
                    S Offline
                    saeid0034
                    wrote on last edited by
                    #11

                    @JonB said in Send a text to textEdit from a non qt cpp file:

                    @saeid0034
                    I knew you were going to ask that next! :) I'm afraid, that's your problem! I don't know enough about how your CreateRemoteThread_c code/object(s) fit together with your main window code. Do you create CreateRemoteThread_c instances more than once? If there really is only one you could use a singleton pattern. Or, main window can create it, I don't know what your non-class function CreateRemoteThread_Type1 is about, why you needed that.

                    Only you know how your code relates. Whatever, the instance that does the emit of the signal must be the same instance as main window specifies in its connect() statement, that is what is vital.

                    thank you for all your help and Sorry for bother you so much
                    at the end I think my best solution is change my code, one last question😅
                    you think if I include all my needed function from CreateRemoteThread_Type1 and SetPrivilege in my mainwindow code its okay?

                    JonBJ 1 Reply Last reply
                    0
                    • S saeid0034

                      @JonB said in Send a text to textEdit from a non qt cpp file:

                      @saeid0034
                      I knew you were going to ask that next! :) I'm afraid, that's your problem! I don't know enough about how your CreateRemoteThread_c code/object(s) fit together with your main window code. Do you create CreateRemoteThread_c instances more than once? If there really is only one you could use a singleton pattern. Or, main window can create it, I don't know what your non-class function CreateRemoteThread_Type1 is about, why you needed that.

                      Only you know how your code relates. Whatever, the instance that does the emit of the signal must be the same instance as main window specifies in its connect() statement, that is what is vital.

                      thank you for all your help and Sorry for bother you so much
                      at the end I think my best solution is change my code, one last question😅
                      you think if I include all my needed function from CreateRemoteThread_Type1 and SetPrivilege in my mainwindow code its okay?

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #12

                      @saeid0034 said in Send a text to textEdit from a non qt cpp file:

                      you think if I include all my needed function from CreateRemoteThread_Type1 and SetPrivilege in my mainwindow code its okay?

                      This is not ideal! You really want to keep separation between a Qt main window/program and some piece of Windows-specific code to do goodness what. Try to make a self-contained class out of what you have with this code. But this is a general programming issue.

                      S 1 Reply Last reply
                      1
                      • JoeCFDJ Offline
                        JoeCFDJ Offline
                        JoeCFD
                        wrote on last edited by JoeCFD
                        #13

                        send out your text via standard output std::cout in your cpp code and catch it in Qt code.
                        https://forum.qt.io/topic/80024/how-to-redirect-output-of-stdout-to-gui-qtextedit-shell-from-one-process-to-another-process/2

                        1 Reply Last reply
                        1
                        • S Offline
                          S Offline
                          saeid0034
                          wrote on last edited by saeid0034
                          #14

                          I manage to fix it wait add in function deceleration from worker.h to mainwindow.h
                          know I can send signal and receive it
                          I'm still don't know if its a good way to do this...

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            saeid0034
                            wrote on last edited by
                            #15

                            one other thing
                            know I can access ui from all c++ file I declared in header
                            its bad? and why?

                            1 Reply Last reply
                            0
                            • JonBJ JonB

                              @saeid0034 said in Send a text to textEdit from a non qt cpp file:

                              you think if I include all my needed function from CreateRemoteThread_Type1 and SetPrivilege in my mainwindow code its okay?

                              This is not ideal! You really want to keep separation between a Qt main window/program and some piece of Windows-specific code to do goodness what. Try to make a self-contained class out of what you have with this code. But this is a general programming issue.

                              S Offline
                              S Offline
                              saeid0034
                              wrote on last edited by saeid0034
                              #16

                              @JonB
                              Hi, I worked with it a little more and I come up with this codes

                              worker.h

                              class Worker : public QObject
                              {
                              	Q_OBJECT
                              
                              public:
                              	bool GetOSInfo();
                              	bool SetPrivilege(HANDLE hToken, LPCTSTR Privilege, BOOL bEnablePrivilege);
                              	void DisplayError(LPCSTR szAPI);
                              	int EscalatePrivilege();
                              	bool CreateRemoteThread_Type1(LPCSTR ID, HANDLE hProcess);
                              
                              signals:
                              	void updateUI(const int error, const QString text, const QString value);
                              
                              };
                              

                              CreateRemoteThread.cpp

                              bool Worker::CreateRemoteThread_Type1(LPCSTR ID, HANDLE hProcess) {
                              //some code
                              	emit updateUI(0, "text", Null);
                              }
                              

                              mainwindow.h

                              private slots:
                                  void Reciver(const int error, const QString text, const QString value);
                              

                              and mainwindow.cpp

                              Worker worker; //global
                              
                              Mainwindow::Mainwindow(QWidget *parent)
                                  : QMainWindow(parent)
                              {
                                  ui.setupUi(this);
                              		
                              	QObject::connect(&worker, &Worker::updateUI, this, &mainwindow::Reciver);
                              }
                              
                              //some code
                              
                              void mainwindow::Reciver(const int error, const QString text, const QString value)
                              {
                              	ui.textEdit->append(text);
                              }
                              

                              its working and slot Trigger when signal fired
                              I only want to know there is any better way then declare Worker globally in mainwindow? (i declare it globally because I need it some other function other then main)
                              and one other thing, after all this code ideal?

                              thanks for all your helps and sorry for bothering you so many times

                              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