|
If there is not enough memory, new will throw bad_alloc exception for you anyway and you will never even get to your NULL test.
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
|
|
|
|
|
> If there is no memory how can i throw an exception?
Never *ever* throw exceptions which allocate memory dynamically (or dynamically allocate the exception object itself), when you are out of memory. Things may happen to your application which you may not recover from if you had a thousand catch clauses (I love paraphrasing lyrics by the way )
As Nemanja says, "reuse" the exception which new provides for you. You'll be much better off that way.
--
Pictures[^] from my Japan trip.
|
|
|
|
|
At least in my case. What I am trying to do is to obtain an icon from an executable and to add it among with exe name to the report-styled list control.
<br />
if(ExtractIconEx(str,0,NULL,NULL,0) > 0)<br />
{<br />
ExtractIconEx(str,0,NULL,&hIcon,1);<br />
m_plistIcons->Add(hIcon);<br />
}<br />
else<br />
m_plistIcons->Add(m_hIcon);<br />
What I get is while every run the handle is different and after the loop ends image list contains 37 entries the only icon which is applied to absolutly all list items is the first added to the image list.
Image list is being attached to list control using this line:
m_ProcList.SetImageList(m_plistIcons,LVSIL_SMALL);
I am quite sure I'm doing something wrong. I just dunno what.
-- modified at 4:49 Saturday 10th December, 2005
|
|
|
|
|
Nobody knows ? Not a single human being ? I take help from aliens as well...as long as you speak the language I speak
|
|
|
|
|
Maybe it depends on how you add the items to your list control. By default the CListCtrl assumes to use image 0 for all items. You have to specify for every item which image it should use.
|
|
|
|
|
Wow. Some people wonder outside of page 1 Anyway I've figured it out on my own but thanks anyway.
--------------------------------
Human stupidity is infinite.
|
|
|
|
|
hi guys
Im trying me in managed C++ Appications. I have a string ( System::String^ item; ) an i want to compare it with another one ( "abc" ). My first idea was strstr but it dont works ( something like cannot convert ... ) Now its your turn. I need a similiar function that works like strstr in managed c++
Salut!
|
|
|
|
|
MaTTEvIL wrote: hi guys
Im trying me in managed C++ Appications. I have a string ( System::String^ item; ) an i want to compare it with another one ( "abc" ). My first idea was strstr but it dont works ( something like cannot convert ... ) Now its your turn. I need a similiar function that works like strstr in managed c++
Salut!
strstr does not compare two strings, it is used to search a string for a substring. You probably mean strcmp .
For managed strings, use String::Compare .
Alternatively you can compare the String handles directly using the == operator, because due to string interning, all handle variables to the same string will have the same handle value.
|
|
|
|
|
Anyone use this product? If so, did you update it for VC7+? If so...I need to talk to you!
ed
~"Watch your thoughts; they become your words. Watch your words they become your actions.
Watch your actions; they become your habits. Watch your habits; they become your character.
Watch your character; it becomes your destiny."
-Frank Outlaw.
|
|
|
|
|
Hello everybody,
i'm using Word automation to generate some data reports in word for printing.
Because my program is a top-level window i need to bring the word window to the top level
to make it accessible. I use a method explained here.
This method of changing the caption with automation and find it with the API FindWindow works like
a charm with Word97. But with Word 2003 the FindWindow API finds nothing . I searched a couple of hours on the internet, but I can't seem to find some answer/hint on how to pull it off.
All suggestions are welcome .
Greets Remco.
|
|
|
|
|
you can use SPY++ to find Word 2003 window,then you can get some information about the Word 2003 window,it help you to use the API FindWindow function.
|
|
|
|
|
Hello sun_shb,
I tried it with spy++, i used the class of the word window and the custom caption i gave it, but still FindWindow is unable to find it.
|
|
|
|
|
I need to open a file from the a CD rom. This application must run on multiple computers, each with a different drive letter for the CD drive. Is there a simple call to obtain which letter the cd drive is on without searching each drive letter A-Z for that file? (I know it will never be A or C)
Currently I use the _chdrive in a for loop to traverse through all possibilities.
Thanks in advance.
|
|
|
|
|
Maybe use RealDriveType Shell function instead ?
|
|
|
|
|
You can call GetDriveType API function to determine if it's a CD Rom.
Check out the following wrapper class:
#include <windows.h>
#include <vector>
#include <string>
#include <iostream>
using namespace std;
class DriveInfo
{
public:
DriveInfo()
{
char* buffer = 0;
size_t bsize = 0;
getDriveLetters(bsize,buffer);
parseDriveLetters(bsize,buffer);
delete [] buffer;
}
size_t NumDrives() const
{ return mDrives.size();}
std::string DriveName(size_t index) const
{ return mDrives[index]; }
char DriveLetter(size_t index) const
{ return mDrives[index][0]; }
DWORD DriveType(const std::string& driveName) const
{ return ::GetDriveType(driveName.c_str()); }
private:
void getDriveLetters(size_t& bufferSize, char*& driveStrings) const
{
bufferSize = MAX_PATH;
driveStrings = new char[bufferSize];
bool gotDriveNames = false;
while (!gotDriveNames)
{
DWORD pos = ::GetLogicalDriveStrings((DWORD)bufferSize, driveStrings);
if (pos > bufferSize)
{
bufferSize *= 2;
delete [] driveStrings;
}
else
{
gotDriveNames = true;
}
}
}
void parseDriveLetters(size_t size, const char* buffer)
{
std::string s(buffer, size);
std::string driveString;
for (size_t i=0; i<size; i++)
="" {
="" if="" (s[i]="=" 0)
="" (!drivestring.empty())="" mdrives.push_back(drivestring);
="" drivestring="" ;
="" }
="" else
="" +="s[i];
" }
="" std::vector<std::string=""> mDrives;
};
Example usage:
int main()
{
DriveInfo di;
for (size_t i=0; i
|
|
|
|
|
Or use something as simple as
<br />
for(int i=0;i<25;i++)
{<br />
if(RealDriveType(i,0) == DRIVE_CDROM)<br />
}<br />
|
|
|
|
|
Thanks for both your help.
I actually am using GetLogicalDriveStrings to get the available letters and then traverse through the letters using GetDriveType.
Thanks again....CP is the best!
|
|
|
|
|
NYTSX wrote: Thanks for both your help.
you can try warpper class for same here :-
http://www.codeproject.com/system/SysUtil.asp[^]
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
|
|
|
|
|
Hi,
I am curious about how c++ stores it's array size after a call to new x[].
does it store the array size, or an object count?
If it is an object count then deleting an object recasted from what it was originally newed could cause an heap error right?
eg, would the code...
class objectx
{
// lots of things that make this an object much larger than bool!
};
objectx* myObject = new (myObject)bool[20];
// do code
delete[] myObject;
...cause a heap corruption as delete trys to delete 20 x objectx's?
-- modified at 16:22 Friday 9th December, 2005
|
|
|
|
|
lastgen wrote: objectx* myObject = new (myObject)bool[20];
???
what are you trying to do there?
lastgen wrote: delete[] myObject
the '[]' means this is an array of objects, so call their destructor (if there is one) before deallocating the memory. but the memory you allocated holds only bools, not myObjects.
massive trouble awaits those who lie to the compiler about what lies on the other side of a pointer.
Cleek | Image Toolkits | Thumbnail maker
-- modified at 16:19 Friday 9th December, 2005
|
|
|
|
|
I'm debugging code that was written by someone else. I'm fairly new to c++ but have been coding in other high level languages for years. I'm getting heap corruption but it's not occurring in previous version, but it seems to occur after leaving the scope of a function I call from the old code. One of the big changes is that 'objectx ' is now 4 times larger than it had previously been, although it had always been much larger than bool. I guessing that perhaps the call to 'delete[] myobject' would create code saying 'deallocate heap at myobject with a size of 20 * sizeof(myobject)' instead of the intended 'deallocate heap at myobject with a size of 20 * sizeof(bool)' and that was always causing heap corruption.
This why the old version never completely worked, but it didn't cause many critical crashes. Now with the much larger object (an array of ~1000 in size of objects roughly 1k each!) its corrupting so much of the heap it killing the app. Is this likely to be correct?
-- modified at 16:44 Friday 9th December, 2005
|
|
|
|
|
If you wish to allocate an instance of MyObject, then just say "MyObject *p = new MyObject". All this other stuff with bool has a terrible number of problems starting with the object won't be properly constructed.
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
|
|
|
|
|
objectx* myObject = new objectx[20];
delete[] myObject;
|
|
|
|
|
The reason why they were allocating this way was the object is a union of static and dynamically sized data. Although not the best code this wasn't causing the issue. After following further I found a function that was being called was shifting the object but not updating the new pointer correctly.
|
|
|
|
|
We have a GUI application that hooks via TCP/IP to a child console process. I want to create a mini launcher console based app for these two applications.
I know the path to the child process. What I want to be able to do is obtain the child process executable and obtain some properties about...like what version it is. Then once I know this I can figure out what compatible version of the GUI to launch to hook the two together.
How can you open up an .exe file and get properties of it? Is there a way to get/set custom properties in the executable itself that you can compile in that another application can retrieve?
Kind of like the information shown in the "Summary" tab when you right-click and select 'Properties' from a .exe file. It lists Title,Subject,Category...Revision Number, etc. If I could figure out how to add to that list, and then obtain it programmatically, that would be perfect.
Thanks!
|
|
|
|