|
Here are a couple of problems with this code
1) GetBuffer() memory has to be released with ReleaseBuffer
2) fwrite( strTitleCurrentDate, sizeof(TCHAR), strTitle.GetLength()/** sizeof( TCHAR )*/, fp );
How do you decide to write strTitleCurrentDate to the length of strTitle, they are not related ... Correction strTitleCurrentDate.GetLength()
3) Here is even more ambiguous code
CString strTitle1;
newBuffer=strTitle1.GetBuffer(strTitle1.GetLength()* sizeof( TCHAR ));
What is the size of allocation ? strTitle1 is a zero length string! Zero and you decide to read
fread( newBuffer, sizeof(char), 100/** sizeof( TCHAR )*/, fp );
100 bytes to this buffer.
Shouldn't this be
fread( newBuffer, sizeof(TCHAR), strTitle1.GetLength(), fp );
if your allocation was right ?
However it should be just
TCHAR myFileBuffer[1024];
fread( myFileBuffer, sizeof(TCHAR), 1024, fp );
why use GetBuffer ?
Remember when dealing with a char pointer, when you cross what you have allocated, you are writing in to other non allocated memory. You have to always allocate for the string terminator. Any such memory overwrite results in crash, usually later in the code.
How to use google
type "how to read a text file line by line" and see the google results, open the second link and it has the following piece of code for a similar task. Please modify this code to get your stuff done. So another advice, try to use google much more with different search strings representing the same question.
#include <iostream>
#include <fstream>
#include <strstream>
using namespace std;
int main()
{
char buffer1[2048];
char buffer2[2048];
istrstream ostr1(buffer1, 2048);
istrstream ostr2(buffer2, 2048);
int values1[100];
int values2[100];
int c=0;
ifstream fin("data.txt");
fin.getline(buffer1, 2048);
fin.getline(buffer2, 2048);
while (ostr1 >> values1[c])
{
ostr2 >> values2[c++];
}
for (int i=0;i<c;i++)
{
="" cout="" <<="" values1[i]="" ":"="" values2[i]="" endl;
="" }
good="" luck,
santhosh
=""
<div="" class="ForumSig">Live and let live
|
|
|
|
|
can any please suggest which tool is available for preparing the documentation for atl component.
|
|
|
|
|
I've not used it but C++ devs recommend this: Doxygen[^]
Kevin
|
|
|
|
|
I want to implement address book like how microsoft outlook does with the following interface.
IContacts //maintains all the IContact interfaces
IContact //store the details of the contact.
I can get IContact by calling the IContacts->Item(i) method.can any one give me some ref to do this type of implementation
Thank and Regards.
|
|
|
|
|
Hi,
I'm planning to implement a chat server + a chat client as a learning exercise for my ATL/COM/DCOM.
As part of that first I would implement a chat server which is a local server and a chat client. Please suggest if the following is possible, if true how.
Clients register their callbacks by using Advice() (Of course, I would do FindConnectionPoint(), before that ). My doubt is is there any way to send the notification from the server to all the clients? and also can the server select the client to which it has to send the notification.
For Example: User1, User2 are currently logged in. If user1 sends a message to User2 by using SendMsg(from, to, msg) of IChatServer, can the server alert client of User2.
Otherwise can server broadcast the event??
Thanks and regards,
Raja Pratap
|
|
|
|
|
Raj Prathap wrote: My doubt is is there any way to send the notification from the server to all the clients? and also can the server select the client to which it has to send the notification.
It's not clear to me what you mean by "chat server" and "notification".
When someone talks about a chat server to me I interpret it as the MSDN sample CHATTER/CHATSRVR[^], but the notification you're talking about feels like a callback on a source interface from a COM server to its client (AKA a COM event).
If the notification is a COM event, the answer is that the server keeps track of its clients in a container; a list, vector or array. When an event is generated the container is walked through notifying all registered clients.
If you want to skip one of the clients you have to write your own code to do that.
A small thought:
You don't have to use the connection point concept, in fact Don Box recommends not to use it in his book Effective COM[^].
You can create your own Advise() method and put whatever functionality in it that you want and/or need.
But if you're new to this it could be a good starting point using the connection point concept to understand how it works.
"It's supposed to be hard, otherwise anybody could do it!" - selfquote "High speed never compensates for wrong direction!" - unknown
|
|
|
|
|
|
I think Nenad re-released it in order to include a sample showing how to use the new TabView - might want to check you've got the most up to date version.
|
|
|
|
|
I was wondering where I could find the Docs on learning WTL, I know my way around MFC and ATL so I am sure I can leverage what I've learned there.
TIA
Yours Truly, The One and Only!
|
|
|
|
|
Hello
I have tab Control on OCX and in the one Tab i have inserted one MFC Dialog which contains List Control.
I want to Set Tool Tip on that List Control.
I have Tried following Events :
OnToolHitTest &
OnToolTipText
these work fine in simple MFC but
OnToolHitTest event does not get called in my application.
Please Help
Rajput
|
|
|
|
|
The MainFrame of my WTL application contains a splitter window.
The left pane of this window is derived from CCheckListViewCtrl,
whereas the right one is derived from CRichEditCtrl.
Items in ListView (or, rather, text strings of items)
represent headers of a collection of texts.
Initially Item[0] is selected in the left pane, and the corresponding
text Texts[0] is printed in the right pane.
Now I wish every time when (multi) selection
in the left pane is changed, the left pane to (re)print
all texts Texts[i] with selected indices i.
My derived ListView class contains
a member stl::vector<int> selectedIndeces,
containing indices of all selected items.
This vector can be updated in time,
if one adds appropriate message handlers
(for ListView)
for KEY_UP/KEY_DOWN keystrokes, as well as
for all kinds of mouse clicks (left, right, double).
My first question is: do there exists
the notification message (similar to LBN_SELCHANGE
for ListBoxes) produced by CCheckListViewCtrl
any time the selection is changed, in no matter which
way -- via keystroke or mouse click?
If yes, how one can catch this message in parent
CMainFrame? I.e., what kind of message handler
is to be added to CMainFrame's message map?
This is necessary, because only CMainFrame
has access to the whole business logic of the application,
in particular to the array of texts Texts[].
|
|
|
|
|
Vlad0 wrote: My first question is: do there exists
the notification message (similar to LBN_SELCHANGE
for ListBoxes) produced by CCheckListViewCtrl
any time the selection is changed, in no matter which
way -- via keystroke or mouse click?
I think LVN_ITEMCHANGED is what you want. It is sent whenever an item changes someway - that includes when the item state changes - and the item state will change when the item is selected or de-selected.
Vlad0 wrote: If yes, how one can catch this message in parent
CMainFrame? I.e., what kind of message handler
is to be added to CMainFrame's message map?
It'll be related to NOTIFY_HANDLER(_EX) - I'm afraid I don't have access to a machine with WTL on at the moment (I use OS X at home), so I can't tell you outright. I suspect Michael Dunn's articles on WTL (here's the one about splitters[^]) will tell you all you need to know - I've always found them very useful!
HTH!
|
|
|
|
|
Stuart Dootson wrote: I think LVN_ITEMCHANGED is what you want. It is sent whenever an item changes someway - that includes when the item state changes - and the item state will change when the item is selected or de-selected
It'll be related to NOTIFY_HANDLER(_EX) - I'm afraid I don't have access to a machine with WTL on at the moment (I use OS X at home), so I can't tell you outright.
I've installed an excellent VS add-on: WTL Helper
by Sergey Solozhentsev http://www.codeproject.com/macro/wtlhelper.asp[^]
It inserted an adequate message handler for LVN_ITEMCHANGED
into Message Map of CMainFrame:
NOTIFY_CODE_HANDLER_EX(LVN_ITEMCHANGED, OnLvnItemChanged)
and created the prototipe of the handler:
LRESULT CMainFrame::OnLvnItemChanged(LPNMHDR pnmh)
{
LPNMLISTVIEW pnmv = (LPNMLISTVIEW)pnmh;
//If the iItem member of the structure pointed to by pnmv is -1,
//the change has been applied to all items in the list view.
return 0;
}
for this handler.
To test the handler I added first the code ignoring the parameter pnmv:
m_wndView2.AppendText(L"Item changed!\n");
Here m_wndView2 is the right pane, i.e. rich edit control.
This code produced one strange phenomenon:
if one clicks with the mouse on some non selected item
(when only one other item is selected) the line "Item changed!"
appears THREE times instead of 2. I do not know why.
If one changes this code, taking into account the parameter pnmv:
if((pnmv->uNewState&LVNI_SELECTED)!=(pnmv->uOldState&LVNI_SELECTED))
m_wndView2.AppendText(L"Item changed!\n");
then the line "Item changed!" is printed 2 times, as expected.
But there is one problem with this message handler.
If one adds at once 100 items to selected one (via SHIFT+Left mouse click),
then OnLvnItemChanged() executes 100 times: every newly selected item
causes the execution. I will not permit to redraw rich edit pane 100 times:
texts corresponding to items of ListView can be quite large.
So I would like to have 1 single notification instead of 100.
If I could determine somehow which of these 100 notifications is the last
one in a series, I would react only on this last message. But I see no way
to catch the last message in a series. OK, there is one clumsy way:
to add a timer to CMainFrame, showing how much time is passed
after the last LVN_ITEMCHANGED message.
If this time becomes more than, say, 2ms, then
CMainFrame should update rich edit pane...
But this way is both too clumsy and too ugly.
Do there exists a more elegant way?
Stuart Dootson wrote: I suspect Michael Dunn's articles on WTL (here's the one about splitters[^]) will tell you all you need to know - I've always found them very useful!
Yes, I became acquainted with WTL reading the first articles of the series:
"WTL for MFC programmers". They really turned out to be invaluable
source of info. In spite of the fact that the last time I used MFC
was about 5--6 years ago, so I remember almost nothing about MFC
except magic words "Doc/View paradigm"
And I've used some code from later articles of the series.
In particular ListView in my program is borrowed from article VI.
I believe this will not be the last borrowing
|
|
|
|
|
Vlad0 wrote: OK, there is one clumsy way:
to add a timer to CMainFrame, showing how much time is passed
after the last LVN_ITEMCHANGED message.
If this time becomes more than, say, 2ms, then
CMainFrame should update rich edit pane...
But this way is both too clumsy and too ugly.
Do there exists a more elegant way?
That's the way I was going to suggest, until I read that you'd already thought of it
The other way might be to use PeekMessage to see if there are any pending notification messages for the frame? Alternatively - looking at the documentation:
If a list-view control has the LVS_OWNERDATA style, and the user selects a range of items by holding down the SHIFT key and clicking the mouse, LVN_ITEMCHANGED notifications are not sent for each selected or deselected item. Instead, you will receive a single LVN_ODSTATECHANGED notification, indicating that a range of items has changed state.
you could make your list-view owner data?
|
|
|
|
|
Thank You for this suggestion! WTL helper shows
this message as legal for ListView, but I had no idea,
what "ODSTAT" means, so did not made any attempts to experiment
with this.
I've just added "| LVS_OWNERDATA" to view list style.
Compiler had no objections Now I will make experiments
with LVN_ODSTATECHANGED messages instead of LVN_ITEMCHANGED.
Thank You once again!
|
|
|
|
|
You'll need to respond to LVN_GETDISPINFO[^] notifications with LVS_OWNERDATA set - but that's not too much of a hardship - it also helps if you have *a lot* of items in the list.
|
|
|
|
|
Alas WTL List View control fails to send any LVN_ODSTATECHANGED notification as well as any LVN_GETDISPINFO. I suspect
that the style flag LVS_OWNERDATA is simply ignored by
this control.
All this is very strange. One expects
that this control shold produce an "integral"
"Selection changed" event instead of
(or in addition to) "atomic" ITEMCHANGED.
As is the case with ListBox control.
The method using something like PostMessage/PeekMessage
has the same drawback, as using the series
of LBN_ITEMCHANGED with no redirection -- one is to find
a way to determine, which of the posted/picked messages is
the last one in a series.
Of course, one can write a procedure,
making an incremental update of RichEdit control
after any picked ITEMCHANGED message
(using CRichEdit.Cut()|Copy()|Paste()|SetSel()).
Incremental update may turn out to be more effective solution
than deleting all rich edit text and inserting the new one
after any "global selection changed" event. Suppose,
e.g., that one adds item 100 to 100 already
selected items -- from 0 to 99. Then incremental update
just adds Texts[100] to the end of the previous text.
Nevertheless, even while incremental update it is
more preferable to get one "global" selection change
notification instead of 100 "atomic" ones.
To guarantee that none of this 100 atomic notifications
will drop out of the message queue just because
the process of rich edit control update (even incremental one)
is much slower than the sending of a series
of notification messages.
|
|
|
|
|
Vlad0 wrote: I suspect
that the style flag LVS_OWNERDATA is simply ignored by
this control.
No - it's simply a thin wrapper over the ListView Windows control - it *does* understand LVS_OWNERDATA.
One thing...you have to set LVS_OWNERDATA when the control is created - it will ignore it if you try to set it later.
|
|
|
|
|
My List View class is constructed according to
Michael Dunn ritual as follows:
typedef CCheckListViewCtrlImplTraits
<0,0,LVS_EX_CHECKBOXES|LVS_EX_GRIDLINES|LVS_EX_UNDERLINEHOT|
LVS_EX_ONECLICKACTIVATE|LVS_EX_FLATSB|LVS_OWNERDATA> CMyCheckListTraits;
class CMyListView:
public CCheckListViewCtrlImpl<CMyListView,CListViewCtrl,CMyCheckListTraits>
{
.......
};
so the style LVS_OWNERDATA is somehow inhereted.
Adding of the line
SetExtendedListViewStyle(GetExtendedListViewStyle()|LVS_OWNERDATA);
to the List View's OnCreate()
gives no results as well.
Replacing elaborate Michael Dunn's class with plain and simple
class CMyListView: public CWindowImpl<CMyListView,CListViewCtrl>
produced by WTL Wizard gives no effect as well.
Missing "EX" in the name LVS_OWNERDATA makes it plausible,
that CListView has another kind of style, different from "extended" one.
Looking through atlctrls.h I've seen no counterparts of
Get|SetExtendedListViewStyle() without "Extended".
But there is a function Create() having both kinds of style as parameters.
But adding
ATL::CWindow parent=GetParent();
Create(parent.m_hWnd,0,0,LVS_OWNERDATA,
LVS_EX_CHECKBOXES|LVS_EX_GRIDLINES|LVS_EX_UNDERLINEHOT|
LVS_EX_ONECLICKACTIVATE|LVS_EX_FLATSB);
to OnCreate() compiles OK, but leads to the crash of the prog at sturtup.
And the debugger says that ListView has no parent: parent.m_hWnd==0.
Replacing of GetParent() with GetTopLevelWindow() produces the same result.
So, I do not know at present, how one can correctly set this style at all.
|
|
|
|
|
Try using this as the traits style
typedef CCheckListViewCtrlImplTraits<WS_CHILD | WS_VISIBLE | LVS_REPORT | LVS_SHOWSELALWAYS|LVS_OWNERDATA, WS_EX_CLIENTEDGE, LVS_EX_CHECKBOXES | LVS_EX_FULLROWSELECT> CCheckListViewCtrlTraits;
This applies LVS_OWNERDATA as a basic window style.
|
|
|
|
|
Yes, I guessed, looking at the code in atlctrlx.h,
that LVS_OWNERDATA belongs to the same kind of style
as all WS_XXX styles, so placed it in the first template
parameter. But this produced no effect -- no notifications
LVN_ODSTATECHANGED or LVN_SETDISPINFO were triggered by the app.
In fact the class CCheckListViewCtrlImplTraits<...>
has no member fields, it only defines three member functions
GetWndStyle(DWORD dwStyle), GetWndExStyle(DWORD dwExStyle) and
GetExtendedLVStyle(). The first 2 of them IGNORE first 2 template
parameters if being called with non-zero style argument.
So it seems that if these 2 functions were called at all,
say, by CListView constructor, they were called with non-zero
argument (default style for List View), so these 2 template parameters were ignored and CListView was constructed with some set of default style arguments, not including LVS_OWNERDATA.
I tried to modify the style "manually",
adding the line
ModifyStyle(0,GetWndStyle(0),0);
to OnCreate(), but this again produced no effect.
-- modified at 11:18 Wednesday 13th June, 2007
|
|
|
|
|
OK - I knocked up a quick sample using splitters and list views.
1. Creating the list view (and a rich edit control) as children of a splitter - this is part of CMainFrame::OnCreate :
splitter_.Create(*this, rcDefault, 0, 0, WS_EX_CLIENTEDGE);
list_.Create(splitter_, rcDefault, 0, CControlWinTraits::GetWndStyle(0)|LVS_OWNERDATA|LVS_REPORT, CControlWinTraits::GetWndExStyle(0), 1234, 0);
edit_.Create(splitter_, rcDefault, 0,
WS_CHILD|WS_VISIBLE|WS_HSCROLL|WS_VSCROLL|ES_READONLY|ES_AUTOHSCROLL|ES_AUTOVSCROLL|ES_MULTILINE);
edit_.SetFont(AtlGetStockFont(ANSI_FIXED_FONT));
m_hWndClient = splitter_;
splitter_.SetSplitterPanes(list_, edit_);
list_.AddColumn(_T("Test"), 0);
list_.SetItemCount(20);
UpdateLayout();
splitter_.SetSplitterPos(200);
The controls are declared as members of CMainFrame with these types:
CSplitterWindow splitter_;
CListViewCtrl list_;
CRichEditCtrl edit_;
Note that LVS_OWNERDATA is part of the window style (i.e. ORed with styles like WS_VISIBLE etc). SetItemCount tells the list view what items to ask for.
2. I handle notifications like this:
MainFrm.h: (this is within the CMainFrame definition)
BEGIN_MSG_MAP(CMainFrame)
MESSAGE_HANDLER(WM_CREATE, OnCreate)
MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
COMMAND_ID_HANDLER(ID_APP_EXIT, OnFileExit)
COMMAND_ID_HANDLER(ID_FILE_NEW, OnFileNew)
COMMAND_ID_HANDLER(ID_APP_ABOUT, OnAppAbout)
NOTIFY_HANDLER(1234, LVN_GETDISPINFO, OnGetDispInfo)
NOTIFY_HANDLER(1234, LVN_ODSTATECHANGED, OnODStateChanged)
CHAIN_MSG_MAP(CUpdateUI<CMainFrame>)
CHAIN_MSG_MAP(CFrameWindowImpl<CMainFrame>)
END_MSG_MAP()
LRESULT OnGetDispInfo(WORD , LPNMHDR nmHeader, BOOL&);
LRESULT OnODStateChanged(WORD , LPNMHDR nmHeader, BOOL&);
MainFrm.cpp:
LRESULT CMainFrame::OnGetDispInfo(WORD , LPNMHDR nmHeader, BOOL&)
{
NMLVDISPINFO* getDispInfo = reinterpret_cast<NMLVDISPINFO*>(nmHeader);
getDispInfo->item.state = LVIF_TEXT;
CString s;
s.Format(_T("%d"), getDispInfo->item.iItem);
lstrcpy(getDispInfo->item.pszText, s);
return 0;
}
LRESULT CMainFrame::OnODStateChanged(WORD , LPNMHDR nmHeader, BOOL&)
{
OutputDebugString(_T("CMainFrame::OnODStateChanged"));
return 0;
}
LVN_ODSTATECHANGED is fired by the list view when you do a Shift-click to select multiple items. LVN_ITEMCHANGED seems to be fired when single items are selected.
Anyway - hope this helps.
|
|
|
|
|
1. The immediate child windows of the splitter in my app
are 2 CPaneContainers (their function is just to show
text labels). ListView and RichEdit controls are childs
of these Pane containers. I think this is not essential
difference with Your sample. Except the eventual cases
when I will need the "real" parent of List View -- CMainFrame
and not CPaneContainer -- I should call GetParent() twice.
2. And what is very essential difference, surely responsible
for the bug in my app is that I choose wrong format
for notification handlers. I mimicked the definition
of the message handler for LVN_ITEMCHANGED:
NOTIFY_CODE_HANDLER_EX(LVN_ITEMCHANGED, OnLvnItemChanged)
Now I see from Your example that the correct format
for LVN_ODSTATECHANGED and LVN_GETDISPINFO
must use the macro NOTIFY_HANDLER(...) instead!
OK, now I'll rewrite my code in accord with Your sample code.
Hope this really would help.
Thank You very much in advance!
...Well I've modified my code. Really, my app now triggers
messages if shift+Mouse is clicked. But:
1) After several such clicks app crashes due to variety
of reasons. One of them is out of memory error.
These bugs probably can be fixed,
but this may take too much time;
2) Check boxes do not appear. If one or's the item state flag
with LVIF_IMAGE an empty place appears before messages instead of
checkboxes.
3) List view items info, set by ListView.InsertItem(i,text)
is ignored:
if one comments the line:
lstrcpy(getDispInfo->item.pszText, s);
in OnGetDispInfo() the texts of all items becomes empty.
This is not fatal, because I can set item texts
in OnGetDispInfo() calling InsertItem with empty text=L"";
4) Most important of all: there are no notifications
in case of ctrl+click as well as in case of simple clicks.
Using of LVN_ITEMCHANGED together with LVN_ODSTATECHANGED
does not resolve the problem of multiple LVN_ITEMCHANGED
events (see posts above), creating an additional mess.
By the way, even a single mouse click (with no shift or ctrl)
can clearly change the state of many items at once
creating multiple LVN_ITEMCHANGED events.
-- modified at 13:17 Wednesday 13th June, 2007
|
|
|
|
|
I have a TCPIP server using the fonction recv(). The header contain the sise of message. Then I read the entire message.
I would like have a dynamic buffer char* because the server get many different size of data from 10 until 30000 char, but 80% from message are shorter as 100 char.
I have test the following code but it's do not works, I do not understand why.
'msglenght'
char* msgBuffer;
msgBuffer=(char*)malloc( msglenght );
memset(msgBuffer,0,msglenght);
rVal = recv(m_acceptSocket,msgBuffer,msglenght,0);
when I show the contain of msgBuffer it is empty.
Thank you for help
AutreChien
|
|
|
|
|
That should work - I use the exact same call in a program using sockets I wrote recently. How many bytes is recv getting (that's the return value from recv )?
|
|
|
|
|