main.cpp
上传用户:romrleung
上传日期:2022-05-23
资源大小:18897k
文件大小:68k
- //---------------------------------------------------------------------------
- #include <vcl.h>
- #pragma hdrstop
- #include "main.h"
- #include "initsetup.h"
- #include "db.h"
- //---------------------------------------------------------------------------
- #pragma package(smart_init)
- #pragma resource "*.dfm"
- #include <shellapi.h>
- #include <registry.hpp>
- #include <winsvc.h>
- #include <winsock.h>
- #include <shlobj.h>
- #include <IniFiles.hpp>
- #include <dir.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <deque.h>
- #include <vector.h>
- #include <fstream.h>
- #include <iostream.h>
- #include <iterator.h>
- #include <sstream.h>
- #include "mysql.h"
- #include <Printers.hpp>
- TForm1 *Form1;
- bool i_start, NT;
- bool IsForce = false;
- bool IsVariables = false;
- bool IsProcess = false ;
- bool IsDatabases = false;
- bool new_line = 0;
- bool ya = true;
- bool yy = true;
- bool rinit = false;
- AnsiString vpath;
- AnsiString vip;
- MYSQL_RES *res_1;
- static unsigned long q = 0;
- bool preport = false;
- bool treport = false;
- bool ereport = false;
- AnsiString mainroot;
- bool IsMySQLNode = false;
- MYSQL *MySQL;
- //---------------------------------------------------------------------------
- __fastcall TForm1::TForm1(TComponent* Owner)
- : TForm(Owner)
- {
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::FormCreate(TObject *Sender)
- {
- i_start = true;
- IsConnect = false;
- if (ParamCount() > 0){
- if (ParamStr(1) == "-h" || ParamStr(1) == "h" ) {
- ShowHelp(); Application->Terminate(); }
- else if (ParamStr(1) == "-w" || ParamStr(1) == "w") {
- i_start = false; ContinueLoad(); }
- }
- else {
- ContinueLoad(); Hide(); GetServerOptions(); }
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::DrawItem(TMessage& Msg)
- {
- IconDrawItem((LPDRAWITEMSTRUCT)Msg.LParam);
- TForm::Dispatch(&Msg);
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::MyNotify(TMessage& Msg)
- {
- POINT MousePos;
- switch(Msg.LParam) {
- case WM_RBUTTONUP:
- if (GetCursorPos(&MousePos)){
- PopupMenu1->PopupComponent = Form1; SetForegroundWindow(Handle);
- PopupMenu1->Popup(MousePos.x, MousePos.y);}
- else Show();
- break;
- case WM_LBUTTONUP:
- if (GetCursorPos(&MousePos)){
- PopupMenu1->PopupComponent = Form1; SetForegroundWindow(Handle);
- PopupMenu1->Popup(MousePos.x, MousePos.y); }
- ToggleState();
- break;
- default:
- break; }
- TForm::Dispatch(&Msg);
- }
- //---------------------------------------------------------------------------
- bool __fastcall TForm1::TrayMessage(DWORD dwMessage)
- {
- NOTIFYICONDATA tnd;
- PSTR pszTip;
- pszTip = TipText();
- tnd.cbSize = sizeof(NOTIFYICONDATA);
- tnd.hWnd = Handle;
- tnd.uID = IDC_MYICON;
- tnd.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
- tnd.uCallbackMessage = MYWM_NOTIFY;
- if (dwMessage == NIM_MODIFY){
- tnd.hIcon = IconHandle();
- if (pszTip)lstrcpyn(tnd.szTip, pszTip, sizeof(tnd.szTip));
- else tnd.szTip[0] = ' '; }
- else { tnd.hIcon = NULL; tnd.szTip[0] = ' '; }
- return (Shell_NotifyIcon(dwMessage, &tnd));
- }
- //---------------------------------------------------------------------------
- HANDLE __fastcall TForm1::IconHandle(void)
- {
- if (!NT){
- if (MySQLSignal()){Image3->Visible = false; Image2->Visible = true;
- return (Image2->Picture->Icon->Handle); }
- else {Image2->Visible = false; Image3->Visible = true;
- return (Image3->Picture->Icon->Handle); }
- }
- else {
- if (TheServiceStatus()){Image3->Visible = false; Image2->Visible = true;
- return (Image2->Picture->Icon->Handle); }
- else if (MySQLSignal()){Image3->Visible = false; Image2->Visible = true;
- return (Image2->Picture->Icon->Handle); }
- else {Image2->Visible = false; Image3->Visible = true;
- return (Image3->Picture->Icon->Handle); }
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::ToggleState(void)
- {
- TrayMessage(NIM_MODIFY);
- if (!NT){
- if (MySQLSignal()){SSW9->Caption = "ShutDown the Server";
- Image3->Visible = false; Image2->Visible = true; }
- else {SSW9->Caption = "Start the Server";
- Image2->Visible = false; Image3->Visible = true; }
- }
- else {
- if (TheServiceStart()) {
- Standa->Enabled = false;
- if (TheServiceStatus()) {RService->Enabled = false;
- StopS->Enabled = true;
- StopS->Caption = "Stop the Service";
- Image3->Visible = false;
- Image2->Visible = true; }
- else {RService->Enabled = true;
- StopS->Enabled = true;
- RService->Caption = "Remove the Service";
- StopS->Caption = "Start the Service";
- Image2->Visible = false;
- Image3->Visible = true; }
- }
- else {
- Standa->Enabled = true;
- StopS->Enabled = false;
- if (MySQLSignal()) {
- RService->Enabled = false;
- Standa->Caption = "ShutDown the Server Standalone";
- Image3->Visible = false;
- Image2->Visible = true; }
- else {
- RService->Enabled = true;
- RService->Caption = "Install the Service";
- Standa->Caption = "Start the Server Standalone";
- Image2->Visible = false;
- Image3->Visible = true; }
- }
- }
- }
- //---------------------------------------------------------------------------
- PSTR __fastcall TForm1::TipText(void)
- {
- char* status = StatusLine->SimpleText.c_str();
- return status;
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::WMQueryEndSession(TWMQueryEndSession &msg)
- {
- if (!NT) {
- if (MySQLSignal()){
- StatusLine->SimpleText = "Shutdown in progress.....";
- Show(); Shutd(); msg.Result = 1; }
- else {
- StatusLine->SimpleText = "The Server already is down......";
- Show(); msg.Result = 1; Close(); }
- }
- else {
- Show();
- if (!TheServiceStart()) { if (MySQLSignal()) Shutd(); }
- msg.Result = 1;
- }
- }
- //---------------------------------------------------------------------------
- LRESULT IconDrawItem(LPDRAWITEMSTRUCT lpdi)
- {
- HICON hIcon;
- hIcon = (HICON)LoadImage(g_hinst, MAKEINTRESOURCE(lpdi->CtlID), IMAGE_ICON,
- 16, 16, 0);
- if (!hIcon)
- return(false);
- DrawIconEx(lpdi->hDC, lpdi->rcItem.left, lpdi->rcItem.top, hIcon,
- 16, 16, 0, NULL, DI_NORMAL);
- return(true);
- }
- //---------------------------------------------------------------------------
- AnsiString __fastcall TForm1::TheComputer()
- {
- AnsiString theword;
- DWORD dwSize = MAX_COMPUTERNAME_LENGTH + 1;
- char szBuf[MAX_COMPUTERNAME_LENGTH + 1];
- szBuf[0] = ' ';
- GetComputerName(szBuf, &dwSize);
- theword = (AnsiString) szBuf;
- delete [] szBuf;
- return theword;
- }
- //---------------------------------------------------------------------------
- AnsiString __fastcall TForm1::TheOS()
- {
- AnsiString theword;
- OSVERSIONINFO info;
- info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
- GetVersionEx(&info);
- switch (info.dwPlatformId)
- {
- case VER_PLATFORM_WIN32s:
- NT = false;
- theword = "Win32s detected";
- break;
- case VER_PLATFORM_WIN32_WINDOWS:
- NT = false;
- theword = "Win 95 or Win 98 detected";
- break;
- case VER_PLATFORM_WIN32_NT:
- NT = true;
- theword = "Windows NT detected";
- break;
- }
- return theword;
- }
- ///---------------------------------------------------------------------------
- AnsiString __fastcall TForm1::TheUser()
- {
- AnsiString theword;
- DWORD dwSize = 0;
- GetUserName(NULL, &dwSize);
- char *szBuf = new char[dwSize];
- szBuf[0] = ' ';
- GetUserName(szBuf, &dwSize);
- theword = (AnsiString) szBuf;
- delete [] szBuf;
- return theword;
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::TakeIP(void)
- {
- WORD wVersionRequested;
- WSADATA WSAData;
- wVersionRequested = MAKEWORD(1,1);
- WSAStartup(wVersionRequested,&WSAData);
- hostent *P;
- char s[128];
- in_addr in;
- char *P2;
- gethostname(s, 128);
- P = gethostbyname(s);
- Memo2->Lines->Clear();
- Memo2->Lines->Add((AnsiString)P->h_name);
- mainroot = P->h_name;
- in.S_un.S_un_b.s_b1 = P->h_addr_list[0][0];
- in.S_un.S_un_b.s_b2 = P->h_addr_list[0][1];
- in.S_un.S_un_b.s_b3 = P->h_addr_list[0][2];
- in.S_un.S_un_b.s_b4 = P->h_addr_list[0][3];
- P2 = inet_ntoa(in);
- vip = P2;
- mainroot += " ( " + (AnsiString)P2 + " )";
- Memo2->Lines->Add(P2);
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::GetmemStatus(void)
- {
- MEMORYSTATUS ms;
- ms.dwLength = sizeof(MEMORYSTATUS);
- GlobalMemoryStatus(&ms);
- Edit2->Text = AnsiString((double)ms.dwTotalPhys / 1024000.0) + " MB RAM";
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::ShowHelp(void)
- {
- Application->MessageBox("Usage: WinMySQLadmin.EXE [OPTIONS]nn-w Run the tool without start the Server.n-h Shows this message and exit ", "WinMySQLadmin 1.0", MB_OK |MB_ICONINFORMATION);
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::ContinueLoad(void)
- {
- OS->Text = TheOS();
- Localhost->Text = TheComputer();
- Localuser->Text = TheUser();
- GetmemStatus();
- ClearBox();
- TakeIP();
- MyODBC();
- IsMyIniUp();
- if (!NT) { WinNT->Enabled = false; NtVer->Enabled = false; Win9->Enabled = true; }
- else { WinNT->Enabled = true; Win9->Enabled = false; }
- if (i_start)
- {
- // NT never is started from the prompt
- if ((!NT) && (!MySQLSignal())) mysqldstart();
- {
- TrayMessage(NIM_MODIFY);
- SeekErrFile();
- }
- }
- Hide();
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::MyODBC(void)
- {
- TRegistry *Registry = new TRegistry();
- Memo3->Lines->Clear();
- try
- {
- Registry->RootKey = HKEY_LOCAL_MACHINE;
- // the basic data of myodbc
- if (Registry->OpenKey("Software\ODBC\ODBCINST.INI\MySQL", false))
- {
- Memo3->Lines->Add("Driver Versiont" + Registry->ReadString("DriverODBCVer"));
- Memo3->Lines->Add("Drivertt" + Registry->ReadString("Driver"));
- Memo3->Lines->Add("API Leveltt" + Registry->ReadString("APILevel"));
- Memo3->Lines->Add("Setuptt" + Registry->ReadString("Setup"));
- Memo3->Lines->Add("SQL Levelt" + Registry->ReadString("SQLLevel"));
- }
- else
- Memo3->Lines->Add("Not Found");
- }
- catch (...)
- {
- delete Registry;
- }
- Memo3->Enabled = false;
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::IsMyIniUp(void)
- {
- // we see if the my.ini is Up
- AnsiString asFileName = FileSearch("my.ini", TheWinDir());
- if (asFileName.IsEmpty())
- {
- IsForce = true;
- i_start = false;
- QuickSearch();
- }
- else
- {
- Memo1->Enabled = true;
- Memo1->Lines->Clear();
- FillMyIni();
- GetBaseDir();
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::QuickSearch(void)
- {
- AnsiString asFileName = FileSearch("mysql.exe", "c:/mysql/bin");
- if (!asFileName.IsEmpty())
- BaseDir->Text = "c:/mysql";
- }
- //---------------------------------------------------------------------------
- AnsiString __fastcall TForm1::TheWinDir()
- {
- AnsiString WinDir;
- UINT BufferSize = GetWindowsDirectory(NULL,0);
- WinDir.SetLength(BufferSize+1);
- GetWindowsDirectory(WinDir.c_str(),BufferSize);
- char* dirw = WinDir.c_str();
- return dirw ;
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::FillMyIni(void)
- {
- Memo1->Lines->LoadFromFile(TheWinDir() + "\my.ini");
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::GetBaseDir(void)
- {
- char drive[_MAX_DRIVE];
- char dir[_MAX_DIR];
- char file[_MAX_FNAME];
- char ext[_MAX_EXT];
- TIniFile *pIniFile = new
- TIniFile(TheWinDir() + "\my.ini");
- BaseDir->Text = pIniFile->ReadString("mysqld","basedir","") ;
- AnsiString lx = pIniFile->ReadString("WinMySQLadmin","Server","") ;
- _splitpath((lx).c_str(),drive,dir,file,ext);
- AnsiString lw = (AnsiString) file + ext;
- if ( lw == "mysqld-shareware.exe") {ShareVer->Checked = true;}
- if ( lw == "mysqld.exe") {MysqldVer->Checked = true;}
- if ( lw == "mysqld-opt.exe") {OptVer->Checked = true;}
- if ( lw == "mysqld-nt.exe") {NtVer->Checked = true;}
- delete pIniFile;
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::Showme1Click(TObject *Sender)
- {
- if(Showme1->Caption == "Show me") { TrayMessage(NIM_DELETE);
- Showme1->Caption = "Hide me"; Show(); }
- else { TrayMessage(NIM_ADD); TrayMessage(NIM_MODIFY);
- Showme1->Caption = "Show me"; Hide(); }
- }
- //---------------------------------------------------------------------------
- bool __fastcall TForm1::MySQLSignal()
- {
- HANDLE hEventShutdown;
- hEventShutdown=OpenEvent(EVENT_MODIFY_STATE, 0, "MySqlShutdown");
- if(hEventShutdown)
- {
- CloseHandle(hEventShutdown);
- return true;
- }
- else
- {
- CloseHandle(hEventShutdown);
- return false;
- }
- }
- //---------------------------------------------------------------------------
- bool __fastcall TForm1::mysqldstart()
- {
- memset(&pi, 0, sizeof(pi));
- memset(&si, 0, sizeof(si));
- si.cb = sizeof(si);
- si.dwFlags |= STARTF_USESHOWWINDOW;
- si.wShowWindow |= SW_SHOWNORMAL;
- TIniFile *pIniFile = new
- TIniFile(TheWinDir() + "\my.ini");
- if (NT)
- vpath = pIniFile->ReadString("WinMySQLadmin","Server","") + " --standalone " ;
- else
- vpath = pIniFile->ReadString("WinMySQLadmin","Server","") + "