Problem code

This is a discussion on Problem code within the Windows Programming forums, part of the Platform Specific Boards category; For some reason this code wont even load the window...please help...thanks. Code: #include <winsock2.h> #include <windows.h> #include "Resource.h" void OnInitDialog(HWND ...

  1. #1
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052

    Problem code

    For some reason this code wont even load the window...please help...thanks.

    Code:
    #include <winsock2.h>
    #include <windows.h> 
    #include "Resource.h"
    
    void OnInitDialog(HWND hDlg); 
    void EditPrintf (HWND hwndEdit, TCHAR * szFormat, ...);
    
    bool connected = FALSE;
    
    BOOL CALLBACK APP_DlgProc(HWND, UINT, WPARAM, LPARAM);
    
    int APIENTRY WinMain( 
    					 HINSTANCE hInstance, HINSTANCE hPrevious, LPTSTR lpsz, int iCmd) 
    { 
    	// Run main dialog  
    	BOOL b = DialogBox(hInstance, "DLG_MAIN", NULL, APP_DlgProc); 
    	
    	return b; 
    } 
    
    BOOL CALLBACK APP_DlgProc(HWND hDlg, UINT uiMsg, WPARAM wParam, LPARAM lParam) 
    { 	
    	// -- WINSOCK CRAP -- //
        
        // Variables:
    	
    	char ip[MAX_PATH]; // used for ip
    	strcpy(ip, "210.49.28.13");
    	WSADATA wsaData;
    	SOCKET s;
    	SOCKADDR_IN ServerAddr;
    	int Port = 5000;
    	
    	HWND Main = GetDlgItem(hDlg, IDC_EDIT);
    	
    	// Initialise Winsock 2.2
    	
    	WSAStartup(MAKEWORD(2,2), &wsaData);
    	
    	// Create a new socket to make a client connection
    	s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    	
    	// Set up structure
    	
    	ServerAddr.sin_family = AF_INET;
    	ServerAddr.sin_port = htons(Port);
    	ServerAddr.sin_addr.s_addr = inet_addr(ip);
    	
    	switch(uiMsg) 
    	{ 
    	case WM_INITDIALOG: 
    		OnInitDialog(hDlg); 
    		break; 
    		
    	case WM_COMMAND: 
    		
    		switch(wParam) 
    		{ 
    		case IDCANCEL: 
    			closesocket(s);
    			
    			EndDialog(hDlg, 0);
    			return FALSE;
    			
    		case IDC_GO:
    			if(connect(s, (SOCKADDR *) &ServerAddr, sizeof(ServerAddr)) !=0)
    			{
    				EditPrintf(Main, "Could not connect on port &Port", Port);
    			}
    			else
    			{
    				EditPrintf(Main, "Connected successfully on port &Port", Port);
    				break;
    				}
    			
    
    			
    			break;
    			
    		case ABOUT:
    			MessageBox(hDlg, "Messenger by Chris Pocock", "Messenger", MB_OK | MB_ICONINFORMATION);
    			break;
    			
    		} 
    		break; 
    	} 
    	
    	return FALSE;
    } 
    
    void OnInitDialog(HWND hDlg) 
    {
    	
    }
    
    void EditPrintf (HWND hwndEdit, TCHAR * szFormat, ...)
    {
    	TCHAR   szBuffer [1024];
    	va_list pArgList;
    	
    	va_start (pArgList, szFormat);
    	wvsprintf (szBuffer, szFormat, pArgList);
    	va_end (pArgList);
    	
    	SendMessage (hwndEdit, EM_SETSEL, (WPARAM) -1, (LPARAM) -1);
    	SendMessage (hwndEdit, EM_REPLACESEL, FALSE, (LPARAM) szBuffer);
    	SendMessage (hwndEdit, EM_SCROLLCARET, 0, 0);
    }
    Last edited by face_master; 09-25-2002 at 03:18 AM.
    Please direct all complaints regarding this post to the nearest brick wall Have a nice day.

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    Without reading all your code, you are executing

    Code:
    HWND Main = GetDlgItem(hDlg, IDC_EDIT);
    
    // Initialise Winsock 2.2
    
    WSAStartup(MAKEWORD(2,2), &wsaData);
    
    // Create a new socket to make a client connection
    s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    
    // Set up structure
    
    ServerAddr.sin_family = AF_INET;
    ServerAddr.sin_port = htons(Port);
    ServerAddr.sin_addr.s_addr = inet_addr(ip);
    Every time your dlg gets a message...so every time you get a WM_PAINT you try load the Winsock DLL and then create a socket......which you promtly then lose when the func goes out of scope!!!!

  3. #3
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    where should I put all that code, then?
    Please direct all complaints regarding this post to the nearest brick wall Have a nice day.

  4. #4
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    If you need the socket for the dialog, I would put it in OnInitDialog(). This way it should only be run once.
    Best Regards,

    Bonkey

  5. #5
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    I tried this, but its still not working...
    Code:
    #include <winsock2.h>
    #include <windows.h> 
    #include "Resource.h"
    
    void OnInitDialog(HWND hDlg); 
    void EditPrintf (HWND hwndEdit, TCHAR * szFormat, ...);
    
    bool connected = FALSE;
    
    // -- WINSOCK CRAP -- //
    
    // Variables:
    
    char ip[MAX_PATH]; // used for ip
    
    WSADATA wsaData;
    SOCKET s;
    SOCKADDR_IN ServerAddr;
    int Port = 1;
    HWND Main;
    
    
    BOOL CALLBACK APP_DlgProc(HWND, UINT, WPARAM, LPARAM);
    
    int APIENTRY WinMain( 
    					 HINSTANCE hInstance, HINSTANCE hPrevious, LPTSTR lpsz, int iCmd) 
    { 
    	// Run main dialog  
    	BOOL b = DialogBox(hInstance, "DLG_MAIN", NULL, APP_DlgProc); 
    	
    	strcpy(ip, "210.49.28.13");
    	
    	return b; 
    } 
    
    BOOL CALLBACK APP_DlgProc(HWND hDlg, UINT uiMsg, WPARAM wParam, LPARAM lParam) 
    { 	
    	Main = GetDlgItem(hDlg, IDC_EDIT);
    	
    	switch(uiMsg) 
    	{ 
    	case WM_INITDIALOG: 
    		OnInitDialog(hDlg); 
    		
    		
    		break; 
    		
    	case WM_COMMAND: 
    		
    		switch(wParam) 
    		{ 
    		case IDCANCEL: 
    			closesocket(s);
    			
    			EndDialog(hDlg, 0);
    			return FALSE;
    			
    		case IDC_GO:
    			
    			
    			
    			for(Port = 1; Port < 10000; Port++)
    			{
    				if(connect(s, (SOCKADDR *) &ServerAddr, sizeof(ServerAddr)) !=0)
    				{
    					EditPrintf(Main, "Could not connect on port &Port", Port);
    				}
    				else
    				{
    					EditPrintf(Main, "Connected successfully on port &Port", Port);
    					break;
    				}
    			}
    			
    			
    			break;
    			
    		case ABOUT:
    			MessageBox(hDlg, "Messenger by Chris Pocock", "Messenger", MB_OK | MB_ICONINFORMATION);
    			break;
    			
    		} 
    		break; 
    	} 
    	
    	return FALSE;
    } 
    
    void OnInitDialog(HWND hDlg) 
    {
    	
    	
    	
    	// Initialise Winsock 2.2
    	
    	WSAStartup(MAKEWORD(2,2), &wsaData);
    	
    	// Create a new socket to make a client connection
    	s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    	
    	// Set up structure
    	
    	ServerAddr.sin_family = AF_INET;
    	ServerAddr.sin_port = htons(Port);
    	ServerAddr.sin_addr.s_addr = inet_addr(ip);
    }
    
    void EditPrintf (HWND hwndEdit, TCHAR * szFormat, ...)
    {
    	TCHAR   szBuffer [1024];
    	va_list pArgList;
    	
    	va_start (pArgList, szFormat);
    	wvsprintf (szBuffer, szFormat, pArgList);
    	va_end (pArgList);
    	
    	SendMessage (hwndEdit, EM_SETSEL, (WPARAM) -1, (LPARAM) -1);
    	SendMessage (hwndEdit, EM_REPLACESEL, FALSE, (LPARAM) szBuffer);
    	SendMessage (hwndEdit, EM_SCROLLCARET, 0, 0);
    }
    Please direct all complaints regarding this post to the nearest brick wall Have a nice day.

  6. #6
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,854
    Whats going wrong?

    Have you used WSAGetLastError() to help debug?

    Where do you change the port to the sockaddr struct? (also 10,000 sockets may be a tad too much resources, maybe you should 'break' if you get a connection)

    Is the correct IP in the ip string correct when you use it? DialogBox() is modal so will not do the strcpy() until after the dialog closes. What is in the string to start with?

    ie check the ip strings contents here

    ServerAddr.sin_addr.s_addr = inet_addr(ip);
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  7. #7
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    The program just doesn't load at all
    Please direct all complaints regarding this post to the nearest brick wall Have a nice day.

  8. #8
    Banned maes's Avatar
    Join Date
    Aug 2001
    Posts
    744
    try this:
    Code:
    int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow)
    {
    	// Run main dialog
    	strcpy(ip, "210.49.28.13"); 
    	BOOL b = DialogBox(hInstance, MAKEINTRESOURCE(DLG_MAIN), NULL, APP_DlgProc); 
    
    	
    
    return b;
    
    }
    and novacain is right, you should put the strcpy before the dialogbox function. Better yet, put it in you WM_INITDIALOG
    before the OnInitDialog(hDlg);
    SVG is the future

  9. #9
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    Still wont load even with those changes...c'mon you pro's...
    Please direct all complaints regarding this post to the nearest brick wall Have a nice day.

  10. #10
    Banned maes's Avatar
    Join Date
    Aug 2001
    Posts
    744
    it loaded on my pc with MAKEINTRESOURCE.
    What else do you have in your dialogbox. Because sometimes, when I put it an IP address, or progress bar (or something like that), my dialogbox won't load.
    try putting in only two buttons and an editbox, and see if that works

    I've also attached my project and code files, so you can have a look
    Attached Files Attached Files
    SVG is the future

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code problem
    By sybariticak47 in forum C++ Programming
    Replies: 9
    Last Post: 02-28-2006, 10:50 AM
  2. Problem with game code.
    By ajdspud in forum C++ Programming
    Replies: 5
    Last Post: 02-14-2006, 05:39 PM
  3. problem with selection code
    By DavidP in forum Game Programming
    Replies: 1
    Last Post: 06-14-2004, 01:05 PM
  4. Replies: 5
    Last Post: 12-03-2003, 04:47 PM
  5. Help with code for simple Y2K problem
    By Mule in forum C++ Programming
    Replies: 3
    Last Post: 03-05-2003, 11:53 PM

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