|
I found this in my code, and it works with no compiler or runtime error (so far). But I am thinking now it should have been delete[] a and not delete a . So why didnt it crash or complain?
char* a = new char [pDoc->m_KeysOptions.GetLength () + 1];
strcpy (a, pDoc->m_KeysOptions);
m_nToDisplay = pDoc->m_db.DLLSendKeywordsOptions(a);
delete a;
Thanks,
ns
Or should it really be delete a; after all?
|
|
|
|
|
|
It should be 'delete [] a'. Or, better yet, you should use CString or std::string.
Programs compiled with VC++ will not crash when you have an array of simple data types like char, double or int allocated with new and freed with delete without []. However, this is not guaranteed to work across all compilers/platforms.
The difference between 'delete x' and 'delete [] x' is that there are no destructor calls for elements of the array in the first version. Obviously, when you have char array, you don't need to call any d'tors. But with any non-trivial class you'd get into trouble.
Tomasz Sowinski -- http://www.shooltz.com
What is "scratch" and why can everything be made from it?
|
|
|
|
|
You're much better off using CString .
/ravi
Let's put "civil" back in "civilization"
http://www.ravib.com
ravib@ravib.com
|
|
|
|
|
ns wrote:
Or should it really be delete a; after all?
Indeed it should.
If you are interested in the details (what is the exact difference between delete[] and delete and why is the compiler not able to detect if you use the wrong version), you may take a look at this forum message.
--
Daniel Lohmann
http://www.losoft.de
(Hey, this page is worth looking! You can find some free and handy NT tools there )
|
|
|
|
|
to simplify the question, let me use an example:
i have a customer struct:
struct Cust
{
char name[20];
int ID;
};
i save huge custs to a file.
but the struct is a dynamic one, fields may be addd (i.e. int age) or deleted (i.e. delete field int ID) or changed (ie. char name from [20] to [50]).
'dynamic' means modify happens at run-time and very often. because there are lots data (impossible load all to memory), any change needs long time.
do u have idea to solve the problem for quick modify?
(i try stream but can't find useful tech)
thx
includeh10
|
|
|
|
|
Hello!
You could try and save the names or codes of the fields before you save the data inside them. Then, when you load the information from a file, you create a struct with the correct fields based upon the names/codes that you load for each struct.
For instance, you have one customer with this struct:
struct Customer
{
char name[20];
int age;
}
And another with this struct:
struct Customer
{
char name[50];
int ID;
int age;
}
When you save it to a file, your file could look like this:
Customer1
name:20;age
"customer name";27
Customer2
name:50;ID;age
"longer customer name";100T67;35
Hope that helps!
Sincerely,
Alexander Wiseman
Est melior esse quam videri
It is better to be than to seem
|
|
|
|
|
thx but modify or 'dynamic' is unknown in advance.
includeh10
|
|
|
|
|
Hello,
Oops! I reread your post and realized I somewhat misread the question.
Sorry about that. I'll try and figure out an answer to your question.
Sincerely,
Alexander Wiseman
Est melior esse quam videri
It is better to be than to seem
|
|
|
|
|
Fast and easy way: use XML
Concussus surgo.
When struck I rise.
|
|
|
|
|
No. You cannot use a struct in the way you are seeking. They are not dynamic. You could add a data array or vector to your struct to hold the variable data fields. You would need a second array to keep track of which position in the data array holds which field values.
If the fields vary from one record to another, this technique would require use of a sparse array so that each new field could have its own slot in the array. If a given record doesn't have data for a given field, just leave that array element empty.
If this doesn't answer the question, please be more specific about the file and how the fields get changed in it (timing relative to your apps operation is the important information).
|
|
|
|
|
Hello,
Please help me with CListCtrl arrow key navigation - I have my own listctrl class, but when I use the arrow keys to scroll up and down though it, the current item is not identified correctly (I use UINT whichItem=GetNextItem(-1,LVNI_SELECTED) ). It is updated one step behind, i.e. I'm on item 0, I press the down arrow, the whichItem is still 0, I press the down arrow again and then it becomes 1. Maybe I need to implement a keyboard handler in my derived class or something?
Thanks in advance,
Dennis
MCP, MCSD
|
|
|
|
|
Where are you trying to retrieve the current item? If it's from a state-changing notification, then the previous item may yet be selected.
---
Shog9
If I could sleep forever, I could forget about everything...
|
|
|
|
|
I'm trying to get this in the OnLvnKeydown by responding to this:
<br />
ON_NOTIFY(LVN_KEYDOWN, IDC_LIST, OnLvnKeydown)<br />
(and in my previous code I of course meant cList.GetNextItem(), instead of GetNextItem() - I'm not trying to get this from inside my derived class).
MCP, MCSD
|
|
|
|
|
Ok, that makes sense now. Unless you specifically need to pay mind to keyboard selection while excluding all other forms, it would be easier for you to use the notification LVN_ITEMCHANGED and watch for changes to the selection state:
void CMyWin::OnItemchangedListCtrl(NMHDR* pNMHDR, LRESULT* pResult)
{
NMLISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
*pResult = 0;
if ( pNMListView->iItem < 0 )
return;
if ( (pNMListView->uOldState & LVIS_SELECTED)
&& !(pNMListView->uNewState & LVIS_SELECTED) )
{
}
else if ( !(pNMListView->uOldState & LVIS_SELECTED)
&& (pNMListView->uNewState & LVIS_SELECTED) )
{
}
}
---
Shog9
If I could sleep forever, I could forget about everything...
|
|
|
|
|
Yeah, that code works, thanks. I was watching for the OnItemChanged in another list I had, but that was to process checkmarks, for some reason I didn't think about watching for selection as well. Could've saved me some time But I really don't understand why it doesn't process the OnKeyDown correctly.
MCP, MCSD
|
|
|
|
|
dennisV wrote:
But I really don't understand why it doesn't process the OnKeyDown correctly.
Most likely it sends the notification *before* it does it's own processing, thus the selection doesn't get changed until after your notification handler returns. Couldn't say why they did it that way, but you're usually better off not using it for selection changes anyway, as it'll ignore the mouse.
---
Shog9
If I could sleep forever, I could forget about everything...
|
|
|
|
|
Hello all,
I have a dialog app that sits in the tray and pops up a dialog to remind the user of something every hour. It obviously does no good if a screensaver is running. I did some searching around and found ways of interrupting it and found some good stuff from Bob Moore's site:
void CMCAlarmDlg::KillScreenSaver()
{
HWND hwnd;
if (hwnd = ::FindWindow ("WindowsScreenSaverClass", NULL))
{
::PostMessage(hwnd, WM_CLOSE, 0, 0);
}
else
{
if (hwnd = ::FindWindow ("Default Screen Saver", NULL))
{
::PostMessage(hwnd, WM_CLOSE, 0, 0);
}
}
}
This does work on Win2K, but not on NT 4. I've tried several other things (tried this one too: http://support.microsoft.com/default.aspx?scid=kb;en-us;Q140723)but haven't had any success. Several people have mentioned how unreliable FindWindow() can be.
My question is, is there another way to interrupt the screensaver by a keyboard or a mouse? Bob Moore mentions doing this himself with hooks, but doesn't elaborate. Hooks are a bit beyond my beginners skill level anyway. Or has anyone had good luck with something that works across the board (9x, NT, 2K, XP)
Ideas?
Thank you all (again) in advance.
Matt
|
|
|
|
|
Hello,
There is a nice little function which simulates a mouse event (by sending a mouse message to the active window) called 'mouse_event'. Sending a MOUSE_MOVE message to a screen saver would make it kill itself, because it automatically closes itself wheenever there is any mouse activity.
MSDN said that on Windows NT and 2000, 'mouse_event' has been superseded by 'SendInput', so here is the information for both functions (you might want to check the OS, so you can use the correct function):
'mouse_event':
VOID mouse_event(
DWORD dwFlags,
DWORD dx,
DWORD dy,
DWORD dwData,
ULONG_PTR dwExtraInfo
}
dwFlags: look up this function on MSDN to see all the options; the one you want is MOUSEEVENTF_MOVE, to simulate a mouse movement. MOUSEEVENTF_ABSOLUTE can be combined with this flag (e.g. MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_MOVE) to let you specify absolute coordinates in 'dx' and 'dy'.
dx: if MOUSEEVENTF_ABSOLUTE is set in the firest parameter, this specifies the absolute position along the 'x' axis of the mouse. Othwerwise, this specifies how many points you want to move along the 'x' axis.
dy: same as 'dx' only for the 'y' axis
dwData: this specifies mouse wheel movement; you don't need to worry about this, because you won't be using the MOUSEEVENTF_WHEEL option. Just set it to 0 or NULL.
dwExtraInfo: used to send a value with the message. Set this to 0 or NULL for your purposes.
'SetInput':
UINT SetInput(
UINT nInputs,
LPINPUT pInputs,
int cbSize
}
nInputs: number of input events (mouse events) that you want to send
pInputs: a pointer to an array of INPUT structures. An input structure looks like this:
typedef struct tagINPUT {
DWORD type;
union {
MOUSEINPUT mi;
KEYBDINPUT ki;
HARDWAREINPUT hi;
};
} INPUT, *PINPUT;
For your purposes, you want to use the 'mi' variable, so you would set 'type' equal to INPUT_MOUSE. The MOUSEINPUT structure contains almost identical variables to the 'mouse_event' function. To send a movement command, you would set the 'dwFlags' variable of the structure to: MOUSEEVENTF_MOVE or MOUSEEVENTF_ABSOLUTE, and the 'dx' and 'dy' variables to the coordinates of your choice. Every other variable can be filled with 0 or NULL.
cbSize: size of the structure. Set this equal to sizeof(INPUT) , which gives you the size, in bytes, of an INPUT structure.
Here are sample calls to each function:
'mouse_event' sample call:
mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_MOVE, 0, 0, NULL, NULL);
'SetInput' sample call:
INPUT myInput;
myInput.type = INPUT_MOUSE;
MOUSEINPUT mouseInput;
mouseInput.dwFlags = MOUSEEVENTF_MOVE|MOUSEEVENTF_ABSOLUTE;
mouseInput.dx = 0;
mouseInput.dy = 0;
mouseInput.mouseData = NULL;
mouseInput.time = 0;
mouseInput.dwExtraInfo = NULL;
myInput.mi = mouseInput;
SetInput(1, &myInput, sizeof(INPUT));
SetInput is a pain to call, and since you are only moving the mouse, you could get away with calling mouse_event across all platforms.
Hope that helps you out!
Sincerely,
Alexander Wiseman
Est melior esse quam videri
It is better to be than to seem
|
|
|
|
|
Excellent! This looks exactly like what I need. I'll try this out and let you know.
Thanks Alexander for your time.
Matt
|
|
|
|
|
Great! Let me know if it works out for you!
Sincerely,
Alexander Wiseman
Est melior esse quam videri
It is better to be than to seem
|
|
|
|
|
Hello Alexander,
Well after implementing your idea, I've come to find out that it works great on Win2k, but not on NT. I didn't have the opportunity to test it on XP, but I've since found a newsgroup posting that suggests that it does (his code and sugguestions were very similar).
I ended up using this: http://www.mooremvp.freeserve.co.uk/Win32/framed_tip022.htm; and modified it a bit 'til it started working. Like I said, yours worked great for 2k (and probably XP). I've stored your reply for future use if needed. You might test it on NT and see what your results are. If you find something, let me know since it's alot easier than doing it like the link above shows.
Can't thank you enough for your help. Your instructions were comprehensive and easy to follow.
Thanks again,
Matt
|
|
|
|
|
Hi,
I'm writing a ISAPI program for a company project. The project requires that I don't use a database to save the user input. Instead I want to write the user data into a disk file on the server using CFile, is this possible?? I tried using CFile and writing it into a directory on the server that has write permissions, however all I get is a Exception in filecore.h.
Thanks for your help!!
|
|
|
|
|
Akula wrote:
Instead I want to write the user data into a disk file on the server using CFile, is this possible??
Yes.
Jason Henderson quasi-homepage articles "Like it or not, I'm right!"
|
|
|
|
|
Here is the code I used
CFile myFile;
myFile.Open("Data//MyFile.txt",CFile::modeCreate || CFile::modeWrite);
myFile.Write((void*)pCtxt,sizeof(*pCtxt));
myFile.Close();
I also tried that like this
CFile myFile;
myFile.Open("MyFile.txt",CFile::modeCreate || CFile::modeWrite);
myFile.Write((void*)pCtxt,sizeof(*pCtxt));
myFile.Close();
|
|
|
|
|