|
Thank you, I think this is what I need.
|
|
|
|
|
I'd like to have a piece of code executed upon definite end of a program. atexit() is not appropriate because there may be some code executed when destructing static objects and this function does things beforehand.
|
|
|
|
|
|
Keep in mind that if your program is terminated by Task Manager, it won't have the chance to run any code at all.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Just do what CPallini recommended. If you would do the same on linux then you could easily do the same trick with fork() without using 2 binaries.
|
|
|
|
|
CPallian gave a good idea.
Maybe you can do this in other way.
You can complete a dll for this. when a dll is detached, the branch named "DLL_PROCESS_DETACH" of Dllmain. yeah, it's your wanted place.
Nice to make friends with you !
|
|
|
|
|
What about something like:
#include <...>
#include "..."
class dummy
{
public:
dummy::dummy()
{
...code here is executed before main()
}
dummy::~dummy()
{
...code here is executed after main()
}
};
void main( void )
{
...do some stuff
}
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
Perfect ideas, using the order of constructor of a object.
But the question is how can you control this object is destructed behind the any other one?
Nice to chat with you !
|
|
|
|
|
Heng Xiangzhong wrote: But the question is how can you control this object is destructed behind the any other one?
You can control it is constructed before main execution (and destructed after main execution) making it global, e.g.
#include <iostream>
using namespace std;
class A
{
public:
A(){cout << "A ctor" << endl;}
~A(){cout << "A dtor" << endl;}
};
A a;
int main()
{
cout << "main" << endl;
}
Veni, vidi, vici.
|
|
|
|
|
Yeah, you are right.
It is all they can do. But it is too simple to complete the most situations.
for example, i want execute a piece of code behind the all of objects' destruct function is invoked.
if this, dll is a good way.
|
|
|
|
|
Thanks for all replies. Anyway, the solution requires quite tricky programming.
Heng Xiangzhong wrote: for example, i want execute a piece of code behind the all of objects' destruct function is invoked. ...and is right.
I would say it looks much more difficult than using simple atexit() function and I wonder why. Probably it belonged to early C standard libraries and no one invented anything better at C++ times.
I'm not sure whether dll is a solution because the same question remains: if I'd like to execute some code after all static objects in dll are disposed, I'd come to the same point of interest.
|
|
|
|
|
Let met think.
If you don't think it is complex, hook is a good solution for this.
You can hook the ExitProcess API in win32 by inline, IAT or something you can do.
Because a process exits in windows through invoke the ExitProcess by itself, so after CRT lib destructs all the objects,
it will invoke ExitProcess, at that time, the control flow will give you a chance to execute your code.
If you don't know how to complete it, I can give a piece of code that i wrote before.
Nice to make friends with you !
My email, Henzox.7@gmail.com
|
|
|
|
|
Please read the following code:
class MyString {
private:
char *str;
public :
MyString():str(NULL){}
MyString(const char* iStr);
MyString(const MyString& iMyStr);
~MyString();
MyString& operator=(const MyString &iMystr);
friend char* operator+(const char* iStr, const MyString& iMyStr);
};
char* operator+(const char* iStr, const MyString& iMyStr)
{
char * newStr = NULL;
newStr = new char[strlen(iStr)+strlen(iMyStr.c_str()) + 1];
strcpy(newStr,iStr);
strcat(newStr,iMyStr.c_str());
return newStr; }
int main()
{
MyString S1("fgh");
MyString S2("abc");
S1 = "cde" + S1;
return 0;
}
I set break point, and I found if I use commented statements, compiler popups exception.
But I think both commented statements ,and non comment statements
are same.both use constructor to construct a temporary object.
Don't know why it does not work.
|
|
|
|
|
Well of course it gives a compile error, you have declared the return value of operator+ to be char* instead of MyString& .
char* operator+(const char* iStr, const MyString& iMyStr)
{
char * newStr = NULL;
newStr = new char[strlen(iStr)+strlen(iMyStr.c_str()) + 1];
strcpy(newStr,iStr);
strcat(newStr,iMyStr.c_str());
return newStr;
}
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
In class declaration, it has a constructor:
MyString(const char * istr);
|
|
|
|
|
The implementation of your overload must match the definition, in parameters and return type. You are trying to define the overload as:
friend MyString& operator+(const char* iStr, const MyString& iMyStr);
and the implementation as:
char* operator+(const char* iStr, const MyString& iMyStr)
{
}
The return types are different.
|
|
|
|
|
Please don't do that.
Such a operator should only return a MyString reference.
Veni, vidi, vici.
|
|
|
|
|
but, I use :
return MyString(newStr);
I got a compile error message.
|
|
|
|
|
You have to change the method signature.
It is safe to add a C -like string to a MyString object and then return a reference to the latter:
MyString & operator+(MyString & iMyStr, const char * str);
Veni, vidi, vici.
|
|
|
|
|
I am testing the constructor call with the code below:
#include <iostream>
class A
{
public:
A(int n = 0)
: m_n(n)
{
}
A(const A& a)
: m_n(a.m_n)
{
}
private:
int m_n;
};
int main()
{
A a(2), b(1);
const A c(a);
const A &d = c;
const A e = b;
b = d;
return 0;
}
Everything is OK with me.
But there is one point that I cannot understand is that: At the line "b = d" (Note 2), why the copy constructor is not called while at the line "const A e = b" (Note 2) the constructor is called?
I expect that at line Note 2, the copy constructor will be called to copy the value of "c" (referenced by "d") to "b", but it doesn't happen.
Please explain it, thank you in advance!
|
|
|
|
|
Hi,
You are not invoking the assignment operator with your const A e = b; . You are constructing a new object of const A e and performing initialization by copy of b .
This is covered in section 8.5 paragraph 15 of the latest standard.
www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3690.pdf[^]
Best Wishes,
-David Delaune
|
|
|
|
|
Thanks Randor for your prompt reply.
As your answer, I can conclude that the copy constructor will be call for initialization only, not for "=" (assignment operator). Since long time ago, I have believed that the copy constructor will be call for "=" operator if there is no "=" operator defined for that class.
But I still wonder this: If there is no "=" operator defined for that class, what will be implemented for "=" operator? Binary copy?
Thanks.
|
|
|
|
|
Hi,
trinh.nguyen wrote: But I still wonder this: If there is no "=" operator defined for that class, what will be implemented for "=" operator? Binary copy?
If you do not write an assignment operator for your class the compiler will generate a default assignment operator that performs a memberwise copy[^] of base classes and members.
Have a look at section 12.8 paragraph 15:
www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3690.pdf[^]
Best Wishes,
-David Delaune
|
|
|
|
|
Hello,
I want to make a USB write blocker with c++ to work for linux. I have been looking for open source USB write blockers, but I just found few for windows that disable the writing functionality from the registry. The idea is to prevent the OS from changing the USB devices timestamps while plugging the device for cloning or analysis the data.
I need help in figure out the best way to do it.
Thank you.
|
|
|
|
|
Override ::
MSVC > Class View > Properties > Override >
OnKillActive , OnSetActive
/*Dialog ( in sheet page ) */
virtual BOOL OnKillActive();
virtual BOOL OnSetActive();
/*exit page*/
BOOL CMyPropertyPage1::OnKillActive()
{
TRACE(_T("OnKillActive\n"));
return CMFCPropertyPage::OnKillActive();
}
/* entrance page*/
BOOL CMyPropertyPage1::OnSetActive()
{
TRACE(_T("OnSetActive\n"));
return CMFCPropertyPage::OnSetActive();
}
Visual C
|
|
|
|