The problem I encountered is that I defined a string field and string property 'hostInfo' in Form1,when I test,it turned out that the property 'hostInfo' actually did not get its data.Pelase refer to below code.
char* GetHostInfo() read from the OS and get the host_name/host_address ,then combine them into a String.When I build the hostInfo property,I use this GetHostInfo() function to get the host information and build the hostInfo's getter.The issue is that in the client code,using GetHostInfo to show the host information is workable but it isn't if using hostInfo.It would be gratefulf if you can figure me out what I did wrong.
EDIT:just noticed that in (w)sprintf(result,"bla\tbla\t",...) the '\t' does not make sense in GUI.Is this normal?
Code: Select all
#ifdef __WIN32__
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <shellapi.h>
#include <winsock.h>
#endif
#undef MessageBox
import "ecere"
class Form1 : Window
{
text = "Form1";
background = activeBorder;
borderStyle = fixed;
hasMinimize = true;
hasClose = true;
tabCycle = true;
size = { 344, 112 };
anchor = { horz = -143, vert = -205 };
showInTaskBar = true;
nativeDecorations = true;
char hostInfo[1024];
public:
property char* hostInfo
{
get
{
strcpy(hostInfo,GetHostInfo());
//hostInfo=CopyString(GetHostInfo());
return hostInfo;
}
}
Button btnHostInfo
{
this, text = "(&H)显示主机信息", altH, foreground = darkSlateGray, font = { "黑体", 10, bold = true, underline = true }, size = { 258, 29 }, position = { 32, 24 };
bool NotifyClicked(Button button, int x, int y, Modifiers mods)
{
MessageBox {text="主机信息",contents=hostInfo}.Modal(); //hostInfo does not work,GetHostInfo() works!!
return true;
}
};
char* GetHostInfo()
{
int WSA_return;
WSADATA WSAData;
HOSTENT *host_entry;
char host_name[256];
char host_address[256];
char result[1024];
WSA_return=WSAStartup(0x0101,&WSAData);
if(WSA_return==0)
{
gethostname(host_name,256);
host_entry=gethostbyname(host_name);
if(host_entry!=null)
{
wsprintf(host_address,"%d.%d.%d.%d",
(host_entry->h_addr_list[0][0]&0x00ff),
(host_entry->h_addr_list[0][1]&0x00ff),
(host_entry->h_addr_list[0][2]&0x00ff),
(host_entry->h_addr_list[0][3]&0x00ff));
}
}
WSACleanup();
sprintf(result,"Host Name:%s\nHost Address:%s",host_name,host_address);
return result;
}
}
Form1 form1 {};