-
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);
}
-
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!!!!
-
where should I put all that code, then?
-
If you need the socket for the dialog, I would put it in OnInitDialog(). This way it should only be run once.
-
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);
}
-
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);
-
The program just doesn't load at all
-
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);
-
Still wont load even with those changes...c'mon you pro's...
-
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