|
Paulraj G wrote: How to find a text file is already opened or not?
It quite difficult, try to open file in write and sharedenyall , if it fails that means somebody else already open the file otherwise none.
Preconditon: file must exists at stated path
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow Never mind - my own stupidity is the source of every "problem" - Mixture
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief and You
|
|
|
|
|
You can use CreateFile Function[] with "OPEN_EXISTING" flag in 5th argument(__in DWORD dwCreationDisposition) and use GetLastError Function to determine f the file opened already or not. Read complete documentation for the above.
--
"Programming is an art that fights back!"
|
|
|
|
|
Hello All,
I have a code snippet as follows
struct ItemNode
{
CString szID;
};
void DoSomething()
{
ItemNode *a = NULL;
TRY {
CString s;
s.Format (_T("%s"), a->szID);
}
CATCH(CException b){
MessageBox (_T("Exception occured"));
b->Delete ();
}
END_CATCH
return;
}
Here, I am not able to catch the exception. The app crashes saying "There is an access violation.... "
What is the correct way to catch the exception??
I am using C++, MFC and the development is in VisualStudio 2005.
|
|
|
|
|
Try like this for instance:
void DoSomething()
{
ItemNode *a = NULL;
TRY {
CString s;
s.Format (_T("%s"), a->szID);
}
CATCH(...){
MessageBox (_T("Exception occured"));
}
END_CATCH
}
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Computers are evil, EVIL i tell you!! <
|
|
|
|
|
shouldn't try...catch catch only C++ exceptions? An access violation is a crash, not an exception.
There is sufficient light for those who desire to see, and there is sufficient darkness for those of a contrary disposition.
Blaise Pascal
|
|
|
|
|
As far as i know access violation raises an exception, the crash occurs if this exception is nowhere cought in the program itself. I did use try...catch to catch access violations in the past and it worked fine, althorough doing so isn't a very..."nice" solution, but sometimes it can be necesarry.
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Computers are evil, EVIL i tell you!! <
|
|
|
|
|
Yeah. I thought, that the CException class should be able to catch all the exceptions.
But in my example, the app crashes without the exception being caught.
Im looking for a way to be able to catch that exception.
|
|
|
|
|
I was talking about the "not nice" part too. That's why I called it crash not exception. IMHO C++ exceptions should be an error reporting mechanism, whereas an access violation means your program just got corrupted.
There is sufficient light for those who desire to see, and there is sufficient darkness for those of a contrary disposition.
Blaise Pascal
|
|
|
|
|
True, but he asked how he could catch that exception, that is a way.
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Computers are evil, EVIL i tell you!! <
|
|
|
|
|
Tried this, but it is giving errors as "not enough actual parameters for macro 'CATCH'"
|
|
|
|
|
Try this way then
void DoSomething()
{
ItemNode *a = NULL;
try {
CString s;
s.Format (_T("%s"), a->szID);
}
catch(...){
MessageBox (_T("Exception occured"));
}
}
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Computers are evil, EVIL i tell you!! <
|
|
|
|
|
catch( ... ) is generally a really bad idea, it just eats anything and doesn't really tell you a lot. About the only place you should use it is around main so you at least get a hint that something out of the ordinary has occured and you've got a well serious problem:
int main()
try
{
}
catch( std::exception &e )
{
std::cout << "An error occurred: " << e.what() << std::endl;
}
catch( ... )
{
std::cout << "An error occurred: No idea what!" << std::endl;
}
Cheers,
Ash
PS: Using catch( ... ) around thread functions, message handlers and module boundaries can also be good as you don't want to throw through C-style or OS interfaces.
|
|
|
|
|
|
Tried this also...
Still the app shows "Encountered a problem and needs to close..."
Not able to see the messagebox.
|
|
|
|
|
Well, what is MessageBox anyways? IS it your own method? I mean, the MessageBox[^] method in the docs requires more than a string.
Anyways, if i put this in the InitInstance of a test app it displays me the message box as it should.
try {
char *c = NULL;
*c = 3;
} catch (...)
{
AfxMessageBox("Yikes");
}
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Computers are evil, EVIL i tell you!! <
|
|
|
|
|
To catch access violations and such, you need to use Win32 structured exception handling, like __try __except
There is sufficient light for those who desire to see, and there is sufficient darkness for those of a contrary disposition.
Blaise Pascal
modified on Friday, May 28, 2010 8:12 AM
|
|
|
|
|
VC++ has a method of converting structured exceptions into C++ exceptions( _se_set_translator() IIRC). It's handy to be able to converge SEH and C++ exceptions into the same handler - provided some muppet doesn't use it as a way to keep code limping along after a null pointer dereference.
Cheers,
Ash
|
|
|
|
|
A small code snippet would be very helpful
Or, can you post how to use the _se_set_translator(), according to the snippet i posted.
|
|
|
|
|
In general catching access violations is the wrong thing to do. If the access violation is coming from inside the TRY block in the code you posted it's more than just "the wrong thing to do", it's sheer madness. Find the real problem! Where exactly in the code is the crash?
Steve
|
|
|
|
|
|
No ... that's not a good solution.
If the file is corrupted you must be able to understand the corruption while reading the file, and throw yourself an exception if you cannot recover (and then handle the exception properly)
Doing the way you think may not working as expected.
If you are lucky, some pointer can be null, and an hardware exception is thrown during dereference, and you can catch it either using catch(...) or using the OS API support.
But if you're not, some pointer may be wrong, but still valid (I mean: pointing to something different to what it should be expected, but still inside your program memory space), hence dereferencing them doesn't throw anything. It simple made wrong results the user may even not recognize (and are the most subtle bug to fix).
Catching a bad dereference in case of data corruption to avoid a crash is not the solution. It's just an illusion to solve the problem, that may have also other consequences.
2 bugs found.
> recompile ...
65534 bugs found.
|
|
|
|
|
The problem is that an access violation isn't error reporting - it means that your program has memory corruption. There is a difference. An access violation ALWAYS means that your code isn't well done. You should simply change your code to avoid an access violation occurring at all. Fix your code. Debug your program, find where you access the NULL pointer, and put something like this:
if (ptr) {
ptr->szID;
}
else
throw std::exception("The pointer is NULL!");
If you really want exceptions, that's the way you do it. An exception is something you "throw". An access violation isn't an exception - it's a crash.
There is sufficient light for those who desire to see, and there is sufficient darkness for those of a contrary disposition.
Blaise Pascal
|
|
|
|
|
You've been told by basically every reply that using exceptions to catch access-violations is almost certainly a bad design. This is why standard compliant compilers don't allow it. If you insist on ignoring the advice, I guess there's isn't a lot more we can do. In the link you gave you said you can't check for NULLs yourself. Why not?
Steve
|
|
|
|
|
Hi all,
i am trying to display a image in dialog box. i have used this code to do it so far.
CString szFilename ("C:\\abc.bmp");
HBITMAP hBmp = (HBITMAP)::LoadImage(
NULL,
szFilename,
IMAGE_BITMAP,
0,
0,
LR_LOADFROMFILE|LR_CREATEDIBSECTION
);
m_st_pic.SetBitmap(hBmp);
My problem is i want to resize my image that is i want to display it according to a particular size...
but i am not getting how to do it...
Can anybody help in this...
|
|
|
|
|
I'm probably not completely aware of all the bitmap-sizing capabilities of the static control (i assume your m_st_pic is a static) but you could try drawing the bitmap in your dialog's OnPaint and use StretchBlt[^] to size it to whatever size you want to.
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Computers are evil, EVIL i tell you!! <
|
|
|
|