2012-02-26 11 views
1

私は、機能に接続されたQTimerを持っています。 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

txtMessagestringオブジェクト(std::stringまたはQString、あなたは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です。あなたはあなたが記憶しようとしているメッセージを常に記憶しておく必要があります。

+0

あなたは私のプロジェクト全体をもう一度保存しました。私はあなたのヒーローに感謝します。私はあなたの優しさを忘れないだろう;) – HitomiTenshi

関連する問題