winsock and dialogboxes

This is a discussion on winsock and dialogboxes within the Networking/Device Communication forums, part of the General Programming Boards category; After the winsock initializeres WORD and WSADATA and the other stuff was put in to the dialog procedure the dialog ...

  1. #1
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638

    winsock and dialogboxes

    After the winsock initializeres WORD and WSADATA and the other stuff was put in to the dialog procedure the dialog box does not show up. Should Word and wsadata stuff be global?

    Code:
    ....
    #include<winsock.h>
    ....
    
    BOOL CALLBACK ServerDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
    {
    
    WORD wVersionRequested = MAKEWORD(1,1);
        WSADATA wsaData;
        int SVR;
    
     SVR  = WSAStartup(wVersionRequested, &wsaData);
    
    	switch(Message)
    	{
    		case WM_INITDIALOG:
    
    		return TRUE;
    
    		case WM_COMMAND:
    			switch(LOWORD(wParam))
    			{
    
                         case IDC_SRVR:
                                 {
    
    ....
    Where it is now the dialogbox does not display.... not sure why. meow. Any thoughts or suggestions?

    ps. note winapi codecolor by codeform!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,496
    Well given that WSAStartup only needs to be called once (you can call it many times, but you need to match it with the same number of WSACleanup calls), I'd say calling it every time in a dialog proc is bad news.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    try:

    Code:
    ....
    #include<winsock.h>
    ....
    
    BOOL CALLBACK ServerDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
    {
    
    WORD wVersionRequested = MAKEWORD(1,1);
        WSADATA wsaData;
        static int SVR = WSAStartup(wVersionRequested, &wsaData);
    
    	switch(Message)
    	{
    		case WM_INITDIALOG:
    
    		return TRUE;
    
    		case WM_COMMAND:
    			switch(LOWORD(wParam))
    			{
    
                         case IDC_SRVR:
                                 {
    
    ....
    also it might not even be a socket related problem, make sure that there are appropriate breaks/returns/gotos in each case (unless you want an intentional fall through, that is handle the cases with the same code) so that execution is not falling through to whatever case closes the window.
    Last edited by Bleech; 04-08-2007 at 12:18 AM. Reason: typo

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  4. #4
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    i made the dialog box first with the bitmaps and editboxes and buttons etc compiled it with out the sock stuff and it compiled fine. then i added the sock stuff and that was when the proiblem
    of not displaying happened. it compiled just fine with no errors with the sock stuff added.

    i put the winsock stuff in the dialog procedure because i wanted to open the dialog box and open the sock stuff at the same time and close it at the same time. every time i thought a new set up.
    so much for theory.

    will give that a try.

    thank you all.

  5. #5
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    ok with that i get

    1115: Illegal initialization in function

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,496
    Lemme guess, the 'static' hack mentioned by sl34k ?

    How about connecting to winsock with the first message (which matters) when the dialog proc is invoked.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    So you are saying put it in the winproc under wm_command ?

    Code:
    		case WM_COMMAND:
    			switch(LOWORD(wParam))
    			{
    where the MAKEINTRESOURCE( is for the dialog box. there is good tutes on winsock and good tutes on winapi but not on combininding the two. this with the dialogboxs is a different way than i am use too. ususally it is just one winmain and no dbs.

    will give that a try.

    eidt going back and reading http://tangentsoft.net/wskfaq/
    Last edited by kryptkat; 04-08-2007 at 11:49 AM.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,496
    Well normally, since you only need one WSAStartup per process, putting it close to the start of winMain() seems to be normal practice, so that you can call WSACleanup just the once when you finally get around to ending the program.

    Or are you trying to defer network activity until absolutely necessary.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    Or are you trying to defer network activity until absolutely necessary.
    exactly. i thought i could put all winsock activity in a dialogbox and that way only call it when needed. was trying to save ram and only when needed sock activity. ok will put in winmain(). was converting console sock prog to win.

    also need to convert argv[] to lpcstr.

    Code:
    main(int argc, char **argv)
    {
        WORD wVersionRequested = MAKEWORD(1,1);
        WSADATA wsaData;
    
    to edit box     EDITTEXT        IDC_TEXTB,...., ES_AUTOVSCROLL | ES_MULTILINE | ES_WANTRETURN from .rc file. 
    
    from dialog srvrproc....
     buff = (char*)GlobalAlloc( GPTR, textlen + 1);
                                                         GetDlgItemText(hwnd,IDC_TEXTB, buff , textlen + 1);
    
    to sock
    int Printsrvr(LPCSTR lpServerNameOrAddress)
    {....
    
        iaHost.s_addr = inet_addr(lpServerNameOrAddress);
    instead of getting **argv from console would be getting webpage from dialogbox idc_textb editbox to buff then convert to lpcstr lpservernameoraddr hence the prob of where to put sock. page not always needed. hope that made sense. again thank you.

    edit convert globalalloc buff to lpcstr lpaddr....
    Last edited by kryptkat; 04-08-2007 at 07:11 PM.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21