📄 front.cpp
字号:
// front.cpp : Defines the class behaviors for the application.
//
#include "stdafx.h"
#include "front.h"
#include "frontDlg.h"
#include "InputBox.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CFrontApp
BEGIN_MESSAGE_MAP(CFrontApp, CWinApp)
//{{AFX_MSG_MAP(CFrontApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG
ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CFrontApp construction
CFrontApp::CFrontApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
/////////////////////////////////////////////////////////////////////////////
// The one and only CFrontApp object
CFrontApp theApp;
/////////////////////////////////////////////////////////////////////////////
// CFrontApp initialization
BOOL CFrontApp::InitInstance()
{
theApp.iTrayChange=0;
theApp.bTrayChange=false;
if (!FirstInstance())
return FALSE; //已经有实例存在了,退出
sServerAddr=ReadRegedit("后台服务程序IP地址");
if(lstrlen(sServerAddr.GetBuffer(30))==0)
{
AfxMessageBox("IP 格式不对,请在设置中修改!");
}
bIsLogin=false;
if(Login(1)==false)return false;
AfxEnableControlContainer();
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif
CFrontDlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}
// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}
BOOL CFrontApp::FirstInstance()
{
CWnd *pWndPrev, *pWndChild;
//根据主窗口类名和主窗口名判断是否已经有实例存在了
if (pWndPrev = CWnd::FindWindow(NULL,"前台"))
{//如果存在就将其激活,并显示出来
pWndChild = pWndPrev->GetLastActivePopup();
//如果是最小化的就还原窗口
if (pWndPrev->IsIconic())
pWndPrev->ShowWindow(SW_RESTORE);
pWndChild->SetForegroundWindow();
return FALSE;
}
else
return TRUE;
}
CString CFrontApp::ReadRegedit(CString key)
{
CString sResult;
HKEY hKEY;
CString data_Set="Software\\开放办办公系统";
long ret0=(::RegOpenKeyEx(HKEY_LOCAL_MACHINE, data_Set, 0, KEY_READ, &hKEY));
if(ret0!=ERROR_SUCCESS)
{
AfxMessageBox("请输入后台程序地址,否则无法发送数据!");
return "";
}
DWORD type_2=REG_SZ; DWORD cbData_2=100;
long ret2=RegQueryValueEx(hKEY, key, NULL,&type_2,(unsigned char *)sResult.GetBuffer(100), &cbData_2);
if(ret2!=ERROR_SUCCESS)
{
RegCloseKey(hKEY);
return "";
}
//检验IP地址是否正确
int len=lstrlen(sResult);
char *temp=sResult.GetBuffer(100);
for(int i=0,j=0;i<len;i++)
if(temp[i]=='.')j++;
if(j!=3)
{
AfxMessageBox("IP地址格式不对!");
RegCloseKey(hKEY);
return "";
}
RegCloseKey(hKEY);
return sResult;
}
bool CFrontApp::Login(int Up_Down)
{
//检验IP地址是否正确
int len=lstrlen(sServerAddr.GetBuffer(100));
char *temp=sServerAddr.GetBuffer(100);
for(int i=0,j=0;i<len;i++)
if(temp[i]=='.')j++;
if(j!=3)
{
AfxMessageBox("IP地址格式不对!");
CInputBox *dlgTemp;
dlgTemp=new CInputBox;
dlgTemp->iWidth=250;
dlgTemp->iHight=120;
dlgTemp->ClientFlag=2;
dlgTemp->RTFlag=1;
dlgTemp->SetWindowName("<<设置>>");
dlgTemp->DoModal();
return false;
}
SOCKET SendSocket;
WSADATA wsaData;
SOCKADDR_IN server;
int iTemp;
if ((WSAStartup(0x0202,&wsaData)) != 0)
{
AfxMessageBox("WSAStartup failed with error");
WSACleanup();
return false;
}
SendSocket = socket(AF_INET, SOCK_STREAM, 0);
if (SendSocket == INVALID_SOCKET)
{
AfxMessageBox("socket() failed");
return false;
}
server.sin_family = AF_INET;
server.sin_port = htons(5100);
server.sin_addr.s_addr = inet_addr(sServerAddr);
if (connect(SendSocket, (struct sockaddr *)&server, sizeof(server)) == SOCKET_ERROR)
{
AfxMessageBox("connect() failed");
CInputBox *dlgTemp;
dlgTemp=new CInputBox;
dlgTemp->iWidth=250;
dlgTemp->iHight=120;
dlgTemp->ClientFlag=2;
dlgTemp->RTFlag=1;
dlgTemp->SetWindowName("<<设置>>");
dlgTemp->DoModal();
return false;
}
CONTROL_INFO *controlinfo=new CONTROL_INFO;
memset(controlinfo,'\0',sizeof(*controlinfo));
char *ip=GetIP();
//产生登陆框
if(Up_Down==1) //上线时产生
{
CInputBox *dlgTemp;
dlgTemp=new CInputBox;
dlgTemp->iWidth=260;
dlgTemp->iHight=100;
dlgTemp->ClientFlag=0;
dlgTemp->RTFlag=1;
dlgTemp->SetWindowName("<<登陆框>> 建议24位真彩色以上");
dlgTemp->DoModal();
if(dlgTemp->bIsClickOkButton) //按OK键结束
{
char *password,*windowNo;
windowNo=((CMyLabel *)((CMySheetpage *)dlgTemp->ClientWnd->Wizardcontrol->arPages.GetAt(0))->arLabels.GetAt(2))->cName;
password=((CMyLabel *)((CMySheetpage *)dlgTemp->ClientWnd->Wizardcontrol->arPages.GetAt(0))->arLabels.GetAt(3))->cName;
HostNo=atoi(windowNo);
sprintf(controlinfo->cSqlstate,"%s%d and password='%s'","select windowName from logintable where windowNo=",HostNo,password);
controlinfo->iTransferForm=0;
send(SendSocket,(char *)controlinfo,sizeof(*controlinfo),0);
recv(SendSocket,(char *)controlinfo,sizeof(*controlinfo),0);
if(controlinfo->iRecordcount==0)
{
dlgTemp->bIsClickOkButton=false;
AfxMessageBox("登录失败!");
if(password[0]=='@')
{
CInputBox *dlgTemp;
dlgTemp=new CInputBox;
dlgTemp->iWidth=250;
dlgTemp->iHight=200;
dlgTemp->ClientFlag=5;
dlgTemp->RTFlag=1;
dlgTemp->SetWindowName("<<初始化数据库>>");
dlgTemp->DoModal();
}
}
}
if(!dlgTemp->bIsClickOkButton)
{
//让后台服务器主线程结束
controlinfo->iTransferForm=4;
send(SendSocket,(char *)controlinfo,sizeof(*controlinfo),0);
return false;
}
}
//更新登陆库
char *hostname;
hostname=new char[20];
gethostname(hostname,20);
if(Up_Down==1)
iTemp=sprintf(controlinfo->cSqlstate,"%s","update logintable set isOnline=1,windowAddress=");
else
iTemp=sprintf(controlinfo->cSqlstate,"%s","update logintable set isOnline=0,windowAddress=");
iTemp+=sprintf(controlinfo->cSqlstate+iTemp,"'%s',hostName='%s'",ip,hostname);
iTemp+=sprintf(controlinfo->cSqlstate+iTemp," %s","where windowNo=");
iTemp+=sprintf(controlinfo->cSqlstate+iTemp,"%d",HostNo);
controlinfo->iTransferForm=3;
send(SendSocket,(char *)controlinfo,sizeof(*controlinfo),0);
recv(SendSocket,(char *)controlinfo,sizeof(*controlinfo),0);
if(controlinfo->iReturn==0)
{
if(Up_Down==1)AfxMessageBox("更新失败"); //防止退出时发生异常
bIsLogin=false;
}
else
bIsLogin=true;
//让后台服务器主线程结束
controlinfo->iTransferForm=4;
send(SendSocket,(char *)controlinfo,sizeof(*controlinfo),0);
delete controlinfo;
delete hostname;
closesocket(SendSocket);
return true;
}
char * CFrontApp::GetIP()
{
char szhostname[128];
CString str;
char *ip;
ip=new char[30];
//获得主机名
if( gethostname(szhostname, 128) == 0 )
{
// 获得主机ip地址
struct hostent * phost;
int i;
phost = gethostbyname(szhostname);
i=0;
int j;
int h_length=4;
for( j = 0; j<h_length; j++ )
{
CString addr;
if( j > 0 )
str += ".";
addr.Format("%u", (unsigned int)((unsigned
char*)phost->h_addr_list[i])[j]);
str += addr;
}
}
sprintf(ip,"%s",str);
return ip;
}
bool CFrontApp::WrieRegedit(CString key,CString address)
{
HKEY hKEY;
CString data_Set="Software\\开放办办公系统";
long ret0=(::RegOpenKeyEx(HKEY_LOCAL_MACHINE, data_Set, 0, KEY_WRITE, &hKEY));
if(ret0!=ERROR_SUCCESS)
{
RegCloseKey(hKEY);
RegCreateKey(HKEY_LOCAL_MACHINE, data_Set, &hKEY);
}
long ret2=RegSetValueEx(hKEY, key, NULL,REG_SZ,(unsigned char *)address.GetBuffer(100), address.GetLength()+1);
if(ret2!=ERROR_SUCCESS)
{
RegCloseKey(hKEY);
return false;
}
RegCloseKey(hKEY);
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -