MFC Progress Bar Issue And Filing Menu Generation - c++

void combination::OnButton2()
{
// TODO: Add your control notification handler code here
m_progress.SetPos(0);
m_progress.SetRange(0,100);
combination a;
a.make_combinations(0);
}
void combination::make_combinations(int lo_val)
{
srand(time(0));
m_progress.StepIt();
ofstream fout("combination.txt",ios::app);
ofstream fout2("time.txt",ios::app);
for(int i=0; i<theApp.no_of_process; i++)
{
//m_progress.OffsetPos(100/4);
//m_progress.SetStep(200);
clock_t begin = clock();
arr[lo_val] = i;
if (lo_val == (theApp.no_of_tasks)-1)
{
for (int j=0; j<theApp.no_of_tasks; j++)
{
int number = arr[j];
fout << Matrix[j][number];
}
fout<<endl;
}
else
{
//Sleep(2);
//make_combinations(lo_val+1);
clock_t end = clock();
theApp.combination_time[i][0] = (diffclock(end, begin))/1000;
fout2 << theApp.combination_time[i][0] << endl;
}
}
}
there is a dialog on mfc with a button behine that button am calling a recursive function. i placed a progress bar on the same dialog that should tell me the progress of recursion. but iam gettin an error on clicking the button. debug assertion failed. your program caused an assertion failure. i dnt know what is wrong with my code. please help!!
ISSUE NO 2:
Iam making this project on MFC! it includes brute force capability. It also has file handling in it!! am stuck at another point! i have a couple of files with file writing! my project has multiple files .txt format! on the main MFC board i want to add an option for browsing those written files . they should open in the format they are written! any help how it can be done?? just like a browsing menu! help??

The assertion is probably because either the control does not exist on the dialog, or the variable m_progress has not been subclassed to the control. Make sure you have a DDX_Control entry for m_progress in your DoDataExchange function.

Related

Prevent GetAsyncKeyState() from getting blocked by Antivirus Software

I'm trying to create an afk money bot for a game.
I'm using GetAsyncKeyState() to start and stop the bot.
I've run the code a few times to try out a few things. Then I added a delay between calling the GetAsyncKeyState() function using the clock() function. When I tried to run the new code, I got an error stating that the .exe file was missing. I tried rebuiding or cleaning the project but it didn't work. Then I deleted the project, created a new one and copied the code back into the project. This did not work either, but I noticed a notification by my antivirus program: The .exe was detected as a fugrafa threat. I'm pretty confident that this was somehow caused by the GetAsyncKeyState() function since keyloggers can be recognized as a fugrafa threat.
There's gotta be a way to prevent this from happening, since I've seen GetAsyncKeyState() being used a lot.
Or do I really need to disable the antivirus in order to be able to use GetAsyncKeyState()?
Here's the Code:
#include <chrono>
#include <iostream>
#include <windows.h>
bool botActive = false;
void timeout(int);
int main()
{
bool F3_CurrentKeyState = GetAsyncKeyState(VK_F3);
bool F3_PreviousKeyState = F3_CurrentKeyState;
while (true)
{
//F3 Key Edge detection
F3_PreviousKeyState = F3_CurrentKeyState;
F3_CurrentKeyState = GetAsyncKeyState(VK_F3);
if (GetAsyncKeyState(VK_F3) && (F3_CurrentKeyState != F3_PreviousKeyState)) botActive = !botActive;
if (botActive)
{
std::cout << "Bot Active" << std::endl;
}
else
{
std::cout << "Bot Inactive" << std::endl;
}
timeout(100/*ms*/);
}
}
void timeout(int delay_ms)
{
clock_t toutStart = clock();
while (((float)clock() - toutStart) < delay_ms);
}

MFC app "Not Responding", although computation is already in separate thread

I didn't find sufficient answer for this, any suggestion is welcome..
I have a simple MFC single-document app, upon opening file a lengthy computation takes place in separate thread:
BOOL CrhMonkeyDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
if (!CDocument::OnOpenDocument(lpszPathName))
return FALSE;
CMainFrame* pMainFrame = (CMainFrame*)AfxGetMainWnd();
// Start working thread to process the file
m_rhFile.StartParseFile(lpszPathName);
//periodically check progress and update
int progress, lines;
while ((progress = m_rhFile.GetProgress()) < 1000) {
lines = m_rhFile.GetNumLines();
CString strProgress;
strProgress.Format(_T("%d lines, %d percent complete"), lines, progress);
pMainFrame->SetStatusBarText(strProgress);
Sleep(1000);
}
UpdateAllViews(NULL);
}
The thread is started like this:
UINT ParseFileThread(LPVOID Param)
{
RhFile* rhFile = (RhFile*)Param;
rhFile->ParseFile();
return TRUE;
}
int RhFile::StartParseFile(LPCTSTR lpszPathName)
{
m_file.open(lpszPathName);
AfxBeginThread(ParseFileThread, this);
return 0;
}
Initially it does work updating the status bar periodically, but after 10-15 seconds the updates stop and "Not Responding" appears in app title.
I have tried to start the thread at lower priority, and also added periodic SwitchToThread() (always returning 0), and Sleep(50) inside ParseFile(), but it doesn't help.
I guess I am doing something wrong, but can't figure out what
Thanks for reading this!

