|
Read the MSDN stuff again and found I should have been using GetSysColorBrush(COLOR_MENU); rather then the GetSysColor(COLOR_MENU); I was using which apparently 'returns a cached brush, instead of allocating a new one'. This now does the correct menu background colour.
Could explain why GetSysColor(COLOR_MENU); worked yesterday and not today.
|
|
|
|
|
Thanks for the info! We learn something new every day...
|
|
|
|
|
Hi there everyone.
I have some pretty much basic UDP sockets scenario: my app requests data from server and receives requested data. There is a lot of data so there is a cancellation method as well. When I cancel data receiving and request data again, app not getting FD_READ events anymore. Strange thing is, it is not always the same - sometimes there are a couple of FD_READs, sometimes half of the data could be transmitted before it stops, sometimes - none.
Code itself is basically the same as anywhere on the net and goes like this:
1. Before any receiving, check if FD_READ is set
DWORD dwResult =
::WSAWaitForMultipleEvents(vecHandles.size(), &vecHandles[0], FALSE, dwTimeout, FALSE);
- vecHandles is a vector of event handles, which includes my WSAEVENT for triggering cancellation.
which is done like this:
::WSASetEvent(hEvent);
So now ::WSAWaitForMultipleEvents will return with my event.
2. Check which event we got: if it is read event, then call recv.
if( socket_event_read * pReadEvent = dynamic_cast<socket_event_read *>(pSocketEvent) )
{
WSANETWORKEVENTS nt = { 0x00 };
if( SOCKET_ERROR != ::WSAEnumNetworkEvents(pReadEvent->get_socket(), pSocketEvent, &nt) )
{
if( nt.lNetworkEvents & FD_READ && nt.iErrorCode[FD_READ_BIT] == false )
{
::WSARecvFrom(...);
}
}
}
3. Handle data.
That's it. It all works great if receiving is never cancelled. Otherwise it just stops at some point while waiting for multiple events (::WSAWaitForMultipleEvents ) Again, what I mean is:
I hit cancel button and
::WSASetEvent(hEvent); is called, that causes ::WSAWaitForMultipleEvents to return with my cancel event (WSAWaitForMultipleEvents is called in a loop for every incoming data chunk - each one is about 512kb). And then app just stops receiving - it leaves socket as it is, socket is not closed and still alive. Then I hit a button again for app to begin download data again and that is when app stops receiving packets because WSAWaitForMultipleEvents is waiting for fd_read which is never signaled. Then I hit cancel again, - this is handled properly and then again download data - this time data is downloading ok.
This behavior occurs all the time.
I am struggling with this for a couple of days now and got completely no idea what is going on, have searched all the net with no result. I have tried checking, when cancellation is issued, if there is still any data pending on the socket and reading this data - all in vain - it is still remains stuck and not getting fd_read event
Maybe this issue is related to the fact that app is using one single socket all the time? Maybe it is because server fails and stops sending data? (I have to investigate server as well)
Maybe there is something that you may know and be able to help me with this?
Thanks in advance.
011011010110000101100011011010000110100101101110
0110010101110011
|
|
|
|
|
Could you please post the code of the whole network loop? This info isn't enough. Send also the code/functions (like cancel) that are used to send signal to your network thread and indicate which thread calls what and what does the network thread does in case of a cancel.
BTW, why are you using UPD? You should use TCP to transfer data. In case of UDP you have to implement congestion control and reliability (defense against packet loss) yourself. Is it some kind of real-time data transfer (like audio/video streaming) where data loss is not a problem?
modified 17-Aug-13 13:15pm.
|
|
|
|
|
I think I am going to deal with it myself - code is too big and complicated to post it, or to even take one method from it - it is 1000+ lines custom network framework
I am using UDP for consistency with discovery mechanisms, which I am implementing in app - it should do a lot of networking, pretty fast, without any user intervention - tcp will not allow me to, - it will force a solution where app will have to be configured by user and I cannot allow it to.
I could do discovery only with UDP but I have written huge framework which actually implements TCP partially.
Basically, I am not sure when FD_READ gets signaled? When there is data available or when WSARecvFrom can perform read? This would explain why it is not raised...
I am going to deal with it on my own - it is too complicated
011011010110000101100011011010000110100101101110
0110010101110011
|
|
|
|
|
The main loop of your network thread that handles the send and receive should be relatively small and should be kept together in a small place separated from the other parts of the code. If this critical piece of code is not coupled well separated from your higher level logic, then you should refactor it. This piece of code should handle just raw outgoing and incoming packets on the network thread and all the logic should be implemented separately on top of this layer. A low-level data recv/send loop implementation should be around 100-200 lines of code and all it knows is sending bytes/packets from your send queue, receiving them into a recv queue (and notifying your high level handlers) and exiting when your application exits.
if( socket_event_read * pReadEvent = dynamic_cast<socket_event_read *>(pSocketEvent) )
{
WSANETWORKEVENTS nt = { 0x00 };
if( SOCKET_ERROR != ::WSAEnumNetworkEvents(pReadEvent->get_socket(), pSocketEvent, &nt) )
{
if( nt.lNetworkEvents & FD_READ && nt.iErrorCode[FD_READ_BIT] == false )
{
::WSARecvFrom(...);
}
}
}
For example here your application logic and the a part of your raw network data receiver logic is already mixed that is bad and dynamic_cast is always the indicator of some hacks and bad OO design that needs refactoring. We don't even know when does that dynamic_cast result in a non-null pointer... It seems that you have a big bunch of refactorable code that you will be fixing till the end of your life without a rewrite (and it is worth rewriting stuff... )
BTW, there is another thing that came to my mind regarding WSAWaitForMultipleEvents: FD_READ is level triggered but FD_WRITE is edge triggered and many guys get confused of this. This means that waiting for an FD_READ is safe even if you wait for it twice without reading anything from the socket. The event isn't cleared until data is available for read in the recv buffer of the socket. But FD_WRITE is edge triggered: If you receive an FD_WRITE event then you must assume that FD_WRITE is set until your write fails with EWOULDBLOCK. Receiving an FD_WRITE event clears the FD_WRITE event immediately and it will be set again only when the send buffer of the socket transitions from full to non-full next time. If you try to wait for an FD_WRITE without writing all the available buffer space by receiving EWOULDBLOCK then you will wait for FD_WRITE forever! Depending on how you handle WSAEventSelect and WSAWaitForMultipleEvents your logic can get stuck in many different ways if you don't handle this well.
EDIT: Besides the wakeup event there should be a lock/critical_section as well and these should be used together well along with the shared data (like cancel flag). My loop often got stuck in initial/new network code by screwing up the usage of these or the communication between threads somehow.
modified 17-Aug-13 14:27pm.
|
|
|
|
|
Thanks for your suggestions
Well, I knew about fd_write thing
Part about critical section got me thinking for sure - that is something I haven't considered in current implementation.
Anyways, thanks very much, I need a moment to think about all this.
011011010110000101100011011010000110100101101110
0110010101110011
|
|
|
|
|
|
I can use DirectShow to connect to a device ( source) , for example audio, I can process the audio “steam” , or not, and “render it to audio device (sink) or GUI.
Or I can use DirectSound to do the same.
So what are the relations between these DirectX “processes” , is one a subset of the other or what?
Thanks for educating me, even if this is not pure C / MFC question.
I do appreciate your time.
Cheers Vaclav
|
|
|
|
|
|
Just found DirectSound API used as audio renderrer in DirectShow graph, hence DirectShow is on first IMHO
|
|
|
|
|
hello;
I tried to write a function that calculates the harmonic average as follows:
long double Harmonique(long double *t,int l)
{
long double s=0.0;
for(int i=0;i<l;i++)
{
if(t[i]==0){
printf("error!!!");
s +=1.0/(1+t[i]);}
else s +=1.0/t[i];
}
return l/s;
}
the console I have this error -1.#IND00 .
what is the origin of this error ?
cordialy
|
|
|
|
|
what are the values of 'l' and 's', when you hit this line:
return l/s;
|
|
|
|
|
I think you should check address of pointer and length of array
|
|
|
|
|
I have checked on visual studio 2010 , you can check again
#include <iostream>
#include <stdio.h>
using namespace std;
long double Harmonique(long double *t,int l)
{
long double s=0.0;
for(int i=0; i<l; i++)
{
if(*t == 0)
{
printf("error!!!");
s +=1.0/(1+(*t));
}
else
{
s +=1.0/(*t);
}
t++ ;
}
return l/s;
}
int main()
{
long double arr[] ={1,5,5,7,43};
long double temp = Harmonique(arr ,5);
cout << temp <<endl;
return 0;
}
3.19262
Press any key to continue . . .
Thanks
Thong Le Trung
|
|
|
|
|
Here, I found the solution. the * t I used in "main" was of type int, so there was a problem of division by zero.
thank you for example and help.
|
|
|
|
|
I am sorry , I see error for you .
I have a example , i hope this example will help you.
What will be output of the following program?
#include<stdio.h>
int main(){
float a=0.7;d
if(a<0.7){
printf("C");
}
else{
printf("C++");
}
return 0;
}
EXPLANATION
Output:
Turbo C++ 3.0: c
Turbo C ++4.5: c
Linux GCC: c
Visual C++: c
Explanation:
0.7 is double constant (Default). Its binary value is written in 64 bit.
Binary value of 0.7 = (0.1011 0011 0011 0011 0011 0011 0011 0011 0011 0011 0011 )
Now here variable a is a floating point variable while 0.7 is double constant. So variable a will contain only 32 bit value i.e.
a = 0.1011 0011 0011 0011 0011 0011 0011 0011 while
0.7 = 0.1011 0011 0011 0011 0011 0011 0011 0011 0011 0011 0011....
It is obvious a < 0.7
|
|
|
|
|
|
Hi ,all,
When I open a .txt file .I think Notepad will call NtReadFile function .
If I modify content .txt file and then I save txt file . I think
Notepad will call Notepad will call NtWriteFile function .
My question :
When I open .txt file ,and next I choose "save as" .
How many functions does Notepad call to be "save as" ?
Will Data store on buffer or hard disc before we choose "save as "?
How many functions does "Microsoft Word" call when we choose "save as" ?
I think in the case "Microsoft Word" will use memory mapped file to be " save as"
Thanks
Thong Le Trung
modified 16-Aug-13 5:26am.
|
|
|
|
|
It will call NtCreeateFile to change the name. As for the file itself, it will be in memoery generally unless it is so big it gets paged out or read in in chunks.
|
|
|
|
|
Hi,Erudite_Eric,
Before I write data , I will hook NtWriteFile function and encrypt content file .
Before I read data , I will hook NtReadFile function and decrypt content file to display file
My code work well with "Notepad" , but crash with "Microsoft Word" when I choose "save as"
I have checked .pdf file , .pdf file still works well .
Thank you for your time.
Thong Le Trung.
|
|
|
|
|
Well obviously I cant say why your application is crashing.
|
|
|
|
|
Function NtCreateFile will call to change the name when we choose "save as" .
Do I wonder content new file will have from ? .
I think after create new file, it will read File from old file and then it will write data to new file.
|
|
|
|
|
No, it wont do that. The kernel API does exactly what it says it does. If it says it create a file it does, it doesnt copy a file.
Believe me, I have spent 16 years writing windows kernel code.
--edit--
Oh look a one voter. Well, NtCreateFIle etc is the API you use inside the kernel and I have used it a lot. so mr one voter, ypu are wrong.
--edit--
Oh look, another 1 voter. What fun!
modified 20-Aug-13 8:50am.
|
|
|
|
|
Hi, Erudite ,
I graduated about one year so I don't have much experience about Win kernel .Thank you for helping me
Thong Le Trung
|
|
|
|