|
As mentioned, the main is exiting before the thread can even execute. To wait for the thread to terminate, you can do a WaitForSingleObject(hThread, INFINITE); . This will wait for the thread handle to become "invalid," which occurs when the thread terminates. At that point, you should also do a CloseHandle(hThread); before exiting.
Bob Ciora
|
|
|
|
|
 I agree with cedric. I recommend you look into events, they help control synchronization between the thread and your app.
Here's what I'd usually do:
struct threadPkg {<br />
HANDLE hStartup, hStopNow, hShutdown;<br />
};<br />
<br />
UINT ThreadFunc (LPVOID lpParam) {<br />
threadPkg * pkg = (threadPkg *) lpParam;<br />
::SetEvent(pkg->hStartup);<br />
<br />
while (true) {<br />
switch (::WaitForSingleObject(pkg->hStopNow, 10)) {<br />
case WAIT_OBJECT_0: {<br />
::SetEvent(pkg->hShutdown);<br />
AfxEndThread(0, true);<br />
return 0;<br />
} break;<br />
case WAIT_TIMEOUT: {<br />
} break;<br />
}<br />
}<br />
}<br />
<br />
bool StartThread (threadPkg * pkg) {<br />
CWinThread * thread = AfxBeginThread(ThreadFunc, pkg, THREAD_PRIORITY_IDLE, 0, 0, NULL);<br />
<br />
switch (::WaitForSingleObject(pkg->hStartup, INFINITE)) {<br />
case WAIT_OBJECT_0: { return true; } break;<br />
case WAIT_TIMEOUT: {
return false;
} break;<br />
}<br />
}<br />
<br />
void StopThread (threadPkg * pkg) {<br />
::SetEvent(pkg->hStopNow);<br />
switch (::WaitForSingleEvent(pkg->hShutdown, INFINITE)) {<br />
case WAIT_OBJECT_0: break;<br />
}<br />
}<br />
<br />
void main () {<br />
threadPkg * pkg = new threadPkg;<br />
pkg->hStartup = ::CreateEvent(NULL, true, false, "startup sequence, thread->main");<br />
pkg->hStopNow = ::CreateEvent(NULL, true, false, "signal to stop now, main->thread");<br />
pkg->hShutdown = ::CreateEvent(NULL, true, false, "thread has shut down, thread->main");<br />
<br />
if (StartThread(pkg)) {<br />
StopThread(pkg);<br />
} else {<br />
MessageBox(NULL, "Error: The worker thread was not started.", "Critical Error", MB_OK);<br />
}<br />
}
What that will do is begin a handshaking process when you start up the thread. This function creates 3 events and do what the text description says. It creates the thread, then waits for the thread to tell it that it has started and is on it's feet. You can do what you want and the thread will continue working, occaisionally asking if it's been told to quit. When you are ready to stop the thread, the function is called and it sends the hStopNow singal, the thread catches it, quits nicely and simultaneously signals back it's received and has shutdown. This is the basic organization of my threads I use all the time. Of course, what happens in main depends on what you want, you can put the start and end thread functions however you need them.
Note: I just wrote that off the cuff, I haven't tested that exact code but it looks correct in it's entirety.
Good Luck
|
|
|
|
|
Sometimes I wonder whether I need a C++ geek or a shrink to solve my coding problems...
(info: I am using Visual Studio .NET 2003 7.1.3008 on Windows XP Pro SP 2)
With the project wizard of my Visual Studio I made an MFC application with multiple documents (MDI). I assigned a file type to my project: .nix (my .nix files are plain text files). In addition to the now automatically created code I added in the Doc an OnOpenDocument reading the first five lines of the openend file, storing them in a list of std::strings. I even added in the View in OnDraw a simple routine that prints those list of stings from the Doc straight to the pDC with TextOut. So far, nothing spectacular.
Problem #1
After starting the application the very first time my file type associations get registered. Afterwards my .nix files have the default icon of my application and are even named correctly. Now when I double-click those files in the Windows Explorer my application starts BUT the .nix file is never read and no document is opened. I can open those files manually with "File" -> "Open..." with my application but the double-click on the file itself never ever works.
Problem #2
When I start my application by double-clicking the .exe everything is fine. But when I start the application by double-clicking an associated file .nix (next to my problem #1) there is a very strange effect: when I simply close the application again the function CWinApp::ExitInstance throws an exception in line 661 of appcore.cpp because CWinapp::m_pCmdInfo has a value of 0x00000001 which obviously is a pointer no nowhere (and having a pointer address of exactly 1 is like winning the lottery, right?!).
As I said, next to my trivial code additions described above this all comes from MFC source files or automatically generated code by the project wizard. It's hard to believe that this code is so bad that it could go so horribly wrong.
I guess a huge amount of other people make MFC MDI applications with associated file types so all those other people should suffer from those problems, too, shouldn't they?! Or is it a problem only for me?! On the other hand I do have the same behaviour of my application on other PCs.
Help needed... (at least moral support...)
Bye,
Matthias
P.S.: my little example project can be found here
-- modified at 4:38 Wednesday 7th September, 2005
|
|
|
|
|
If I don't want the length of the string to be sent but I just want the sting that I type send to server. How to I change the code? Bytesend to strsend? how to change the lstrlen?
// Send and receive data.
long bytesSent;
long bytesRecv = SOCKET_ERROR;
char sendbuf[300] = "Client: Sending data.";
char recvbuf[300] = "";
bytesSent = send( m_socket, sendbuf, lstrlen(sendbuf), 0 );
printf( "Bytes Sent: %ld\n", bytesSent );
while( bytesRecv == SOCKET_ERROR )
{
bytesRecv = recv( m_socket, recvbuf, 300, 0 );
if ( bytesRecv == 0 || bytesRecv == WSAECONNRESET )
{
printf( "Connection Closed.\n");
break;
}
if (bytesRecv < 0)
return;
printf( "Bytes Recv: %ld\n", bytesRecv );
}
return;
|
|
|
|
|
What's your problem ? For me, this code looks fine (except that I don't know what send, recv, ... do).
Your send function needs to know how many bytes to send so you have to specify the number of bytes in your string.
Is there a problem with that?
|
|
|
|
|
The code don't have any problem. After I compile, it will show bytesrecv: 21 which is the length of the string. I don't want the length of the string to print out but is the string itself print out. how do I program it?
|
|
|
|
|
Replace:
if (bytesRecv < 0)<br />
return;<br />
printf( "Bytes Recv: %ld\n", bytesRecv );<br />
}<br />
by:
if (bytesRecv < 0)<br />
return;<br />
printf( "String Recv: %s\n", recvbuf);<br />
}<br />
|
|
|
|
|
Its work! How do I change it so that I can type on the dos and send it. The code now is when I type what I want to send in this:
char sendbuf[300] = " ";
and after I compile, it will automatically send to the server. But what I want is when I compile it, I can type on it and then send it to the server.
|
|
|
|
|
It's possible that you have a problem with this line:
bytesSent = send( m_socket, sendbuf, lstrlen(sendbuf), 0 );
Since you use the length of sendbuf, you won't transmit the terminating NULL-byte of sendbuf.
This could present a problem in the receiving end.
If you wan't the original data (a char array) in you have got two options:
1.
Send the terminating NULL-character by doing this:
bytesSent = send( m_socket, sendbuf, lstrlen(sendbuf) + 1, 0 );
2.
At the receiving end, add a '\0' in the receive buffer at offset <noofreceivedbytes>. For instance:
bytesRecv = recv( m_socket, recvbuf, 300, 0 );
recvbuf[bytesRecv] = '\0';
Else, the code looks fine to me.
|
|
|
|
|
Hi
anyone have any idea on how to insert page number into ms word doc. I have an application that creates a report, now I need to include the page number as well.
I tried HeaderFooter class and PageNumbers class but ends up with illegal operation msg. Anyone have any idea?
Thanks in advance.
Thomas
|
|
|
|
|
How to change font of caption of a MFC dialog box ??
Any idea?
Ana
Keywords
Font Caption of Dialog Box, Caption Font of Dialog Box, Dialog Box Caption Font
|
|
|
|
|
u could try the following
CFont font;
font.CreateFont(0,0,0,0,FW_NORMAL,FALSE,FALSE,0,ANSI_CHARSET,OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH | FF_SWISS,"MS Shell Dlg");
((CEdit*)GetDlgItem(IDC_EDIT1))->SetFont(&font);
this will change the font in that particular edit box
|
|
|
|
|
But, I want to change Font of caption (title) of dialog box ???
Ana
|
|
|
|
|
There's a child windows for which I want to record its visibility after the main program exits. But the value IsWindowVisible() in OnDestroy() returns always zero, where else can I record its visivility before it gets destroyed?
|
|
|
|
|
LiYS wrote:
But the value IsWindowVisible() in OnDestroy() returns always zero...
OnDestroy() is called after the CWnd object is removed from the screen.
LiYS wrote:
...where else can I record its visivility before it gets destroyed?
Try OnClose() .
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
|
|
|
|
|
Respected All
I am a begginer n VC and having facing problem.I already sent such like problem but no fruitfull help comes to me, now I am going to explain a different manner ,may be helping me.
" I have to load a number of bitmaps for that I used CImageList which loaded all the bitmaps from desk but while using this class you have to fix the size of Imagelist ,but I want that i have temporary buffer like CImageList which reserve memory according to each bitmap size and no more ,,
so
--- is there customize property in CImageList in which we would not bound to fix the size if not
...Then is there any alternate method or class which will fullfill my requirment.
urgently needed plz guide me.
David
david
|
|
|
|
|
|
You can handle the LVN_ENDLABELEDIT message in the CListCtrl, and in the message handler, post a custom Windows message to the parent:
void CMyListCtrl::OnEndLabelEdit(NMHDR* pNMHDR, LRESULT* pResult)
{
...
GetParent()->SendMessage(WM_LISTWASMODIFIED);
...
}
There's a great article on custom windows messages somewhere on CodeProject. I don'w remember who wrote it, but if you google, "custom windows messages codeproject", you should find it.
Hope this helps.
-Ian
|
|
|
|
|
This message will not be sent, when i use SetItemText to change the item
text.
I want the parent to be notified when SetItemText is called, because I don't call SetItemText in CListCtrl's parent.
|
|
|
|
|
When an item is changed, the control will send a LVN_ITEMCHANGED notification to its parent.
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
|
|
|
|
|
Is there any code for seaching a pattern in a given string
example
String given for search -
HELLO THIS IS A TEST
Pattern given for search -
1 - ?ELLO
2 - *IS A
3 - THIS*
4 - *THIS*
5 - T?IS
6 - ?ELLO
7 - ??LLO
These all must return as found the pattern in string
? - For any charecter
* - Any word
|
|
|
|
|
|
Hi,
I have a VB6 application with a listview control in it. I have written an app in VC++ to
get the handle of the ListView control of the VB6 app.Then I used Jeff Ritcher's suggestion
from http://www.microsoft.com/msj/0997/win320997.aspx and according read the items from the
listview.Then I have to select the item. The problem comes here.When I select an item in the
listview, it gets selected ,but the previously selected item is still hilighted.The multiple
selection property of the listview is set to false.I have also tried to deselct the items.But
it didnot work.I am pasting the code below:
void CListtestDlg::OnButton1()
{
CString str1,str2,str3,str4="Item1";
HWND hWnd=::FindWindow ("ThunderRT6FormDC",NULL);
str1.Format ("%x",hWnd);
//::MessageBox(0,str1,"Handle1",MB_OK);
HWND hWnd1=::FindWindowEx (hWnd,NULL,"ListView20WndClass",NULL);
// HWND hWnd1=::GetWindow (hWnd,GW_CHILD);
HWND hWnd2=::FindWindowEx (hWnd,NULL,"ThunderRT6CommandButton",NULL);
str2.Format ("%x",hWnd2);
//::MessageBox(0,str2,"Handle2",MB_OK);
// BOOL hWnd2=::EnableWindow (hWnd,TRUE);
//int m=::GetDlgCtrlID (hWnd1);
/* if(hWnd2)
{
str3.Format ("%d",hWnd2);
::MessageBox (0,str3,"m",MB_OK);
}
//long c=::SendDlgItemMessage(hWnd,
*/
//::SendMessage(hWnd1,LVN_INSERTITEM,1,5);
TCHAR sz[100];
LV_ITEM lvItem;
//LPTSTR lpText=(LPVOID*)::HeapAlloc (::GetProcessHeap (),0,200);
//int x;
//x=ListView_GetCountPerPage(hWnd1);
// lvItem.mask=LVIF_TEXT;
//lvItem.pszText="Item 100";
// lvItem.iItem=0;
// lvItem.iSubItem=0;
// lvItem.pszText=sz;
//lvItem.cchTextMax= sizeof(sz);
//LRESULT lResult = ::SendMessage(hWnd1, LVM_GETITEM, 0, (LPARAM) &lvItem );
//BOOL b = ListView_GetItem(hWnd1,&lvItem);
TCHAR* szText1=NULL;
TCHAR* szText2=NULL;
TCHAR* szText3=NULL;
TCHAR *szText4=" Item 1";
// int n=ListView_GetItemCount(hWnd1);
// str3.Format ("%d",n);
//::MessageBox (hWnd1,str3,"Item count",MB_OK);
getItemTextFromListBox(hWnd1, 0,0, &szText1);
getItemTextFromListBox(hWnd1, 0,1, &szText2);
getItemTextFromListBox(hWnd1, 1,0, &szText3);
// ListView_SetItemState(hWnd1, -1, 0, LVIS_SELECTED)
//int x=ListView_GetItemState(hWnd1,1,1);
// str3.Format ("%d",x);
//::MessageBox (0,str3,"Value",MB_OK);
// ListView_SetItemState(hWnd1,-1,0,LVIS_SELECTED );
//ListView_SetHotItem(hWnd1,2);
//ListView_RedrawItems(hWnd1,0,2);
if( 0==_tcscmp(szText1,szText4))
{
TRACE("Matched");
//
// ListView_RedrawItems(hWnd1,0,2);
// int dd=ListView_GetSelectedCount(hWnd1);
//if(dd>0)
//{
//::SetFocus (hWnd1);
// ::SetActiveWindow (hWnd1);
//ListView_SetItemState(hWnd1, -1, 0, 0);
//::SendMessage(hWnd1,WM_SETFOCUS,0,0);
::SendMessage(hWnd1,WM_SETFOCUS,0,0);
::SendMessage(hWnd1,LVM_SETITEMSTATE,0,0);
//::SendMessage (hWnd1,LVM_REDRAWITEMS,0,0);
//ListView_SetItemState(hWnd1, -1, 0, (LVIS_FOCUSED) ); // deselect // deselect
// ::SetFocus (hWnd1);
// ListView_RedrawItems(hWnd1,0,2);
//if(!x)
//{
// ListView_SetItemState(hWnd1, -1, 0, LVIS_SELECTED);
//ListView_SetItemState(hWnd1, 1, 0, LVIS_SELECTED );
//}
//if(dd>0)
//{
RECT re;
//ListView_GetViewRect(hWnd1,&re);
//long d=re.left ;
//long e
//BOOL k= ::UpdateWindow (hWnd1);
int x=ListView_GetSelectedCount(hWnd1);
// ListView_SetItemCount
//ListView_SetItemState(hWnd1, 2, LVIS_SELECTED , LVIS_SELECTED );
//}
//:: UpdateWindow(hWnd1);// select iListPos
//ListView_SetItemState(hWnd1, 0, LVIS_FOCUSED , LVIS_FOCUSED );
//::UpdateWindow(hWnd2);
// ListView_Update(hWnd1,1);
//Button_Click(hWnd2);
//::SendMessage(hWnd2,WM_LBUTTONDOWN,0,0);
//::SendMessage(hWnd2,WM_LBUTTONUP,0,0);
}
if(szText1)
delete szText1;
if(szText2)
free(szText2);
if(szText3)
free(szText3);
}
bool
getItemTextFromListBox(HWND hWndListView, int item, int subItem, TCHAR** ppwszText)
{
bool retcode=false;
TCHAR szReadBuffer[1024];
memset(szReadBuffer, 0, sizeof(szReadBuffer) );
LV_ITEM* plvi=NULL;
DWORD dwProcessId;
GetWindowThreadProcessId(hWndListView, &dwProcessId);
// Open a handle to the remote process's kernel object
HANDLE hProcess = OpenProcess(
PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE,
FALSE, dwProcessId);
if (hProcess == NULL) {
MessageBox(NULL, __TEXT("Could not communicate with process"),
"ERROR", MB_OK | MB_ICONWARNING);
goto cleanup;
}
// Allocate memory in the remote process's address space
plvi = (LV_ITEM*) VirtualAllocEx(hProcess,
NULL, 4096, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
if(plvi==NULL) {
goto cleanup;
}
// Get the ListView item's text data
// Initialize a local LV_ITEM structure
LV_ITEM lvi;
memset(&lvi, 0, sizeof(LV_ITEM) );
lvi.mask = LVIF_TEXT;
lvi.iItem = item;
lvi.iSubItem = subItem;
// NOTE: The text data immediately follows the LV_ITEM structure
// in the memory block allocated in the remote process.
lvi.pszText = (LPTSTR) (plvi + 1);
lvi.cchTextMax = 100;
// Write the local LV_ITEM structure to the remote memory block
if( !WriteProcessMemory(hProcess, plvi, &lvi, sizeof(lvi), NULL) ) {
goto cleanup;
}
// Tell the ListView control to fill the remote LV_ITEM structure
ListView_GetItem(hWndListView, plvi);
// Read the remote text string into the end of our clipboard buffer
if( !ReadProcessMemory(hProcess, plvi + 1, (LPVOID) &szReadBuffer, sizeof(szReadBuffer), NULL) ) {
goto cleanup;
}
*ppwszText = (TCHAR*) malloc( _tcslen( szReadBuffer ) + sizeof(TCHAR) );
if(!*ppwszText) {
goto cleanup;
}
retcode=true;
_tcscpy(*ppwszText, szReadBuffer);
// ListView_SetItemState(hWndListView, -1, 0, LVIS_SELECTED);
cleanup:
// Free the memory in the remote process's address space
if(hProcess) {
VirtualFreeEx(hProcess, plvi, 0, MEM_RELEASE);
// Cleanup and put our results on the clipboard
CloseHandle(hProcess);
}
return retcode;
}
|
|
|
|
|
A general way is appreciated. Thanks.
|
|
|
|
|
By "flat" do you mean without the 3D style?
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
|
|
|
|
|