Delete a file when dialog box is closed? - mfc

This is my code, and its working fine.
However I would like to remove the file by variable xmlfilepath which I have mentioned in the OnInitDialog()
BOOL CTestDlg::OnInitDialog()
{
CString xmlfilepath = _T("C:\\Project\\Test\\test.xml");
Navigate(xmlfilepath);
return TRUE;
}
void CTestDlg::OnClose()
{
CDHtmlDialog::OnClose();
remove("C:\\Project\\Test\\test.xml");
}

You probably want something like this:
class CTestDlg : public CDialog
{
...
CString m_xmlfilepath; // << put this somewhere in the definition
// of CTestDlg
...
}
BOOL CTestDlg::OnInitDialog()
{
m_xmlfilepath = _T("C:\\Project\\Test\\test.xml");
Navigate(m_xmlfilepath);
return TRUE;
}
void CTestDlg::OnClose()
{
CDHtmlDialog::OnClose();
remove(m_xmlfilepath);
}
This is really basic C++ knowledge. I suggest you learn the basics of C++ prior to experimenting with MFC.

Related

Control is getting be NULL mf

I have CPrinterDlg class which contains
// CPrinterDlg.h
public:
CEdit m_editRxFreq;
void print_SignalData(unsigned int freq, float wvlen);
// CPrinterDlg.cpp
void CPrinterDlg::print_SignalData(unsigned int freq, float wvlen)
{
m_editRxFreq.SetWindowTextW(L"ddd");
}
In order to access that function I did in MainFrm like this:
public:
CPrinterDlg m_PrinterDlg;
CPrinterDlg& getPrinterDlg() { return m_PrinterDlg; }
And from where I am calling print_SignalData(...) is CSMsg``` class
void CSockMsg::Send_SignalData(unsigned char* msg)
{
//..
CMainFrame* pMain = (CMainFrame*)AfxGetApp()->GetMainWnd();
pMain->getPrinterDlg().print_SignalData(freq, wvlen);
}
When I call print_SignalData(...) from one of the CPrinter function directly, it's working well. But, When I try to call it from CSMsg::Send_SignalData(unsigned char* msg) it's giving me Debug Assertion(...MFC\winocc.cpp Line: 242) from this point:m_editRxFreq.SetWindowTextW(L"ddd");.
And I see that m_editRxFreq is NULL.
So, how do you think why m_editRxFreq is getting be NULL? and how can I solve this problem??
Solved!
I was doing
public:
CPrinterDlg m_PrinterDlg;
CPrinterDlg& getPrinterDlg() { return m_PrinterDlg; }
in CMainFrm.h,
and actually Creating and Showing that dlg
CPrinterDlg dlg;
dlg.Create(...);
dlg.ShowWindow(...);
was in CxxView::OnInitialUpdate().
That's why when I called pMain->getPrinterDlg().print_SignalData(freq, wvlen); it was giving me NULL;
So in order to solve it,
I created CPrinterDlg dlg.Create(...) in CMainFrm class but not in CxxxView class.

c++ read access violation with extended classes [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
As the title says when I run my program, visual studio, give me that error. The class that produce that error is the following:
class UsesScene {
BaseScene & scene;
public:
UsesScene(BaseScene& scene) : scene(scene) {}
void Start() { scene.Start(); }
void EventHandler(SDL_Event ev) { scene.EventHandler(ev); }
void Update() { scene.Update(); }
void Draw(SDL_Renderer* renderer) { scene.Draw(renderer); }
};
The popup appears after } at the end of the initialization of the Start void.
Another code that can interact with these classes is the following:
void GameManager::regScene(UsesScene scene) {
if (display != NULL) {
UsesScene* ptr = &scene;
UsesScene** pptr = &ptr;
display = *pptr;
}
else
{
UsesScene* ptr = &scene;
UsesScene** pptr = &ptr;
buffer = *pptr;
}
}
the display & buffer are declared as
class GameManager
{
private:
SDL_Window* window;
SDL_Renderer* renderer;
UsesScene* display;
UsesScene* buffer;
bool isRunning;
bool reStart;
};
and for access to the display or buffer's parameter I use the following syntax:
display->Start();
and the last thing that has to do with this class is this part of code:
TestScene tScene;
UsesScene testScene(tScene);
this->regScene(testScene);
Test Scene simply extends BaseScene:
class BaseScene
{
private:
bool sceneloop = false;
public:
virtual void Start() { std::cout << "BasceScene::Start()" << std::endl; };
virtual void EventHandler(SDL_Event event) {};
virtual void Update() {};
virtual void Draw(SDL_Renderer* renderer) {};
void _toggleLoopMode() { sceneloop = !sceneloop; }
bool _sceneloop() { return sceneloop; }
};
How I can solve this?
ps:
some code that doesn't interfere with the Display and Buffer wasn't reported, if you need: free to ask
So, after a few hours of coding, thanks to Remy Lebeau, I've finally resolved this issue.
First I've changed the regScene() method and now looks like this:
void GameManager::regScene(UsersScene *scene) {
if (SceneManager::_display == NULL)
_display = scene;
else
_buffer = scene;
}
And then I pass the scene like this:
TestScene tScene;
UsersScene testScene(tScene);
sManager->regScene(&testScene);
Thanks to all.

Command Pattern: Where to create the Command items?

I've used the command pattern quite extensively, and it works well. However, what's usually not discussed is where the instances of the Commands are created.
The following examples illustrate this issue: A Document has a function setText() that sets the text:
class Document {
public:
void setText(const std::string text) {
if (commandManager()->isActive()) {
// called by SetTextCommand
m_text = text;
} else {
// called somewhere in the application
commandManager()->addAndExecute(new SetTextCommand(this, text));
}
}
std::string text() const { return m_text; }
CommandManager * commandManager() const { return m_commandManager; }
private:
std::string m_text;
CommandManager * m_commandManager;
}
Here, the SetTextCommand would execute document->setText(text) like this:
class SetTextCommand : public Command {
public:
SetTextCommand(Document * doc, const std::string & text)
: Command(), m_doc(doc), m_oldText(doc->text()), m_text(text)
{}
void redo() override {
m_doc->setText(m_text);
}
void undo() override {
m_doc->setText(m_oldText, false);
}
}
The SetTextCommand is processed by the CommandManager like this:
CommandManager::addAndExecute(Command * command) {
m_doc->commandManager()->setActive(true); // THIS IS THE TRICK
command->redo();
m_doc->commandManager()->setActive(false); // THIS IS THE TRICK
m_stack->push_back(command);
}
The trick here is, that when running redo(), CommandManager::isActive() is set to true. Hence, Document::setText() will set m_text.
Obviously, all Document setter functions must follow the if (commandManager()->isActive()) { ... } else { ... } paradigm. This is, because the Commands themselves are created in the setter functions.
Question is now: Is this a good way to implement the command pattern? Or are there far cleaner solutions for creating the Commands while at the same time having a nice API?
Please be verbose with your answers.
I think it'd be pretty ugly to have to replicate the if (commandManager()->isActive()) everywhere... probably nicer to have setText always do the SetTextCommand path, and create a new setTextImmediate method which SetTextCommand can use.

Irrlicht Keypresses

I am struggling to program keypresses within Irrlicht.
I have created an eventreciever as such:
class MyEventReceiver : public IEventReceiver
{
virtual bool OnEvent(const SEvent& event)
{
if (event.EventType == irr::EET_KEY_INPUT_EVENT)
{
KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;
{
if (event.EventType == irr::EET_KEY_INPUT_EVENT&&!event.KeyInput.PressedDown)
switch(event.KeyInput.Key)
{
case KEY_KEY_1:
case KEY_KEY_2:
case KEY_KEY_3:
}
return true;
}
}
return false;
}
virtual bool IsKeyDown(EKEY_CODE keyCode) const
{
return KeyIsDown[keyCode];
}
MyEventReceiver()
{
memset(KeyIsDown, false, sizeof(KeyIsDown));
}
private:
bool KeyIsDown[KEY_KEY_CODES_COUNT];
};
This all seems to be working as such. However, within "while(device->run())" I have implemented:
if(receiver.IsKeyDown(irr::KEY_KEY_1))
{
}
to which I get an error for my reciever "identifier reciever is undefined". On all examples I see, I see this reciever variable with no declarations and they claim it works. What am I doing wrong?
I am building on the example project "LoadIrrFile" (#15).
The plan is to implement a weapon switch for keys 1-3. I should be able to get the code in once I have the keypress initialised.
I am using the snippet I found here: http://irrlicht.sourceforge.net/forum//viewtopic.php?p=143082
Here is a full code segment if more info is required: http://pastie.org/pastes/8620301/text
The snippet there is only a patch. receiver is not declared in your main(). Have a look at the "complete" example at http://irrlicht.sourceforge.net/docu/example004.html. Your code lacks something along (from the example in the link above):
MyEventReceiver receiver; // declare it
IrrlichtDevice* device = createDevice(driverType,
core::dimension2d<u32>(640, 480), 16, false, false, false, &receiver); // use it here

How to initial static member in C++ using function

I am using C++.
in .h:
static CRITICAL_SECTION g_CS;
in .cpp:
CRITICAL_SECTION CQCommon::g_CS;
but I want to use
QGUID temp;
EnterCriticalSection(&g_CS);
temp = g_GUID++;
LeaveCriticalSection(&g_CS);
return temp;
in one static function.
How can I invoke InitializeCriticalSection(PCRITICAL_SECTION pcs);?
Can I using the following one:
QGUID func(XXX)
{
static {
InitializeCriticalSection(&g_CS);
}
QGUID temp;
EnterCriticalSection(&g_CS);
temp = g_GUID++;
LeaveCriticalSection(&g_CS);
return temp;
}
And how can I invoke DeleteCriticalSection(&g_CS) after app leave?
Using MFC, it seems CCriticalSection is a solution.
If you want a different approach you can create an object to manage it:
class CriticalSectionManager
{
public:
CriticalSectionManager()
{
InitializeCriticalSection(&g_CS);
}
~CriticalSectionManager()
{
DeleteCriticalSection(&g_CS);
}
};
void Func(void)
{
static CriticalSectionManager man;
//Do stuff
}
This will now be managed automatically by C++. The critical section will be initialized when the function is first entered, and deleted when the program exits.
Furthermore you can extend this by having the actual PCRITICAL_SECTION variable inside the class, etc.. etc..
In the entry point to your code - the main function, call the init:
int main(...)
{
InitializeCriticalSection(&g_CS);
// do some stuff
DeleteCriticalSection(&g_CS);
// exit
return 0;
}
Well, today the best practice is to use "scoped lock" pattern instead of EnterXXX and LeaveXX -like functions. Take a look at what boos has to offer.
Regardless, an RAII approach can help you here:
class MyCriticalSection
{
private:
CRITICAL_SECTION m_CS;
public:
MyCriticalSection()
{
::InitializeCriticalSection(&m_CS);
}
~MyCriticalSection()
{
::DeleteCriticalSection(&m_CS);
}
void Lock()
{
::EnterCriticalSection(&m_CS);
}
void UnLock()
{
::LeaveCriticalSetion(&m_CS);
}
}