The code before Sleep() function doesn't work

I have a simple Qt application - a window with text area (QTextEdit) in in. I print some text into that area, push the button and get the response depending on what has been typed in. Here is the slot responsible for what must be done for a certain input. Block else is doing just fine. But there is a problem with the if. I want it to close the app if the 'close it, please' it typed in. Before closing it shoud change the text in the text area. So there is a need for delay before closing. I tried to implement it with a cycle - doesn't work, it thinks hard and then closes anyway without showin the message.
If I use Sleep() it does the same - waits and closes without changing the text area. Why does it happen? The setText() command is before the Sleep() function, why isn't it implemented before sleep?
void Layout::text_slot()
{
QString s=m_texter->toPlainText();
if (s=="close it, please")
{
m_texter->setText("OK, my Lord!");
//for (int i=0;i<10000;i++)
//for (int j=0;j<10000;j++)
Sleep(1000*10);
QApplication::quit();
}
else
{
m_texter->setText("What 're you saying?");
}
}
Use QTimer::singleShot to wait
e.g.
void Layout::text_slot()
{
QString s=m_texter->toPlainText();
if (s=="close it, please")
{
m_texter->setText("OK, my Lord!");
// TODO: disable any user interaction here
// e.g. disable text input field
QTimer::singleShot(1000*10, qApp, SLOT(quit()));
}
else
{
m_texter->setText("What 're you saying?");
}
}

How not to display system errors in C++?

I have a program that check some information about logical drives but if the drive like A:\ is not in it will display an error, like "
Windows - No Disk
Exception Processing Message C00000013 Parameters 75b76 etc
Cancel Try Again Continue
Is there any way to disable such errors to appear and just let the program continue or automatically press continue button ?
UPDATE:
DWORD drives = GetLogicalDrives();
for (int i = 0; i<26; i++)
{
if ((drives & (1 << i)))
{
TCHAR driveName[] = { TEXT('A') + i, TEXT(':'), TEXT('\\'), TEXT('\0') };
cout<<driveName;
}
}
If i run the code on XP it get the error that A:\ doesn't exists. Is there any way to disable this ?
You're looking for SetErrorMode(SEM_FAILCRITICALERRORS)

Qt MainWindow crashes upon button click

I've used Qt's MainWindow class before, and I have never had any problems.
Right now, I have a button called addLocation that is supposed to create a QInputDialog and get a string from the user, and add it to a QStringList called locationList, which is a public field of my uiactions.h file, declared like this within the header:
class UiActions{
public:
QStringList locationList;
//function prototypes
};
I have a function in my mainwindow.cpp file to handle the event of my addLocation button being clicked, defined as such:
void MainWindow::on_addLocation_clicked()
{
UiActions actions;
actions.addLocation();
}
And my addLocation function is defined as follows in my uiactions.cpp file:
void UiActions::addLocation()
{
bool j; //Input Dialog Handler
QDebug debugTxt(QtDebugMsg);
locationList << QInputDialog::getText(NULL, "Add Location",
"Location Name:", QLineEdit::Normal,
NULL, &j);
}
Whenever I click the addLocation button, the program shows an input dialog, lets me type something, and then hangs for a few seconds and crashes after clicking "ok."
I added some lines under the input dialog to see if the problem was in the QInputDialog, like this:
if(j){
debugTxt << "Input dialog success";
}
else{
debugTxt << "Input failed.";
}
And it would print out "Input dialog success" to the debug console, telling me the problem is not in my use of the QInputDialog.
When it crashes, this is all the error information I get:
The program has unexpectedly finished.
[insert path to EXE here] crashed
I believe this is all of the relevant information. What would be causing the program to crash?
EDIT: I ran with the debugger (should have done this first) and I'm getting a segmentation fault. I wonder what would be causing this, as I'm not doing any pointer trickery. I have a suspicion that it has something to do with the way I'm using QStringList locationList
The highlighted assembly is 0x64942cb6 <+0x0000> cmpl $0xbab1f00d,(%edx)
EDIT 2: I made a modification to the click event for my addLocation button, which is in my mainwindow.cpp file.
void MainWindow::on_addLocation_clicked()
{
UiActions actions;
//actions.addLocation();
actions.locationList << "Test Location" << "Other Data";
}
I get the same error when I try to add something to a QStringList field in the uiactions.h file as well. I'm almost positive I'm trying to access the data in there incorrectly now. Here's the interesting thing though. I have another button which I use to save the data in locationList to a text file, defined like this in my uiactions.cpp file:
bool UiActions::saveLocationList()
{
ofstream file;
file.open ("Location List.txt");
for(int i = 0;i < locationList.size(); i++){
file << locationList[i].toUtf8().data() << "\n";
}
file.close();
return true;
}
which works fine.
EDIT 3: I added a debug line: debugTxt << "\n" << locationList[0]; to the addLocation click event to see if the data was actually getting put into locationList, and it is. I'm completely confused now, since it seems I actually am accessing locationList correctly.