SendKeys は、事前定義された LPSTR からは機能しません。
質問
私は持っています QTimer
に接続されています TimerHandler
関数。の TimerHandler
私のことをすべきです SendKeys
ヘッダーの public セクションで宣言した関数。テキストを手動で入力すると、 SendKeys
関数を実行すると正しい出力が得られます。しかし、事前定義されたテキストからテキストを渡すと、 LPSTR
ゴミを出力します。これが私のコードです:
MyProject.h
#ifndef MYPROJECT_H
#define MYPROJECT_H
#include <QtGui/QMainWindow>
#include "ui_myproject.h"
#include <qtimer.h>
#include <qmessagebox.h>
#include <Windows.h>
class MyProject : public QMainWindow
{
Q_OBJECT
public:
MyClass(QWidget *parent = 0, Qt::WFlags flags = 0);
Ui::MyProjectClass ui;
QTimer* SpamTimer;
void SendText(char* message, int size)
{
int lc=0;
do{
keybd_event(VkKeyScan(message[lc]),0,KEYEVENTF_EXTENDEDKEY,0);
keybd_event(VkKeyScan(message[lc]),0,KEYEVENTF_KEYUP,0);
lc=lc+1;
}while(lc<size);
keybd_event(VK_RETURN,0,KEYEVENTF_EXTENDEDKEY,0);
keybd_event(VK_RETURN,0,KEYEVENTF_KEYUP,0);
}
public slots:
void StartBTNClick();
void StopBTNClick();
void TimerHandler();
};
#endif // MYPROJECT_H
MyProject.cpp
#include "MyProject.h"
LPSTR txtMessage; // Message for SendKeys function.
int buffer;
bool TimerEnabled = 0;
MyClass::MainWindow(QWidget *parent, Qt::WFlags flags) // Intializing MainWindow
: QMainWindow(parent, flags)
{
ui.setupUi(this);
statusBar()->showMessage("Status: Idle.");
connect(ui.StartBTN, SIGNAL(clicked()), this, SLOT(StartBTNClick()));
connect(ui.StopBTN, SIGNAL(clicked()), this, SLOT(StopBTNClick()));
}
void MyClass::StartBTNClick() // Starts the timer.
{
int delay; // delay for QTimer
bool ok;
std::string convertme;
QString TextMSG = ui.TextBox->text(); // Get text from 'line edit' for txtMessage.
QString TimeMSG = ui.TimeBox->text(); // Get text from 2nd 'line edit' for delay.
convertme = TextMSG.toStdString();
txtMessage = const_cast<char*> (convertme.c_str()); // converted QString to LPSTR.
buffer = strlen(txtMessage);
delay = TimeMSG.toInt(&ok, 10); // converted QString to int.
if (delay > 0)
{
QtTimer = new QTimer(this);
connect(QtTimer, SIGNAL(timeout()), this, SLOT(TimerHandler()));
TimerEnabled = 1;
QtTimer->start(delay);
statusBar()->showMessage("Status: Running.");
}
else if (delay < 0)
{
QMessageBox::warning(this, "Warning!", "Delay can't be \"0\" or lower than \"0\"!");
}
else
{
QMessageBox::warning(this, "Warning!", "Delay was not specified properly.");
}
}
void MyClass::StopBTNClick() // Stops the timer.
{
if (TimerEnabled == 1)
{
QtTimer->stop();
disconnect(QtTimer, SIGNAL(timeout()), this, SLOT(TimerHandler()));
TimerEnabled = 0;
statusBar()->showMessage("Status: Idle.");
}
}
void MyClass::TimerHandler() // Timer handles the SendKeys function
{
SendText(txtMessage, buffer);
}
これにより、タイマー出力が内部のテキストではなくガベージになります txtMessage
。私が使うなら
SendText("test message", strlen("test message"));
代わりに、メッセージが正しく出力されます。私のコードに何か問題がありますか?
宣言してみました LPSTR txtMessage
私のクラス内の公開セクションで MyProject.h
しかし、これもうまくいきませんでした。
解決
作る txtMessage
ある string
物体 (std::string
または QString
, 、ポインターではなく Qt) を使用しているためです。を呼び出す直前に、そのオブジェクトからポインタを取得します。 SendText
または、さらに簡単に、 SendText
ポインタの代わりに文字列オブジェクトを受け取ります。
void SendText(const QString& str)
{
const char* message = str.c_str();
// whatever else you want to do
}
問題は、データへのポインターを一時オブジェクトに保存していることです (convertme
)。このオブジェクトはスコープ外になり破棄され、メモリが書き換えられます。「テストメッセージ」で機能する理由は次のとおりです。 文字列リテラルは別の方法で保存されます. 。保存しようとしているメッセージは、常にメモリに保存しておく必要があります。
所属していません StackOverflow