![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 | |
|
Professional Programmer
|
Winapi get textbox
I'm currently trying to merge my socket program, with a gui i just created.. it's basically to help me understand more how each of the project types would work.. Everything thing was going fine, until.. the dreadful ip
. I need to figure out how to grab data that the user inputs to the textbox. I need to use this for the ip, the port and sending messages but the major thing rihgt now is just getting the client to connect. All i need is the "grab data from textbox function". Thanks.EDIT: Ugh, the ip from the text box has to be converted into a char too :mad:
__________________
▄▄▄▄ Quote:
Due to incorrect calculations during the middle ages, our calendar actually begins a few years after Jesus' birth. Thus the real 6/6/6 happened a few years back. The world already ended and you missed it. Download Code::Blocks now! ▄▄▄▄ Last edited by jayme; Jan 16th, 2006 at 1:00 AM. |
|
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: Sep 2005
Location: serbia & montenegro
Posts: 484
Rep Power: 4
![]() |
UINT GetDlgItemText(
HWND hDlg, // handle of dialog box
int nIDDlgItem, // identifier of control
LPTSTR lpString, // address of buffer for text
int nMaxCount // maximum size of string
);That should work fine. And one more thing, you don't need to convert th ip from the text box to a char array, this function does it automatically for you. Later, you may want to convert the array to int, or ip address struct. |
|
|
|
|
|
#3 | |
|
Professional Programmer
|
this is so aggrivating, I've tried so many different combinations, so many different methods. Nothing seems to be working.
char* buf;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case REMOTEADDRESS1:
return TRUE;
break;
}
case MY_CONNECTBUTTON:
GetDlgItemText(
hwndhello,
REMOTEADDRESS1,
buf,
16
);
remote_address.sin_family = AF_INET; /* Internet address family */
remote_address.sin_addr.s_addr = inet_addr(buf); /* Server IP address */
remote_address.sin_port = htons(remote_port); /* Server port */
if (connect(remote_socket, (struct sockaddr *) &remote_address, sizeof(remote_address)) < 0)
{
std::cout << "connect() failed\n";
break; }I don't know what else to try.. sometimes it will seem like its working fine, no errors or anything. But then i will add std::cout buf; to the code just to see if it actually grabs the text, but nothing is outputed to the console.
__________________
▄▄▄▄ Quote:
Due to incorrect calculations during the middle ages, our calendar actually begins a few years after Jesus' birth. Thus the real 6/6/6 happened a few years back. The world already ended and you missed it. Download Code::Blocks now! ▄▄▄▄ |
|
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Could you post the whole code?
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
#5 |
|
Hobbyist
Join Date: Sep 2005
Posts: 261
Rep Power: 4
![]() |
This is how I did it:
First define and include: #define _WIN32_IE 0x0500 // To support INITCOMMONCONTROLSEX #include <commctrl.h> // To include common controls Initialise common controls: // Initialise common contols INITCOMMONCONTROLSEX InitCtrlEx; InitCtrlEx.dwSize = sizeof(INITCOMMONCONTROLSEX); InitCtrlEx.dwICC = ICC_INTERNET_CLASSES; InitCommonControlsEx(&InitCtrlEx); Creat an IP control (change as you like): // IP control CreateWindow( WC_IPADDRESS, // predefined class "", // text WS_VISIBLE | WS_CHILD, // styles 105, // starting x position 20, // starting y position 140, // button width 20, // button height m_hWnd, // parent window (HMENU)IDI_IPBOX, // ID (HINSTANCE) GetWindowLong(m_hWnd, GWL_HINSTANCE), // App instance NULL); // pointer not needed To grab the IP address from the control: // Grab the IP
int len = GetWindowTextLength(GetDlgItem(m_hWnd, IDI_IPBOX));
if (len > 0)
{
char *buffer;
buffer = (char*)GlobalAlloc(GPTR, len + 1);
GetDlgItemText(m_hWnd, IDI_IPBOX, buffer, len + 1);
}
else
{
// Error handling here
}And don't forget to free the memory when your done. If your using Visual Studio link to: comctl32.lib If your using Dev-C++ link to: libcomctl32.a Last edited by Cache; Jan 16th, 2006 at 1:42 PM. |
|
|
|
|
|
#6 | ||
|
Professional Programmer
|
Quote:
I know it's really messy, should still be readable though.. im still having the same problems as before when trying your method cache, so i im pretty sure im just doing something wrong. #include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <winsock2.h>
#include <iostream>
#include <fstream>
#define _WIN32_IE 0x0500
#include <commctrl.h>
#define MAX 500
#define ENTRY 31
#define MAXPENDING 5
#define BUFFER_SIZE 256
#define EXIT_CALL " exit"
#define CONNECTTO 99
#define PORT 100
#define REMOTEADDRESS1 101
#define MY_SENDBUTTON 301
#define MY_CONNECTBUTTON 300
void wait ( int seconds )
{
clock_t endwait;
endwait = clock () + seconds * CLK_TCK ;
while (clock() < endwait) {}
}
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
char szClassName[ ] = "WindowsApp";
HWND hwndhello;
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
int local_socket = 0, remote_socket = 0, menu_switch = 0, message_length = 0, remote_length = 0;
unsigned short local_port = 0, remote_port = 0;
struct sockaddr_in local_address, remote_address;
WSADATA wsa_data;
char message[BUFFER_SIZE], remote_ip[32];
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
INITCOMMONCONTROLSEX InitCtrlEx;
InitCtrlEx.dwSize = sizeof(INITCOMMONCONTROLSEX);
InitCtrlEx.dwICC = ICC_INTERNET_CLASSES;
InitCommonControlsEx(&InitCtrlEx);
local_port = 7447;
if (WSAStartup(MAKEWORD(2, 0), &wsa_data) != 0)
{
std::cout << "WSAStartup() failed\n";
return (1);
}
remote_port = 51;
fflush(stdin);
if ((remote_socket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
{
std::cout << "socket() failed\n";
return(1);
}
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure;
wincl.style = CS_DBLCLKS;
wincl.cbSize = sizeof (WNDCLASSEX);
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL;
wincl.cbClsExtra = 0;
wincl.cbWndExtra = 0;
wincl.hbrBackground = (HBRUSH) (COLOR_WINDOW+18);
if( !RegisterClassEx( &wincl ) )
{
MessageBox( hwnd, "Can't register window class!", "Error", MB_OK | MB_ICONEXCLAMATION );
return 0;
}
hwnd = CreateWindowEx (
0,
szClassName,
"Test App",
WS_OVERLAPPED | WS_VISIBLE | WS_SYSMENU,
CW_USEDEFAULT,
CW_USEDEFAULT,
400,
240,
HWND_DESKTOP,
NULL,
hThisInstance,
NULL
);
ShowWindow (hwnd, nFunsterStil);
while (GetMessage (&messages, NULL, 0, 0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return messages.wParam;
}
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
char szText[BUFFER_SIZE];
switch (message)
{
case WM_CREATE:
{
hwndhello= CreateWindowEx (
0,
"button",
"Connect",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON ,
312,
165,
66,
26,
hwnd,
(HMENU) MY_CONNECTBUTTON,
GetModuleHandle( NULL ),
NULL
);
/* Send button */
hwndhello= CreateWindowEx (
0,
"Button",
"Send",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | ES_PASSWORD,
235,
165,
66,
26,
hwnd,
(HMENU) MY_SENDBUTTON,
GetModuleHandle( NULL ),
NULL
);
/* Message send box */
hwndhello= CreateWindowEx (
WS_EX_WINDOWEDGE,
"edit",
NULL,
WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL | WS_BORDER | ES_WANTRETURN,
30,
169,
200,
22,
hwnd,
NULL,
GetModuleHandle( NULL ),
NULL
);
/* Message recieve box */
hwndhello= CreateWindowEx (
WS_EX_WINDOWEDGE,
"edit",
NULL,
WS_CHILD | WS_VISIBLE | ES_READONLY | WS_BORDER,
30,
12,
200,
150,
hwnd,
NULL,
GetModuleHandle( NULL ),
NULL
);
/* Message recieve box */
hwndhello= CreateWindowEx (
WS_EX_WINDOWEDGE,
"edit",
NULL,
WS_CHILD | WS_VISIBLE | ES_READONLY | WS_BORDER,
30,
32,
200,
130,
hwnd,
NULL,
GetModuleHandle( NULL ),
NULL
);
/* Port address */
hwndhello= CreateWindowEx (
WS_EX_WINDOWEDGE,
"edit",
"51",
WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL | WS_BORDER | ES_NUMBER | ES_RIGHT,
334,
100,
52,
22,
hwnd,
(HMENU) PORT,
GetModuleHandle( NULL ),
NULL
);
CreateWindow(
WC_IPADDRESS, // predefined class
"", // text
WS_VISIBLE | WS_CHILD, // styles
270,
50,
116,
22, // button height
hwnd, // parent window
(HMENU)REMOTEADDRESS1, // ID
(HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE), // App instance
NULL); // po
/* Port address */
hwndhello= CreateWindowEx (
WS_EX_WINDOWEDGE,
"static",
"Port Number",
WS_CHILD | WS_VISIBLE,
304,
79,
85,
18,
hwnd,
NULL,
GetModuleHandle( NULL ),
NULL
);
/* Ip address */
hwndhello= CreateWindowEx (
WS_EX_WINDOWEDGE,
"static",
"IP Address",
WS_CHILD | WS_VISIBLE,
314,
31,
74,
18,
hwnd,
NULL,
GetModuleHandle( NULL ),
NULL
);
/* Connect to.. */
hwndhello= CreateWindowEx (
WS_EX_WINDOWEDGE,
"static",
"Connected to..",
WS_CHILD | WS_VISIBLE,
32,
14,
197,
18,
hwnd,
(HMENU) CONNECTTO,
GetModuleHandle( NULL ),
NULL
);
;}
case WM_COMMAND:
if( HIWORD( wParam ) == BN_CLICKED )
{
switch(LOWORD(wParam))
{
case REMOTEADDRESS1:
if( GetWindowTextLength(GetDlgItem(hwnd, REMOTEADDRESS1)) > 15){
MessageBox( hwnd, "You must enter a port number now!", "Error", MB_OK | MB_ICONEXCLAMATION );
}
return TRUE;
break;
}
case MY_CONNECTBUTTON:
if( GetWindowTextLength(GetDlgItem(hwnd, REMOTEADDRESS1)) == 8 ||
GetWindowTextLength(GetDlgItem(hwnd, PORT)) < 2)
{
MessageBox( hwnd, "Please fill all the fields!", "Error", MB_OK | MB_ICONEXCLAMATION );
return 0;
}
remote_address.sin_family = AF_INET; /* Internet address family */
remote_address.sin_addr.s_addr = inet_addr("127.0.0.1"); /* Server IP address */
remote_address.sin_port = htons(remote_port); /* Server port */
if (connect(remote_socket, (struct sockaddr *) &remote_address, sizeof(remote_address)) < 0)
{
std::cout << "connect() failed\n";
break;
}
default:
return DefWindowProc (hwnd, message, wParam, lParam);
case WM_DESTROY:
PostQuitMessage (0);
break;
}
}
}
__________________
▄▄▄▄ Quote:
Due to incorrect calculations during the middle ages, our calendar actually begins a few years after Jesus' birth. Thus the real 6/6/6 happened a few years back. The world already ended and you missed it. Download Code::Blocks now! ▄▄▄▄ |
||
|
|
|
|
|
#7 |
|
Hobbyist
Join Date: Sep 2005
Posts: 261
Rep Power: 4
![]() |
I was able to connect to my server with the port and IP hard coded using the code below:
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <winsock2.h>
#include <iostream>
#include <fstream>
#define _WIN32_IE 0x0500
#include <commctrl.h>
#define MAX 500
#define ENTRY 31
#define MAXPENDING 5
#define BUFFER_SIZE 256
#define EXIT_CALL " exit"
#define CONNECTTO 99
#define PORT 100
#define REMOTEADDRESS1 101
#define MY_SENDBUTTON 301
#define MY_CONNECTBUTTON 300
void wait ( int seconds )
{
clock_t endwait;
endwait = clock () + seconds * CLK_TCK ;
while (clock() < endwait) {}
}
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
char szClassName[ ] = "WindowsApp";
HWND hwndhello;
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
int local_socket = 0, remote_socket = 0, menu_switch = 0, message_length = 0, remote_length = 0;
unsigned short local_port = 0, remote_port = 0;
sockaddr_in local_address, remote_address;
WSADATA wsa_data;
char message[BUFFER_SIZE], remote_ip[32];
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
INITCOMMONCONTROLSEX InitCtrlEx;
InitCtrlEx.dwSize = sizeof(INITCOMMONCONTROLSEX);
InitCtrlEx.dwICC = ICC_INTERNET_CLASSES;
InitCommonControlsEx(&InitCtrlEx);
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure;
wincl.style = CS_DBLCLKS;
wincl.cbSize = sizeof (WNDCLASSEX);
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL;
wincl.cbClsExtra = 0;
wincl.cbWndExtra = 0;
wincl.hbrBackground = (HBRUSH) (COLOR_WINDOW+18);
if( !RegisterClassEx( &wincl ) )
{
MessageBox(hwnd,
"Can't register window class!",
"Error",
MB_OK | MB_ICONEXCLAMATION );
return 0;
}
hwnd = CreateWindowEx (
0,
szClassName,
"Test App",
WS_OVERLAPPED | WS_VISIBLE | WS_SYSMENU,
CW_USEDEFAULT,
CW_USEDEFAULT,
400,
240,
HWND_DESKTOP,
NULL,
hThisInstance,
NULL);
ShowWindow (hwnd, nFunsterStil);
local_port = 7447;
if (WSAStartup(MAKEWORD(2, 0), &wsa_data) != NO_ERROR)
{
MessageBox(hwnd,
"WSAStartup() failed",
"Error",
MB_OK | MB_ICONWARNING |MB_SETFOREGROUND);
return (1);
}
remote_port = 51;
if ((remote_socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP )) == INVALID_SOCKET)
{
MessageBox(
hwnd,
"Error at socket()",
"Error",
MB_OK | MB_ICONWARNING |MB_SETFOREGROUND);
return(1);
}
while (GetMessage (&messages, NULL, 0, 0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return messages.wParam;
}
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
char szText[BUFFER_SIZE];
switch (message)
{
case WM_CREATE:
{
hwndhello= CreateWindowEx (
0,
"button",
"Connect",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON ,
312,
165,
66,
26,
hwnd,
(HMENU) MY_CONNECTBUTTON,
GetModuleHandle( NULL ),
NULL
);
/* Send button */
hwndhello= CreateWindowEx (
0,
"Button",
"Send",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | ES_PASSWORD,
235,
165,
66,
26,
hwnd,
(HMENU) MY_SENDBUTTON,
GetModuleHandle( NULL ),
NULL
);
/* Message send box */
hwndhello= CreateWindowEx (
WS_EX_WINDOWEDGE,
"edit",
NULL,
WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL | WS_BORDER | ES_WANTRETURN,
30,
169,
200,
22,
hwnd,
NULL,
GetModuleHandle( NULL ),
NULL
);
/* Message recieve box */
hwndhello= CreateWindowEx (
WS_EX_WINDOWEDGE,
"edit",
NULL,
WS_CHILD | WS_VISIBLE | ES_READONLY | WS_BORDER,
30,
12,
200,
150,
hwnd,
NULL,
GetModuleHandle( NULL ),
NULL
);
/* Message recieve box */
hwndhello= CreateWindowEx (
WS_EX_WINDOWEDGE,
"edit",
NULL,
WS_CHILD | WS_VISIBLE | ES_READONLY | WS_BORDER,
30,
32,
200,
130,
hwnd,
NULL,
GetModuleHandle( NULL ),
NULL
);
/* Port address */
hwndhello= CreateWindowEx (
WS_EX_WINDOWEDGE,
"edit",
"51",
WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL | WS_BORDER | ES_NUMBER | ES_RIGHT,
334,
100,
52,
22,
hwnd,
(HMENU) PORT,
GetModuleHandle( NULL ),
NULL
);
CreateWindow(
WC_IPADDRESS, // predefined class
"", // text
WS_VISIBLE | WS_CHILD, // styles
270,
50,
116,
22, // button height
hwnd, // parent window
(HMENU)REMOTEADDRESS1, // ID
(HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE), // App instance
NULL); // po
/* Port address */
hwndhello= CreateWindowEx (
WS_EX_WINDOWEDGE,
"static",
"Port Number",
WS_CHILD | WS_VISIBLE,
304,
79,
85,
18,
hwnd,
NULL,
GetModuleHandle( NULL ),
NULL
);
/* Ip address */
hwndhello= CreateWindowEx (
WS_EX_WINDOWEDGE,
"static",
"IP Address",
WS_CHILD | WS_VISIBLE,
314,
31,
74,
18,
hwnd,
NULL,
GetModuleHandle( NULL ),
NULL
);
/* Connect to.. */
hwndhello= CreateWindowEx (
WS_EX_WINDOWEDGE,
"static",
"Connected to..",
WS_CHILD | WS_VISIBLE,
32,
14,
197,
18,
hwnd,
(HMENU) CONNECTTO,
GetModuleHandle( NULL ),
NULL
);
}
case WM_COMMAND:
{
switch(LOWORD(wParam))
{
case REMOTEADDRESS1:
{
if( GetWindowTextLength(GetDlgItem(hwnd, REMOTEADDRESS1)) > 15)
{
MessageBox(hwnd,
"You must enter a port number now!",
"Error",
MB_OK | MB_ICONEXCLAMATION);
}
return TRUE;
}
break;
case MY_CONNECTBUTTON:
{
if( GetWindowTextLength(GetDlgItem(hwnd, REMOTEADDRESS1)) == 8 ||
GetWindowTextLength(GetDlgItem(hwnd, PORT)) < 2)
{
MessageBox(hwnd,
"Please fill all the fields!",
"Error",
MB_OK | MB_ICONEXCLAMATION);
return 0;
}
remote_address.sin_family = AF_INET; /* Internet address family */
remote_address.sin_addr.s_addr = inet_addr("127.0.0.1"); /* Server IP address */
remote_address.sin_port = htons(remote_port); /* Server port */
if (connect(remote_socket, (struct sockaddr *) &remote_address,
sizeof(remote_address)) == SOCKET_ERROR)
{
MessageBox(hwnd,
"connect() failed",
"Error",
MB_OK | MB_ICONEXCLAMATION);
break;
}
}
}
}
break;
case WM_DESTROY:
PostQuitMessage (0);
break;
default:
return DefWindowProc (hwnd, message, wParam, lParam);
}
}Of course all it did was connect, since you haven't implemented a way to grab information from the controls yet. And no, thats not what my indentation looked like in my compiler..lol You should be able to see the actual code changes I've made since this is your code (I hope). I was fiddlin' around with it alot so I can't remember exactly what I changed. EDIT: Although I said that the code I used (it was), I changed the port number from what is shown in that code i.e 'remote_port = 0' was changed. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|