|
I've just found that the following puts the aplication into an assertion loop.
AddPrinterDialog::AddPrinterDialog()<br />
{<br />
try<br />
{<br />
mAddPrinterDialogPointer = new AddPrinterDialog;<br />
}<br />
catch(...)<br />
{<br />
<br />
}<br />
<br />
}
All I want is some code that has a constructor throwing an exception if it fails. It seems I can't use the current code.
That webpage was interesting, but the code was v.confusing. For example
<br />
C::C()<br />
try<br />
: A ( )
, b_( )<br />
{<br />
}<br />
catch( ... )<br />
{<br />
<br />
}<br />
<br />
is all very well but what if you're NOT inheriting? Or you don't care about parent classes (say you derived from an MFC class).
All I want is a default constructor throwing an exception. But I have no idea how you test for an object's existence, especially inside the default constructor's body as the object won't have been created yet.
Obseve everything, remember more...
|
|
|
|
|
First of all, you have infinite recursion in your construction - I didn't spot this in original post. Why are you keeping pointer to AddPrinterDlg inside AddPrinterDlg?
Second: as the article I've pointed you to said, object doesn't live (and, in fact, have never been born) when exception was thrown during construction. If you expect exceptions coming out of constructor, you should create your object in try/catch block.
Tomasz Sowinski -- http://www.shooltz.com
- It's for protection - Protection from what? Zee Germans?
|
|
|
|
|
Firstly thanks for your continued support. Much appreciated. I am understanding a bit more now.
The pointer is a member of AddPrinterDlg.
My instructions are that "every constructor that fails shall throw an exception" but I am having problems with this.
From what you say, should I assume that every time I create an object in my .cpp files I should put it in a try block?
Like so:
//... some code...
try
{
if ( !AddPrinterDlg DlgObject )
{
throw 100;
}
}
catch( int i )
{
cout << "Error: " << i << endl;
}
DlgObject.DoModal()
//... more code...
Obseve everything, remember more...
|
|
|
|
|
You should use try/catch when your objects may throw an exception from their constructors (and from other methods as well).
What kind of failures are you expecting in AddPrinterDlg constructor?
Tomasz Sowinski -- http://www.shooltz.com
- It's for protection - Protection from what? Zee Germans?
|
|
|
|
|
I'm not expecting any failures whatsoever, but it's got to be in as a safeguard.
So what do I put in the empty constructor body? do I need to put a try and catch block in there or do I just leave it blank?
Obseve everything, remember more...
|
|
|
|
|
tom76 wrote:
So what do I put in the empty constructor body?
Nothing. If your base class constructor throws, you're not going to catch it in constructor body. Standard C++ allows try/catch blocks outside body of the constructor (you could see examples at www.gotw.ca site), but Visual C++ doesn't support that, at least in version 6. Even if you could use try/catch outside of the c'tor body, your only option would be translating caught exception to some other type.
For more info on exceptions, I'd recommend reading "Exceptional C++" by Herb Sutter.
Tomasz Sowinski -- http://www.shooltz.com
- It's for protection - Protection from what? Zee Germans?
|
|
|
|
|
As someone else answer, you do not need to detect exception from inside of your constructor as exception will propagate to the caller.
And in client code, you should add try/catch only if you want to handle the exception (or do some cleanup before throwing it again). Generally, at some point, you may want to catch the exception and display something to the user and then either continue or quit from there...
Also as someone point out, there are good books if you want to learn more.
Philippe Mori
|
|
|
|
|
This is what we do because destructors are not called if an exception occurs in the constructor. We put try/catch blocks in constructors but also do cleanup of allocated memory if it fails. This is a good process for making sure you cleanup what was allocated before the failure occurred.
MyObject::MyObject()
{
// first set all ptrs to null.
m_pBlackCat = NULL;
m_pWhiteCat = NULL;
m_pDog = NULL;
// then allocate the memory or whatever process you're going to try to
// catch exception on
try
{
m_pBlackCat = new BlackCat();
m_pWhiteCat = new WhiteCat();
m_pDog = new Dog();
}
catch(...) // or catch CMemoryException& ce
{
CleanUp();
throw;
}
}
MyObject::~MyObject()
{
CleanUp();
}
MyObject::CleanUp();
{
// It's okay to delete, even if ptr is equal to NULL.
delete m_pBlackCat;
delete m_pWhiteCat;
delete m_Dog;
}
|
|
|
|
|
But if I do
try
{
m_pBlackCat = new MyObject;
}
(which is what I need to do as I am creating an object of type MyObject) I get a recursive loop (the constructor trying to call itself).
I cannot use try/catch in the .cpp file as the object goes out of scope like so
void DevModeDlg::OnButtonPrinter()
{
try
{
AddPrinterDialog Printerbox( this );
}
catch( CMemoryException &ce )
{
MessageBox( "Error", "Error", MB_OK );
}
Printerbox.DoModal();
}
Obseve everything, remember more...
|
|
|
|
|
// Default constructor
AddPrinterDialog::AddPrinterDialog()
{
mAddPrinterDialogPointer = NULL;
mPointerToParent = NULL;
/* If this pointer has failed to be assigned as specified then
construction has failed (object cannot, and therefore never
has, existed.
*/
if ( mAddPrinterDialogPointer != NULL )
{
throw int(100);
}
}
// The function where we try the constructor code.
void DevModeDlg::OnButtonPrinter()
{
try
{
DoOnButtonPrinterWork(); // Sneaky!
}
catch ( int ThrownInt )
{
MessageBox
(
"Error constructing object Printerbox of class AddPrinterDialog.",
"Error",
MB_OK
);
}
}
// Call the constructor from here. The object is in scope (if it was
// created ok) to be able to call DoModal(). Otherwise it breaks out
// and is caught be the catch all block in OnButtonPrinter.
void DevModeDlg::DoOnButtonPrinterWork()
{
AddPrinterDialog Printerbox( this );
Printerbox.DoModal();
}
Thank you to all who helped out!
Daniel
Obseve everything, remember more...
|
|
|
|
|
Dear All,
i want to develop a notepad as comes with MS windows.
it should support unicode like in notepad.
plz help me and guide me if there is code example
plz reffer me.
omar lodhi
|
|
|
|
|
There is a sample of WordPad that come with VC and should also be able to have most of Notepad or Wordpad functionality by generating an application with the Wizard. Simply select the appropriates options.
Philippe Mori
|
|
|
|
|
Hello,
Is there any good web resource teach or list all the tricks of code optimization with vc++? For example, if I define a struct with all 32-bit verable and run it with a 32-bit processor, the code will run faster. If I want to write some data in a file and define a struct like this:
struct filedata {
short a;
int b;
char c;
};
Since the struct is just going to use in-memory but write out to a file. I do not want to waste space by appending dummy veriable to make them all 32-bit. I'll have to access it like this...
filedata *pFiledata = (filedata *)memory_regon;
... use the pFiledata to update the memory_regon
Is this going to run a lot slower then appending dummy bytes into the struct? Is there any good why I can improve this code without modifing the data struct since the file format is already defined.
Thanks for advice!
Hiusing
|
|
|
|
|
|
|
The second argument of the winmain is HINSTANCE hPrevious. this is the handle to the previous instance of the application. how we can use it to control the creation of the application instance so that only one instance should be running at one time.
|
|
|
|
|
|
Yes, this parameter in a 32-bits application is always set to NULL. To test an instance of an application, you can for example test the existence of a Windows Class.
For instance, if you main window class name is "MyClassName", you can use the function FindWindow ("MyClassName", NULL). If this function return not NULL, then another instance of your application is running.
Thks in advance!
Appstmd
|
|
|
|
|
The hPrevious parameter is useless in Win32, it is only there for backward compatibility.
For an in-detail discussion of how to perform a correct instance check (the usual recommended ways to use FindWindow/Mutex lead to unexpected results ), look at this article.
Some ready-to-use code is available here, but I strongly recommend to read the above article first.
--
Daniel Lohmann
http://www.losoft.de
|
|
|
|
|
when i used my third party dll i got dll runtime error.i included lib file in project settings.In spite of that it gives error.i also included header files.
No dll was found in the procedure.
|
|
|
|
|
|
It seems to be loading problem. Is the dll in the directory so it can be found.
Are also the header, libs and dll of the same version?
Your error description is to short to answer more concrete.;)
|
|
|
|
|
Hi,
How can put tooltips for controls like combobox,button etc .. in a win32 application?
Thanks
Neha
|
|
|
|
|
Hi,
First thing you need to do is create a window for your tooltips. ex:
<br />
hwndToolTips = CreateWindow ( TOOLTIPS_CLASS, NULL, WS_POPUP, 0,0,0,0, hwndParent, NULL, hInstance, 0 );<br />
Once, you've created the tool tip window, you have to activate tooltips, do this by sending the TTM_ACTIVATE message to the Tool tip window (hwndToolTips).
Now, you can add tool tip to either a control or a rectangular area of the parent window. To add a tooltip, simply send the message:
SendMessage ( hwndToolTips, TTM_ADDTOOL, 0, &tooltip );
where "tooltip" is a TOOLINFO structure. (see MSDN)
There are more messages for tooltips, just look up "TTM_" in MSDN. You don't have to worry about destroying the tool tip window, Windows will take care of it for you since its a child of "hwndParent".
Fortran
|
|
|
|
|
Fortran and Neha, I am having the same type of problem that the original thread speaks of. I have created a window and I am sure that is being done correctly. I also set the position of the window, where I would like the tooltip to appear. The problem is, my call to add and activate the tooltip keeps failing. I am hoping by replying to this thread it will somehow get to one of you and you might be able to give me some insight. The following code is how I am initializing the toolinfo structure. Any help you might be able to provide would be greatly appreciated.
/* INITIALIZE MEMBERS OF THE TOOLINFO STRUCTURE */
ti.cbSize = sizeof(TOOLINFO);
ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
ti.hwnd = hwnd;
ti.hinst = _Module.GetModuleInstance();
ti.uId = (UINT_PTR)hwnd;
ti.lpszText = (LPTSTR)wszTTtext;
// ToolTip control will cover the whole window
ti.rect.left = rect.left;
ti.rect.top = rect.top;
ti.rect.right = rect.right;
ti.rect.bottom = rect.bottom;
if(!SendMessage(hwndTT,TTM_ACTIVATE,(WPARAM)TRUE,0)){
WriteLog(_T(" > Could not Activate TooltipControl\r\n"));
}
/* SEND AN ADDTOOL MESSAGE TO THE TOOLTIP CONTROL WINDOW */
if(!SendMessage(hwndTT, TTM_ADDTOOL, 0, (LPARAM) (LPTOOLINFO) &ti)){
WriteLog(_T(" > Did not Create TooltipControl\r\n"));
}
|
|
|
|