【问题标题】:SendKeys does not work from pre-defined LPSTR.SendKeys 不适用于预定义的 LPSTR。
【发布时间】:2012-02-26 23:08:25
【问题描述】:

我有一个QTimer 连接到一个TimerHandler 函数。 TimerHandler 应该执行我在标题的公共部分中声明的 SendKeys 函数。如果我手动输入 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

我的项目.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"));

相反,它会正确输出消息。我的代码有问题吗?

我尝试在 MyProject.h 的公共部分在我的班级内声明 LPSTR txtMessage,但这也没有用。

【问题讨论】:

    标签: c++ qt char sendkeys lpstr


    【解决方案1】:

    使txtMessage 成为string 对象(std::stringQString,因为您使用的是Qt),而不是指针。在调用 SendText 之前从该对象中获取指针,或者更简单的是,让 SendText 使用字符串对象而不是指针。

    void SendText(const QString& str)
    {
      const char* message = str.c_str();
      // whatever else you want to do
    }
    

    问题在于您将指向数据的指针存储在临时对象 (convertme) 中。该对象超出范围并被销毁,并且内存被重写。它与“测试消息”一起使用的原因是string literals are stored differently。您需要始终将尝试存储的消息保留在内存中。

    【讨论】:

    • 您先生再次保存了我的整个项目。我要感谢我的英雄。我不会忘记你的好意;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-24
    • 2014-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-18
    相关资源
    最近更新 更多