|
|
|
Why you cant use of MCI functions?
|
|
|
|
|
Hi~
Actually, I am a student, and working on a chat client program.
I would like to create asynchronous socket but not the default CAsynsocket one.
But I encounter a problem, that is how to create hWnd for WSAAsyncSelect.
I have read several articles in this site as well as some found in the internet.
here is code
<code>
define WM_WINSOCK 12345
SOCKET theSocket = socket(AF_INET, SOCK_DGRAM,0);
if(theSocket == SOCKET_ERROR)
{
MessageBox("Failed socket()");
}
bool sock_opt = TRUE;
setsockopt(theSocket, SOL_SOCKET, SO_REUSEADDR, (char*)&sock_opt, sizeof(sock_opt) );
HWND Window;
HINSTANCE m_hInstance = AfxGetInstanceHandle();
Window = CreateWindow("Hidden_Winsock_Window",
"Winsock Window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
0,
CW_USEDEFAULT,
0,
NULL,
NULL,
m_hInstance,
NULL);
WNDCLASS *windowClass;
windowClass = new WNDCLASS;
windowClass->lpszClassName = "Hidden_Winsock_Window";
windowClass->style = CS_HREDRAW | CS_VREDRAW;
windowClass->lpfnWndProc = MsgRouter;
windowClass->cbClsExtra = 0;
windowClass->cbWndExtra = 0;
windowClass->hInstance = m_hInstance;
windowClass->hIcon = NULL;
windowClass->hCursor = NULL;
windowClass->hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
windowClass->lpszMenuName = NULL;
::RegisterClass(windowClass);
WSAAsyncSelect(theSocket, Window, WM_WINSOCK, FD_READ | FD_WRITE | FD_CONNECT | FD_ACCEPT | FD_CLOSE);
bind(theSocket, (LPSOCKADDR)&clientInfo, sizeof(clientInfo));
LRESULT CTest2Dlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
if ((message == WM_WINSOCK))
.....
return CDialog::WindowProc(message, wParam, lParam);
}
LRESULT CALLBACK CTest2Dlg::MsgRouter(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
CTest2Dlg* pWindow = (CTest2Dlg*)GetWindowLong( hWnd, GWL_USERDATA );
return pWindow->WindowProc(message, wParam, lParam);
}
</code>
Questions here~
Q1)
I dont know whether my method is corret. I would be much appreciated if you can give me advice~
1) call WSAasynselect
2) create hWnd window
3) route the WindowProc function (as it is a member function in a class)
4) register the window
Q2)
Is there any simple method to do the same task?
Q3)
I have found that the program does not work (although it is OK to be compiled and executed)
WindowProc seems cannot catch the message and go into the loop
Is there any errors in my code?
Thank you very much~~
modified on Friday, January 04, 2008 10:51:16 AM
|
|
|
|
|
You already have a window and therefore an HWND in CTest2Dlg so you don't need to create the other window. Try making that change and see what happens.
|
|
|
|
|
IC...
But how can I call or obtain the HWND in the Class CTest2Dlg??
or in other words, is there a command for that???
<code>
HWND window = ???();
WSAAsyncSelect(theSocket, window, WM_WINSOCK, FD_READ | FD_WRITE | FD_CONNECT | FD_ACCEPT | FD_CLOSE);
</code>
|
|
|
|
|
drfankw wrote: is there a command for that???
Yes, but they are difficult to come by because silly Microsoft keeps hiding them in the documentation
|
|
|
|
|
|
For what it's worth, if you had chosen to create a separate window
for handling socket messages, you had the RegisterClass and CreateWindow
code reversed - you'd need to register the class before trying to create
a window of the class.
But since you're using MFC, you should have been using CWnd instead
Cheers,
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Mark Salsbery wrote: For what it's worth
I could tell you but I'm trying to minimize the gratuitous sarcasm in the new year
|
|
|
|
|
led mike wrote: I'm trying to minimize the gratuitous sarcasm in the new year
...while simultaneously maximizing your malformed posts?
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Mark Salsbery wrote: your malformed posts?
Hey CPHog did that! I haven't made a mistake since the Fair.
|
|
|
|
|
led mike wrote: I haven't made a mistake since the Fair.
Now I HAVE to ask...what happened at what fair?
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Mark Salsbery wrote: what happened at what fair?
It's an old thing I got from my grandfather. If my memory is correct they used to have a fair once a year so the saying was a smart ass remark indicating that you rarely made mistakes.
|
|
|
|
|
Ohhh... that makes sense. I thought there might be a good story too
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Hello everyone,
In Bjarne's book, it is mentioned that sort of STL may throw exception, like sorting elements in a vector.
In what situation will sort throw exception? I can not find a case.
thanks in advance,
George
|
|
|
|
|
George_George wrote: In what situation will sort throw exception?
An invalid iterator for example?! (I am just guessing.)
I do not have VC++ (and the source code of STL) at home now, but according to the definition of sort :
std::sort (SGI)[^]
Preconditions
(first, last) is a valid range. And the return type of sort is void .
template <class RandomAccessIterator>
void sort(RandomAccessIterator first, RandomAccessIterator last); So how does sort express errors?! Throw something!
A valid range refers to a valid beginning position and a valid ending position. What about this?
std::vector<T> v1;
std::vector<S> v2;
std::vector<T>::iterator p1 = ... ;
std::vector<S>::iterator p2 = ... ;
std::sort(p1, p2);
Maxwell Chen
modified on Friday, January 04, 2008 1:27:06 PM
|
|
|
|
|
Hi Maxwell,
I think it is a bug of application, not an exception from the STL library implementation sort itself. In your sample, it is developer's fault to pass in two iterations belongs to two different containers.
I think Bjarne's means some *expected* errors from STL library sort implementation itself, not such kind of developer fault -- if we treat such fault as exception, every function of STL throws exception.
Do you have any ideas about some exception which is thrown internally, like bad_alloc thrown from new function?
If you do not agree with me, please feel free to correct me.
have a good weekend,
George
|
|
|
|
|
George_George wrote: In what situation will sort throw exception? I can not find a case
For instance, if a comparasion operation for the type being sorted throws; the exception would not be "swallowed" by sort .
|
|
|
|
|
Hi Nemanja,
How could comparasion operation throw an exception? Could you provide more information please?
regards,
George
|
|
|
|
|
Finding it Funny!. What's the exact statement? can you put it here? I wont be crashing , it's not usual. And from my guess, I can say , if it's a huge list that you are sorting and the same list gets manipulated in another thread, like , removing some elements , adding some. etc. It would crash.
OK,. what country just started work for the day ? The ASP.NET forum is flooded with retarded questions. -Christian Graus
Best wishes to Rexx[^]
|
|
|
|
|
Hi VA_,
Sure, here is the source of the statements.
http://www.research.att.com/~bs/3rd_safe.pdf[^]
You can find it from the 1st sample. It is appreciated if you could give some comments why do you think sort will throw any exceptions?
regards,
George
|
|
|
|
|
Ahh boy! Listen the first question that should have arised and get solved immediately is that "How should a sort would fail?".. the next is that "The sort is designed to sort things and not to fail,so it WILL NOT FAIL".
If you look at the document, it says "USER DEFINDED SORTS", ok I make an example of what they are talking about. Just try to execute the below sample in your machine twice. The next time you run it, remove the i=i/0 line from the code.
#include <vector>
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;
class test
{
private:
public:
int nValue;
bool operator <(test obj)
{
int i= i/0;
if(this->nValue<obj.nValue)
{
return(true);
}
else
{
return(false);
}
}
};
int main(int argc, char* argv[])
{
printf("Hello World!\n");
std::vector<test> vec_testObj;
test testObjs[4];
testObjs[0].nValue =5;
testObjs[1].nValue =4;
testObjs[2].nValue =3;
testObjs[3].nValue =2;
vec_testObj.push_back(testObjs[0]);
vec_testObj.push_back(testObjs[1]);
vec_testObj.push_back(testObjs[2]);
vec_testObj.push_back(testObjs[3]);
std::sort(vec_testObj.begin(),vec_testObj.end());
vector<test>::iterator itr_vecTest = vec_testObj.begin();
while(vec_testObj.end()!=itr_vecTest)
{
cout<<"\n"<<itr_vecTest->nValue;
itr_vecTest++;
}
return 0;
}
OK,. what country just started work for the day ? The ASP.NET forum is flooded with retarded questions. -Christian Graus
Best wishes to Rexx[^]
|
|
|
|
|
Thanks VA_,
I think your idea is user defined < operator may fail (in your sample, it is a divide by 0 exception) with exception, which will make STL sort algorithm throw exception (I think STL sort does not catch any exception), right?
regards,
George
|
|
|
|
|
That's the point.
-Vunic
OK,. what country just started work for the day ? The ASP.NET forum is flooded with retarded questions. -Christian Graus
Best wishes to Rexx[^]
|
|
|
|
|