2012-02-26 3 views
1

함수에 연결된 QTimer이 있습니다. TimerHandler은 내 머리글의 공개 섹션에서 선언 한 내 SendKeys 기능을 수행해야합니다. SendKeys 함수의 텍스트를 수동으로 입력하면 정확한 결과를 얻을 수 있습니다. 그러나 사전 정의 된 LPSTR에서 텍스트를 전달하면 가비지가 출력됩니다. 여기 내 코드입니다 :미리 정의 된 LPSTR에서 SendKeys가 작동하지 않습니다.

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

관련 문제