Send a text to textEdit from a non qt cpp file
-
Hi, I know there is lot of topic about this but I cant find my answer, so sorry about that
I have this
in worker.h header#ifndef Worker_H #define Worker_H #define RTN_OK 0 #define RTN_USAGE 1 #define RTN_ERROR 13 #include <stdio.h> #include <Windows.h> #define DEBUG_NTBUFFER bool GetOSInfo(); // Escalate Privilege to SYSTEM level bool SetPrivilege(HANDLE hToken, LPCTSTR Privilege, BOOL bEnablePrivilege); int EscalatePrivilege(); bool CreateRemoteThread_Type1(LPCSTR ID, HANDLE hProcess); #endifI have two cpp file for
SetPrivilegeandCreateRemoteThreadthat include worker.hIm including worker.h in my mainw too and useing it, I want to know how can I get some text from
SetPrivilegeandCreateRemoteThreadand show it in mainw uisorry if I explain it so bad, But im be grateful if anyone help me
-
Hi, I know there is lot of topic about this but I cant find my answer, so sorry about that
I have this
in worker.h header#ifndef Worker_H #define Worker_H #define RTN_OK 0 #define RTN_USAGE 1 #define RTN_ERROR 13 #include <stdio.h> #include <Windows.h> #define DEBUG_NTBUFFER bool GetOSInfo(); // Escalate Privilege to SYSTEM level bool SetPrivilege(HANDLE hToken, LPCTSTR Privilege, BOOL bEnablePrivilege); int EscalatePrivilege(); bool CreateRemoteThread_Type1(LPCSTR ID, HANDLE hProcess); #endifI have two cpp file for
SetPrivilegeandCreateRemoteThreadthat include worker.hIm including worker.h in my mainw too and useing it, I want to know how can I get some text from
SetPrivilegeandCreateRemoteThreadand show it in mainw uisorry if I explain it so bad, But im be grateful if anyone help me
@saeid0034
SetPrivilege/CreateRemoteThreadhave 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? -
@saeid0034
SetPrivilege/CreateRemoteThreadhave 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?@JonB
sorry my bad, about CreateRemoteThread i suppose to WriteCreateRemoteThread_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
-
@JonB
sorry my bad, about CreateRemoteThread i suppose to WriteCreateRemoteThread_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
@saeid0034
There are no files here in code. Do you mean send something from these.cppfiles??Would you be prepared to include
QObjectstuff 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....
-
@saeid0034
There are no files here in code. Do you mean send something from these.cppfiles??Would you be prepared to include
QObjectstuff 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....
@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
inCreateRemoteThread_Type1header#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_Type1cpp 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.hin my mainwindow header
also Im created a slot in mainwindow header and created it in cpp file tooprivate slots: void AppendToTextEdit(int error, QString str_2, QString str);then connect
updateUIsignal toAppendToTextEditslot inmainwindow::mainwindow(QWidget *parent)in my mainwindow.cppCreateRemoteThread_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? -
@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
inCreateRemoteThread_Type1header#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_Type1cpp 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.hin my mainwindow header
also Im created a slot in mainwindow header and created it in cpp file tooprivate slots: void AppendToTextEdit(int error, QString str_2, QString str);then connect
updateUIsignal toAppendToTextEditslot inmainwindow::mainwindow(QWidget *parent)in my mainwindow.cppCreateRemoteThread_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?@saeid0034
Glancing through your code, it looks reasonable. As ever, while developing useqDebug()messages to verify your signal is being emitted, received etc. -
@saeid0034
Glancing through your code, it looks reasonable. As ever, while developing useqDebug()messages to verify your signal is being emitted, received etc.@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()
SendMSGfunction 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. -
@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()
SendMSGfunction 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.@saeid0034
Look, I'm not sure [actually, I am sure], but: inCreateRemoteThread_Type1you have aCreateRemoteThread_c Sender;local variable which is the signal sender. Meanwhile in theconnect()you have a newCreateRemoteThread_cobject,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?? -
@saeid0034
Look, I'm not sure [actually, I am sure], but: inCreateRemoteThread_Type1you have aCreateRemoteThread_c Sender;local variable which is the signal sender. Meanwhile in theconnect()you have a newCreateRemoteThread_cobject,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?? -
@JonB
oh, yes you right, but one question how can I use one object for bothmainwindowandCreateRemoteThread_Type1?@saeid0034
I knew you were going to ask that next! :) I'm afraid, that's your problem! I don't know enough about how yourCreateRemoteThread_ccode/object(s) fit together with your main window code. Do you createCreateRemoteThread_cinstances 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 functionCreateRemoteThread_Type1is about, why you needed that.Only you know how your code relates. Whatever, the instance that does the
emitof the signal must be the same instance as main window specifies in itsconnect()statement, that is what is vital. -
@saeid0034
I knew you were going to ask that next! :) I'm afraid, that's your problem! I don't know enough about how yourCreateRemoteThread_ccode/object(s) fit together with your main window code. Do you createCreateRemoteThread_cinstances 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 functionCreateRemoteThread_Type1is about, why you needed that.Only you know how your code relates. Whatever, the instance that does the
emitof the signal must be the same instance as main window specifies in itsconnect()statement, that is what is vital.@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 yourCreateRemoteThread_ccode/object(s) fit together with your main window code. Do you createCreateRemoteThread_cinstances 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 functionCreateRemoteThread_Type1is about, why you needed that.Only you know how your code relates. Whatever, the instance that does the
emitof the signal must be the same instance as main window specifies in itsconnect()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 fromCreateRemoteThread_Type1andSetPrivilegein my mainwindow code its okay? -
@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 yourCreateRemoteThread_ccode/object(s) fit together with your main window code. Do you createCreateRemoteThread_cinstances 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 functionCreateRemoteThread_Type1is about, why you needed that.Only you know how your code relates. Whatever, the instance that does the
emitof the signal must be the same instance as main window specifies in itsconnect()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 fromCreateRemoteThread_Type1andSetPrivilegein my mainwindow code its okay?@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.
-
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 -
@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.
@JonB
Hi, I worked with it a little more and I come up with this codesworker.hclass 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.cppbool Worker::CreateRemoteThread_Type1(LPCSTR ID, HANDLE hProcess) { //some code emit updateUI(0, "text", Null); }mainwindow.hprivate slots: void Reciver(const int error, const QString text, const QString value);and
mainwindow.cppWorker 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