|
I've searched quite a bit on this forum for the ways to terminate a process, in most case people obtain a HWND by calling ::FindWindow then get the process ID through the HWND, and kill the process.
Now my problem is that my program does not have a window, and suppose I do not know its file name either at this point, I do, however, have its HINSTANCE which was returned by the original ::ShellExecute call(that's how I launched this process). If in some ugly case this application gone frozen, is there a good way to terminate it?
|
|
|
|
|
Bin wrote:
I do, however, have its HINSTANCE
Are you sure? MSDN describes that HINSTANCE as nothing but a return value to be casted for an integer.
Unless you're using a verb, you may try CreateProcess instead of ShellExecute .
rechi
|
|
|
|
|
When I try to compile this piece of code
#include <cstddef>
#include <vector>
class klasa
{
char data[100];
public:
void* operator new (size_t)
{
return ::new klasa;
}
void operator delete (void* pt)
{
if (pt == 0) return;
::delete(pt);
}
};
void main()
{
std::vector <klasa> kontejner;
klasa k;
kontejner.push_back(k);
}
I got compiler error:
d:\program files\microsoft visual studio\vc98\include\xmemory(34) : error C2660: 'new' : function does not take 2 parameters
d:\program files\microsoft visual studio\vc98\include\xmemory(66) : see reference to function template instantiation 'void __cdecl std::_Construct(class klasa *,const class klasa &)' being compiled
WTF is going on here?
|
|
|
|
|
My *guess* would be that there is an operator new that takes two parameters and that it is involved in the creation of a vector.
BTW, you don't need to do this:
Nemanja Trifunovic wrote:
if (pt == 0) return;
delete (0) is guarenteed to be safe.
Christian
Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002
During last 10 years, with invention of VB and similar programming environments, every ill-educated moron became able to develop software. - Alex E. - 12-Sept-2002
|
|
|
|
|
Christian Graus wrote:
My *guess* would be that there is an operator new that takes two parameters and that it is involved in the creation of a vector.
True. Operator new with two arguments is required in an allocator function. The question still is what can I do about it? What to do with this second parameter?
Christian Graus wrote:
delete (0) is guarenteed to be safe.
Yeah, I know. My original new and delete are more complicated: not just wrappers for global ones.
|
|
|
|
|
I would think your object needs to impliment operator new taking the same two parameters then.
Christian
No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002
During last 10 years, with invention of VB and similar programming environments, every ill-educated moron became able to develop software. - Alex E. - 12-Sept-2002
|
|
|
|
|
Christian Graus wrote:
I would think your object needs to impliment operator new taking the same two parameters then.
Any idea what to do with this "extra" parameter (which is a pointer to the class type)?
|
|
|
|
|
Nope - I've never done this stuff. If it were me, I'd write the function that takes the pointer, then I'd run it and check it like crazy for any sort of leak or problem. Or I'd look in the STL source for a similar operator to see what it gets used for.
Christian
No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002
During last 10 years, with invention of VB and similar programming environments, every ill-educated moron became able to develop software. - Alex E. - 12-Sept-2002
|
|
|
|
|
Well, I just looked at "The Book" (St Bjarne Stroustrup), and found out that this new with two arguments is so-called "placement operator" - it usually just returns the pointer that comes as second argument. Strange...
|
|
|
|
|
Unfortunately, and for reasons that I do not understand, we do not have that book at work, hence my inability to check on such details. Sounds like a winner though.
Christian
No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002
During last 10 years, with invention of VB and similar programming environments, every ill-educated moron became able to develop software. - Alex E. - 12-Sept-2002
|
|
|
|
|
Uff, there's a couple of misunderstandings here.
void* operator new (size_t)
{
return ::new klasa;
} This is the first problem in your program. An overloaded new operator is meant to be taking care of memory allocation only: the call to the class contructor is generated automatically by the compiler. Please note that the new you provide is calling implicitly the ctor through the use of golbal ::new (altough in this case the error will go unnoticed cause kasa has a trivial ctor). Instead, write something like:
void* operator new (size_t size)
{
return ::new char[size];
} or
void* operator new (size_t size)
{
return malloc(size);
} Similarly, your overloaded delete operator should be only concerned with deallocating memory without calling (implicitly) klasa::~klasa .
Now for the second problem. Once you provide an overload for new , the compiler won't use (for the relevant class) any of the predefined global new oeprators available. In particular, it won't use the so-called placement new , whose signature is:
void* operator new (size_t size,void* pos); Placement new does not really do anything: the caller already provides the space (in pos ) where the object is to be constructed. An example of an explicit call to placement new :
void *pos;
whatever *p=new(pos)whatever; Now, placement new is extensively used by allocators in STL, and this is what the compiler is complaining about: since you overloaded new you also have to provide an overload for the placement variant:
class klasa{
...
void* operator new (size_t size,void* pos)
{
return pos;
}
}; Hope I made myself clear enough
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Hello, Joaquín. Thanks for your answer.
Joaquín M López Muñoz wrote:
This is the first problem in your program. An overloaded new operator is meant to be taking care of memory allocation only: the call to the class contructor is generated automatically by the compiler. Please note that the new you provide is calling implicitly the ctor through the use of golbal ::new (altough in this case the error will go unnoticed cause kasa has a trivial ctor).
As I already wrote to Christian, my original new and delete look quite different. I replaced my code with calls to global new and delete, because I wanted to make my examlple short and clear. I am aware of the issues you are talking about.
Joaquín M López Muñoz wrote:
In particular, it won't use the so-called placement new, whose signature is:void* operator new (size_t size,void* pos);
Placement new does not really do anything: the caller already provides the space (in pos) where the object is to be constructed.
Exactly. And that's what confuses me a lot. If you take a look at the implementation of placement new and delete in <new> you will see
inline void *__cdecl operator new(size_t, void *_P)
{return (_P); }
#if _MSC_VER >= 1200
inline void __cdecl operator delete(void *, void *)
{return; }
What is this good for?
Joaquín M López Muñoz wrote:
Now, placement new is extensively used by allocators in STL, and this is what the compiler is complaining about: since you overloaded new you also have to provide an overload for the placement variant
You are 100% right. The default allocator uses placement new in function construct, and when I put this code in my class
void* operator new (size_t size,void* pos) { return pos; }
void operator delete (void*, void*) {}
everything seems to work fine.
However, I still don't understand why. What is such implementation of placement new and delete good for? I know that in some cases you can provide i.e. your custom memory pool as a second argument, but here it is not the case. Why do they use this placement new in default allocator?
|
|
|
|
|
|
Given a machine with multiple network cards, how do you tell Windows which card to use when working with Sockets?
|
|
|
|
|
hello!
i have a dialogbased mfc program.
in this programm i have several strings.
CString str='Text';
now i want the string read into a access database. the table inside the database is empty, i have only 2 columns (one column for the number of the string (automatically) and the second one for the string).
i use ODBC and tried this here:
CMyDataBase db;
db.Open();
db.m_zweiter=strText;
db.Update();
db.Close();
WHAT`S WRONG?
can somebody help me?
thanks
MFC
|
|
|
|
|
Well, without seeing what's being your Open, Update, and Close functions, it's hard to say. What's your SQL statement look like, or are you trying to populate a dataset? Besides, you declare CString str and then have a ...=strText. Was that just a typo in the example? Where is the program failing? Connection strings can be complicated to set up--maybe it's the connection string you're using. Also, have you set up an ODBC data source under "Data Sources (ODBC)" usually in the control panel or adminstrative tools program groups.
Marc
Help! I'm an AI running around in someone's f*cked up universe simulator.
|
|
|
|
|
hi!
yes, it was a mistake. both times it must be CString strText.
i tried several things, sometimes the mistake was in that row:
db.m_zweiter=strText;
or everything was fine, but nothing changed in the database.
|
|
|
|
|
|
You need to do this:
1. Create a database object and open it.
2. Create a recordset object and open it. You send the SQL statement to the Recordset.
3. See CRecordset member functions for details how to insert, update and delete rows
4. You close the recordset and then the database.
Best regards,
Alexandru Savescu
|
|
|
|
|
Does anyone know of an easy way to change the text and background colors on one edit control in a dialog box?
|
|
|
|
|
|
Handle the WM_CTLCOLOR message. There are plenty of posts here explaining the use of that message and it is in MSDN. You can check the ID of your edit control and if it is the right one, you can then set the background colour and text colour using the device context...
hope this helps
Adam.
www.beachwizard.com/travelogue[^]
"I spent a lot of my money on booze, birds and fast cars. The rest I just squandered"
George Best.
|
|
|
|
|
I get and assertion when I run this code, is the reason it fails because the m_cTabMain control isn't created when the first OnSize message comes through?
void CToolTabsDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
int ncx = cx - ::GetSystemMetrics (SM_CXBORDER);
int ncy = cy - ::GetSystemMetrics (SM_CYBORDER);
m_cTabMain.SetWindowPos (NULL, 0, 0, ncx, ncy, SWP_NOMOVE | SWP_NOZORDER);
} I put similar code in the OnPaint handler and it works perfectly. Is there anyway I can get the code to run in the OnSize handler? m_cTabMain is a CTabCtrl that I put on the dialog in the resource editor.
Thanks in advance.
- monrobot13
|
|
|
|
|
you can test to see if m_cTabMain is a window (::IsWindow(m_cTabMain.m_hWnd)) - don't call SWP if it's not a window.
-c
"[it was..] one of those evenings when you feel that not only will there definitely be a revolution, but that the Association of Manufacturers will foot the bill."
-- Umberto Eco, Foucault's Pendulum
|
|
|
|
|
the reason it fails because the m_cTabMain control isn't created when the first OnSize message comes through?
Probably. SOmetimes this trick works: insert an UpdateData(FALSE) right at the beginning of CToolTabsDlg::OninitDialog , after the call to the parent's OninitDialog .
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|