2012-02-26 57 views
1

我有一個QTimer連接到TimerHandler函數。 TimerHandler應該執行我在我的標題的公開部分中聲明的SendKeys函數。如果我手動輸入SendKeys函數的文本,它會給出正確的輸出。但是如果我通過預定義的文本LPSTR它會輸出垃圾。這裏是我的代碼: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")); 

相反,它會正確輸出消息。我的代碼有問題嗎?

我試過在公開區MyProject.h裏宣佈LPSTR txtMessage,但這也行不通。

回答

1

製作txtMessage a string對象(std::stringQString,因爲您使用的是Qt),而不是指針。在打電話給SendText之前獲取該對象的指針,或者更容易,只需讓SendText接收字符串對象而不是指針。

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

的問題是,你存儲一個指向數據的臨時對象(convertme)。該對象超出範圍並被銷燬,內存被重寫。它使用「test message」的原因是string literals are stored differently。你需要保持你試圖存儲在內存中的信息。

+0

您先生保存我的整個項目再次。我會感謝你我的英雄。我不會忘記你的善意;) – HitomiTenshi 2012-02-27 00:44:19

相關問題