home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 January / Chip_2001-01_cd1.bin / tema / mysql / mysql-3.23.28g-win-source.exe / winmysqladmin / main.cpp < prev    next >
C/C++ Source or Header  |  2000-07-31  |  70KB  |  2,530 lines

  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4.  
  5. #include "main.h"
  6. #include "initsetup.h"
  7. #include "db.h"
  8.  
  9. //---------------------------------------------------------------------------
  10. #pragma package(smart_init)
  11. #pragma resource "*.dfm"
  12. #include <shellapi.h>
  13. #include <registry.hpp>
  14. #include <winsvc.h>
  15. #include <winsock.h>
  16. #include <shlobj.h>
  17. #include <IniFiles.hpp>
  18. #include <dir.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <deque.h>
  23. #include <vector.h>
  24. #include <fstream.h>
  25. #include <iostream.h>
  26. #include <iterator.h>
  27. #include <sstream.h>
  28. #include "mysql.h"
  29. #include <Printers.hpp>
  30.  
  31. TForm1 *Form1;
  32. bool i_start, NT;
  33. bool IsForce = false;
  34. bool IsVariables = false;
  35. bool IsProcess  = false ;
  36. bool IsDatabases = false;
  37. bool new_line = 0;
  38. bool ya = true;
  39. bool yy = true;
  40. bool rinit = false;
  41. AnsiString vpath;
  42. AnsiString vip;
  43. MYSQL_RES *res_1;
  44. static unsigned long q = 0;
  45. bool preport = false;
  46. bool treport = false;
  47. bool ereport = false;
  48. AnsiString mainroot;
  49. bool IsMySQLNode = false;
  50. MYSQL *MySQL;
  51. //---------------------------------------------------------------------------
  52. __fastcall TForm1::TForm1(TComponent* Owner)
  53.         : TForm(Owner)
  54. {
  55. }
  56. //---------------------------------------------------------------------------
  57. void __fastcall TForm1::FormCreate(TObject *Sender)
  58. {
  59.   i_start = true;
  60.   IsConnect = false;
  61.  if (ParamCount() > 0){
  62.    if (ParamStr(1) == "-h" || ParamStr(1) == "h" ) {
  63.      ShowHelp();  Application->Terminate();  }
  64.    else if (ParamStr(1) == "-w" || ParamStr(1) == "w") {
  65.      i_start = false;  ContinueLoad();   }
  66.   }
  67.  else {
  68.    ContinueLoad();  Hide(); GetServerOptions(); }
  69. }
  70. //---------------------------------------------------------------------------
  71. void __fastcall TForm1::DrawItem(TMessage& Msg)
  72. {
  73.    IconDrawItem((LPDRAWITEMSTRUCT)Msg.LParam);
  74.    TForm::Dispatch(&Msg);
  75. }
  76. //---------------------------------------------------------------------------
  77. void __fastcall TForm1::MyNotify(TMessage& Msg)
  78. {
  79.  POINT MousePos;
  80.  
  81.  switch(Msg.LParam) {
  82.   case WM_RBUTTONUP:
  83.     if (GetCursorPos(&MousePos)){
  84.        PopupMenu1->PopupComponent = Form1; SetForegroundWindow(Handle);
  85.        PopupMenu1->Popup(MousePos.x, MousePos.y);}
  86.     else  Show();
  87.     break;
  88.   case WM_LBUTTONUP:
  89.     if (GetCursorPos(&MousePos)){
  90.        PopupMenu1->PopupComponent = Form1; SetForegroundWindow(Handle);
  91.        PopupMenu1->Popup(MousePos.x, MousePos.y); }
  92.  
  93.     ToggleState();
  94.      break;
  95.   default:
  96.      break; }
  97.  
  98.  TForm::Dispatch(&Msg);
  99. }
  100. //---------------------------------------------------------------------------
  101. bool __fastcall TForm1::TrayMessage(DWORD dwMessage)
  102. {
  103.  NOTIFYICONDATA tnd;
  104.  PSTR pszTip;
  105.  
  106.  pszTip = TipText();
  107.  
  108.  tnd.cbSize          = sizeof(NOTIFYICONDATA);
  109.  tnd.hWnd            = Handle;
  110.  tnd.uID             = IDC_MYICON;
  111.  tnd.uFlags          = NIF_MESSAGE | NIF_ICON | NIF_TIP;
  112.  tnd.uCallbackMessage    = MYWM_NOTIFY;
  113.  
  114.  if (dwMessage == NIM_MODIFY){
  115.    tnd.hIcon    = IconHandle();
  116.    if (pszTip)lstrcpyn(tnd.szTip, pszTip, sizeof(tnd.szTip));
  117.    else tnd.szTip[0] = '\0'; }
  118.  else { tnd.hIcon = NULL; tnd.szTip[0] = '\0'; }
  119.  
  120.  return (Shell_NotifyIcon(dwMessage, &tnd));
  121. }
  122. //---------------------------------------------------------------------------
  123. HANDLE __fastcall TForm1::IconHandle(void)
  124. {
  125.  
  126.  if (!NT){
  127.    if (MySQLSignal()){Image3->Visible = false; Image2->Visible = true;
  128.           return (Image2->Picture->Icon->Handle); }
  129.    else              {Image2->Visible = false; Image3->Visible = true;
  130.           return (Image3->Picture->Icon->Handle); }
  131.     }
  132.  else   {
  133.    if (TheServiceStatus()){Image3->Visible = false; Image2->Visible = true;
  134.           return (Image2->Picture->Icon->Handle); }
  135.  
  136.    else if (MySQLSignal()){Image3->Visible = false; Image2->Visible = true;
  137.           return (Image2->Picture->Icon->Handle); }
  138.    else                   {Image2->Visible = false; Image3->Visible = true;
  139.           return (Image3->Picture->Icon->Handle); }
  140.     }
  141.  
  142. }
  143. //---------------------------------------------------------------------------
  144. void __fastcall TForm1::ToggleState(void)
  145. {
  146.  
  147.  TrayMessage(NIM_MODIFY);
  148.  if (!NT){
  149.    if (MySQLSignal()){SSW9->Caption = "ShutDown the Server";
  150.                      Image3->Visible = false; Image2->Visible = true; }
  151.    else              {SSW9->Caption = "Start the Server";
  152.                      Image2->Visible = false; Image3->Visible = true; }
  153.          }
  154.  else    {
  155.    if (TheServiceStart()) {
  156.       Standa->Enabled = false;
  157.       if (TheServiceStatus()) {RService->Enabled = false;
  158.                      StopS->Enabled = true;
  159.                      StopS->Caption = "Stop the Service";
  160.                      Image3->Visible = false;
  161.                      Image2->Visible = true; }
  162.       else                    {RService->Enabled = true;
  163.                      StopS->Enabled = true;
  164.                      RService->Caption = "Remove the Service";
  165.                      StopS->Caption = "Start the Service";
  166.                      Image2->Visible = false;
  167.                      Image3->Visible = true; }
  168.                           }
  169.     else                  {
  170.        Standa->Enabled = true;
  171.        StopS->Enabled = false;
  172.        if (MySQLSignal()) {
  173.                      RService->Enabled = false;
  174.                      Standa->Caption = "ShutDown the Server Standalone";
  175.                      Image3->Visible = false;
  176.                      Image2->Visible = true; }
  177.  
  178.        else               {
  179.                      RService->Enabled = true;
  180.                      RService->Caption = "Install the Service";
  181.                      Standa->Caption = "Start the Server Standalone";
  182.                      Image2->Visible = false;
  183.                      Image3->Visible = true; }
  184.  
  185.                            }
  186.  
  187.  
  188.           }
  189.  
  190. }
  191. //---------------------------------------------------------------------------
  192. PSTR __fastcall TForm1::TipText(void)
  193. {
  194.   char* status = StatusLine->SimpleText.c_str();
  195.   return status;
  196.  
  197. }
  198. //---------------------------------------------------------------------------
  199. void __fastcall TForm1::WMQueryEndSession(TWMQueryEndSession &msg)
  200. {
  201.  
  202.  
  203.   if (!NT) {
  204.  
  205.     if (MySQLSignal()){
  206.                         StatusLine->SimpleText = "Shutdown in progress.....";
  207.                         Show(); Shutd();  msg.Result = 1; }
  208.     else              {
  209.                         StatusLine->SimpleText = "The Server already is down......";
  210.                         Show();  msg.Result = 1;  Close(); }
  211.            }
  212.   else     {
  213.  
  214.     Show();
  215.     if                 (!TheServiceStart())  { if (MySQLSignal()) Shutd();   }
  216.                         msg.Result = 1;
  217.            }
  218.  
  219. }
  220.  
  221. //---------------------------------------------------------------------------
  222. LRESULT IconDrawItem(LPDRAWITEMSTRUCT lpdi)
  223. {
  224.    HICON hIcon;
  225.  
  226.    hIcon = (HICON)LoadImage(g_hinst, MAKEINTRESOURCE(lpdi->CtlID), IMAGE_ICON,
  227.         16, 16, 0);
  228.    if (!hIcon)
  229.        return(false);
  230.  
  231.    DrawIconEx(lpdi->hDC, lpdi->rcItem.left, lpdi->rcItem.top, hIcon,
  232.         16, 16, 0, NULL, DI_NORMAL);
  233.  
  234.    return(true);
  235. }
  236. //---------------------------------------------------------------------------
  237. AnsiString __fastcall TForm1::TheComputer()
  238. {
  239.   AnsiString theword;
  240.   DWORD dwSize = MAX_COMPUTERNAME_LENGTH + 1;
  241.   char szBuf[MAX_COMPUTERNAME_LENGTH + 1];
  242.   szBuf[0] = '\0';
  243.  
  244.   GetComputerName(szBuf, &dwSize);
  245.   theword = (AnsiString) szBuf;
  246.   delete [] szBuf;
  247.   return theword;
  248.  
  249. }
  250. //---------------------------------------------------------------------------
  251. AnsiString __fastcall TForm1::TheOS()
  252. {
  253.   AnsiString theword;
  254.   OSVERSIONINFO info;
  255.   info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  256.   GetVersionEx(&info);
  257.  
  258.   switch (info.dwPlatformId)
  259.     {
  260.       case VER_PLATFORM_WIN32s:
  261.         NT = false;
  262.         theword = "Win32s detected";
  263.         break;
  264.       case VER_PLATFORM_WIN32_WINDOWS:
  265.         NT = false;
  266.         theword = "Win 95 or Win 98 detected";
  267.         break;
  268.       case VER_PLATFORM_WIN32_NT:
  269.         NT = true;
  270.         theword = "Windows NT detected";
  271.         break;
  272.      }
  273.      return theword;
  274. }
  275. ///---------------------------------------------------------------------------
  276. AnsiString __fastcall TForm1::TheUser()
  277. {
  278.   AnsiString theword;
  279.   DWORD dwSize = 0;
  280.  
  281.   GetUserName(NULL, &dwSize);
  282.  
  283.   char *szBuf = new char[dwSize];
  284.   szBuf[0] = '\0';
  285.  
  286.   GetUserName(szBuf, &dwSize);
  287.   theword = (AnsiString) szBuf;
  288.   delete [] szBuf;
  289.   return theword;
  290.  
  291. }
  292. //---------------------------------------------------------------------------
  293. void __fastcall TForm1::TakeIP(void)
  294. {
  295.   WORD wVersionRequested;
  296.   WSADATA WSAData;
  297.   wVersionRequested = MAKEWORD(1,1);
  298.   WSAStartup(wVersionRequested,&WSAData);
  299.  
  300.   hostent *P;
  301.   char s[128];
  302.   in_addr in;
  303.   char *P2;
  304.   gethostname(s, 128);
  305.   P = gethostbyname(s);
  306.  
  307.   Memo2->Lines->Clear();
  308.   Memo2->Lines->Add((AnsiString)P->h_name);
  309.   mainroot = P->h_name;
  310.   in.S_un.S_un_b.s_b1 = P->h_addr_list[0][0];
  311.   in.S_un.S_un_b.s_b2 = P->h_addr_list[0][1];
  312.   in.S_un.S_un_b.s_b3 = P->h_addr_list[0][2];
  313.   in.S_un.S_un_b.s_b4 = P->h_addr_list[0][3];
  314.   P2 = inet_ntoa(in);
  315.   vip = P2;
  316.   mainroot += " ( " + (AnsiString)P2 + " )";
  317.   Memo2->Lines->Add(P2);
  318.  
  319. }
  320. //---------------------------------------------------------------------------
  321. void __fastcall TForm1::GetmemStatus(void)
  322. {
  323.   MEMORYSTATUS ms;
  324.   ms.dwLength = sizeof(MEMORYSTATUS);
  325.   GlobalMemoryStatus(&ms);
  326.  
  327.   Edit2->Text = AnsiString((double)ms.dwTotalPhys / 1024000.0) + " MB RAM";
  328. }
  329.  
  330. //---------------------------------------------------------------------------
  331. void __fastcall TForm1::ShowHelp(void)
  332. {
  333.    Application->MessageBox("Usage: WinMySQLadmin.EXE [OPTIONS]\n\n-w    Run the tool without start the Server.\n-h        Shows this message and exit ", "WinMySQLadmin 1.0", MB_OK |MB_ICONINFORMATION);
  334. }
  335. //---------------------------------------------------------------------------
  336. void __fastcall TForm1::ContinueLoad(void)
  337. {
  338.  OS->Text = TheOS();
  339.  Localhost->Text = TheComputer();
  340.  Localuser->Text = TheUser();
  341.  GetmemStatus();
  342.  ClearBox();
  343.  TakeIP();
  344.  MyODBC();
  345.  
  346.  
  347.  IsMyIniUp();
  348.  
  349.  if (!NT) { WinNT->Enabled = false; NtVer->Enabled = false; Win9->Enabled  = true; }
  350.  else     { WinNT->Enabled = true;   Win9->Enabled  = false;   }
  351.  
  352.  if (i_start)
  353.    {
  354.      // NT never is started from the prompt
  355.      if ((!NT) && (!MySQLSignal())) mysqldstart();
  356.       {
  357.        TrayMessage(NIM_MODIFY);
  358.        SeekErrFile();
  359.       }
  360.    }
  361.   Hide();
  362.  
  363. }
  364.  
  365. //---------------------------------------------------------------------------
  366. void __fastcall TForm1::MyODBC(void)
  367. {
  368.  
  369.   TRegistry *Registry = new TRegistry();
  370.   Memo3->Lines->Clear();
  371.  
  372.   try
  373.    {
  374.      Registry->RootKey = HKEY_LOCAL_MACHINE;
  375.      // the basic data of myodbc
  376.      if (Registry->OpenKey("Software\\ODBC\\ODBCINST.INI\\MySQL", false))
  377.        {
  378.          Memo3->Lines->Add("Driver Version\t" + Registry->ReadString("DriverODBCVer"));
  379.          Memo3->Lines->Add("Driver\t\t" + Registry->ReadString("Driver"));
  380.          Memo3->Lines->Add("API Level\t\t" + Registry->ReadString("APILevel"));
  381.          Memo3->Lines->Add("Setup\t\t" + Registry->ReadString("Setup"));
  382.          Memo3->Lines->Add("SQL Level\t" + Registry->ReadString("SQLLevel"));
  383.        }
  384.       else
  385.          Memo3->Lines->Add("Not Found");
  386.  
  387.    }
  388.   catch (...)
  389.    {
  390.     delete Registry;
  391.    }
  392.    Memo3->Enabled = false;
  393. }
  394. //---------------------------------------------------------------------------
  395.  
  396. void __fastcall TForm1::IsMyIniUp(void)
  397. {
  398.   // we see if the my.ini is Up
  399.  AnsiString asFileName = FileSearch("my.ini", TheWinDir());
  400.  if (asFileName.IsEmpty())
  401.    {
  402.      IsForce = true;
  403.      i_start = false;
  404.      QuickSearch();
  405.    }
  406.  else
  407.    {
  408.      Memo1->Enabled = true;
  409.      Memo1->Lines->Clear();
  410.      FillMyIni();
  411.      GetBaseDir();
  412.    }
  413. }
  414. //---------------------------------------------------------------------------
  415. void __fastcall TForm1::QuickSearch(void)
  416. {
  417.  AnsiString asFileName = FileSearch("mysql.exe", "c:/mysql/bin");
  418.  if (!asFileName.IsEmpty())
  419.   BaseDir->Text = "c:/mysql";
  420. }
  421. //---------------------------------------------------------------------------
  422. AnsiString __fastcall TForm1::TheWinDir()
  423. {
  424.   AnsiString WinDir;
  425.   UINT       BufferSize = GetWindowsDirectory(NULL,0);
  426.   WinDir.SetLength(BufferSize+1);
  427.   GetWindowsDirectory(WinDir.c_str(),BufferSize);
  428.   char* dirw = WinDir.c_str();
  429.   return dirw ;
  430.  
  431. }
  432. //---------------------------------------------------------------------------
  433. void __fastcall TForm1::FillMyIni(void)
  434. {
  435.  Memo1->Lines->LoadFromFile(TheWinDir() + "\\my.ini");
  436.  
  437. }
  438. //---------------------------------------------------------------------------
  439. void __fastcall TForm1::GetBaseDir(void)
  440. {
  441.  
  442.  char drive[_MAX_DRIVE];
  443.  char dir[_MAX_DIR];
  444.  char file[_MAX_FNAME];
  445.  char ext[_MAX_EXT];
  446.  
  447.  
  448.  TIniFile *pIniFile = new
  449.  TIniFile(TheWinDir() + "\\my.ini");
  450.  
  451.  BaseDir->Text = pIniFile->ReadString("mysqld","basedir","")  ;
  452.  AnsiString lx = pIniFile->ReadString("WinMySQLadmin","Server","")  ;
  453.  _splitpath((lx).c_str(),drive,dir,file,ext);
  454.  AnsiString lw = (AnsiString) file + ext;
  455.  
  456.  if ( lw == "mysqld-shareware.exe") {ShareVer->Checked = true;}
  457.  if ( lw == "mysqld.exe") {MysqldVer->Checked = true;}
  458.  if ( lw == "mysqld-opt.exe") {OptVer->Checked = true;}
  459.  if ( lw == "mysqld-nt.exe") {NtVer->Checked = true;}
  460.  
  461.  delete pIniFile;
  462.  
  463. }
  464. //---------------------------------------------------------------------------
  465. void __fastcall TForm1::Showme1Click(TObject *Sender)
  466. {
  467.  if(Showme1->Caption == "Show me")  {  TrayMessage(NIM_DELETE);
  468.        Showme1->Caption = "Hide me";  Show();  }
  469.   else  { TrayMessage(NIM_ADD);  TrayMessage(NIM_MODIFY);
  470.        Showme1->Caption = "Show me";  Hide();  }
  471. }
  472. //---------------------------------------------------------------------------
  473. bool __fastcall TForm1::MySQLSignal()
  474. {
  475.   HANDLE hEventShutdown;
  476.   hEventShutdown=OpenEvent(EVENT_MODIFY_STATE, 0, "MySqlShutdown");
  477.  
  478.   if(hEventShutdown)
  479.     {
  480.       CloseHandle(hEventShutdown);
  481.       return true;
  482.     }
  483.   else
  484.     {
  485.      CloseHandle(hEventShutdown);
  486.      return false;
  487.     }
  488.  
  489. }
  490.  
  491. //---------------------------------------------------------------------------
  492. bool __fastcall TForm1::mysqldstart()
  493. {
  494.     memset(&pi, 0, sizeof(pi));
  495.     memset(&si, 0, sizeof(si));
  496.     si.cb = sizeof(si);
  497.     si.dwFlags |= STARTF_USESHOWWINDOW;
  498.     si.wShowWindow |= SW_SHOWNORMAL;
  499.  
  500.  
  501.     TIniFile *pIniFile = new
  502.     TIniFile(TheWinDir() + "\\my.ini");
  503.  
  504.     if (NT)
  505.      vpath =   pIniFile->ReadString("WinMySQLadmin","Server","") + " --standalone\0" ;
  506.     else
  507.       vpath =   pIniFile->ReadString("WinMySQLadmin","Server","") + "\0" ;
  508.  
  509.     if ( ! CreateProcess(0,vpath.c_str(), 0, 0, 0, 0, 0, 0, &si,&pi))
  510.      {
  511.        TrayMessage(NIM_MODIFY);
  512.        return false;
  513.      }
  514.     else
  515.       {
  516.         TrayMessage(NIM_MODIFY);
  517.         return true;
  518.  
  519.       }
  520.  
  521. }
  522.  
  523. //---------------------------------------------------------------------------
  524. bool __fastcall TForm1::SeekErrFile()
  525. {
  526.   Memo4->Enabled = true;
  527.   Memo4->Lines->Clear();
  528.   AnsiString asFileName = FileSearch("mysql.err", BaseDir->Text + "/data");
  529.   if (!asFileName.IsEmpty())
  530.    {
  531.      FName = BaseDir->Text + "/data/mysql.err";
  532.      ifstream in((FName).c_str());
  533.      in.seekg(0, ios::end);
  534.      string s, line;
  535.      deque<string> v;
  536.      deque<string> lines;
  537.      streampos sp = in.tellg();
  538.      if (sp <= 1000)
  539.       in.seekg(0, ios::beg);
  540.     else
  541.      {
  542.       in.seekg(0, ios::beg);
  543.       in.seekg((sp - 1000));
  544.      }
  545.  
  546.      do {
  547.           lines.push_back(line);
  548.      }while (getline(in, line));
  549.  
  550.  
  551.      if( lines.size() <= 15)
  552.       {
  553.         deque<string>::reverse_iterator r;
  554.         for(r = lines.rbegin(); r != lines.rend() ; r++)
  555.          {
  556.           if (ereport)
  557.            Memo5->Lines->Add((*r).c_str());
  558.            Memo4->Lines->Add((*r).c_str());
  559.  
  560.          }
  561.       }
  562.      else
  563.       {
  564.         int k = 0;
  565.         deque<string>::reverse_iterator r;
  566.         for(r = lines.rbegin(); r != lines.rend(); r++)
  567.          {
  568.            if (ereport)
  569.            Memo5->Lines->Add((*r).c_str());
  570.            Memo4->Lines->Add((*r).c_str());
  571.            if (++k >= 15) { break;}
  572.          }
  573.       }
  574.       in.close();
  575.       return true;
  576.    }
  577.   else
  578.     return false;
  579.  
  580. }
  581.  
  582. //---------------------------------------------------------------------------
  583. void __fastcall TForm1::Timer1Timer(TObject *Sender)
  584. {
  585.  Showme1->Caption = "Show me";
  586.   TrayMessage(NIM_ADD);
  587.   TrayMessage(NIM_MODIFY);
  588.   Hide();
  589.   if (IsForce) {Form2->Show();}
  590.   Timer1->Enabled = false;
  591. }
  592. //---------------------------------------------------------------------------
  593. void __fastcall TForm1::GetServerFile()
  594. {
  595.  
  596.   AnsiString FileName;
  597.  
  598.  if(!NT) {
  599.    FileName =  FileSearch("mysqld-opt.exe", ExtractFilePath(Application->ExeName));
  600.    if (FileName.IsEmpty())  FileName =  FileSearch("mysqld.exe", ExtractFilePath(Application->ExeName));
  601.    if (FileName.IsEmpty())    FileName =  FileSearch("mysqld-shareware.exe", ExtractFilePath(Application->ExeName));
  602.  
  603.    if (!FileName.IsEmpty()){
  604.       if ( FileName == "mysqld-opt.exe") {OptVer->Checked = true;}
  605.       if ( FileName == "mysqld.exe") {MysqldVer->Checked= true;}
  606.       if ( FileName == "mysqld-shareware.exe") {ShareVer->Checked= true;} }
  607.  
  608.   }
  609.  else {
  610.  
  611.    FileName =  FileSearch("mysqld-nt.exe", ExtractFilePath(Application->ExeName));
  612.    if (FileName.IsEmpty())  FileName =  FileSearch("mysqld.exe", ExtractFilePath(Application->ExeName));
  613.    if (FileName.IsEmpty())  FileName =  FileSearch("mysqld-shareware.exe", ExtractFilePath(Application->ExeName));
  614.  
  615.    if (!FileName.IsEmpty()) {
  616.       if ( FileName == "mysqld-nt.exe") {NtVer->Checked = true;}
  617.       if ( FileName == "mysqld.exe") {MysqldVer->Checked= true;}
  618.       if ( FileName == "mysqld-shareware.exe") {ShareVer->Checked= true;} }
  619.  
  620.      }
  621. }
  622. //---------------------------------------------------------------------------
  623. void __fastcall TForm1::CreateMyIniFile(void)
  624. {
  625.   char szFileName[6];
  626.   int iFileHandle;
  627.   AnsiString jk;
  628.  
  629.   Memo1->Enabled = true;
  630.   Memo1->Lines->Clear();
  631.   strcpy(szFileName,"\\my.ini");
  632.   iFileHandle = FileCreate(TheWinDir() + szFileName );
  633.  
  634.   jk = "#This File was made using the WinMySQLadmin 1.0 Tool\n" ;
  635.   FileWrite(iFileHandle, (jk).c_str(), (jk).Length());
  636.  
  637.   jk = "#" + Now() + "\n\n" ;
  638.   FileWrite(iFileHandle, (jk).c_str(), (jk).Length());
  639.  
  640.   jk = "#Uncomment or Add only the keys that you know how works.\n" ;
  641.   FileWrite(iFileHandle, (jk).c_str(), (jk).Length());
  642.  
  643.   jk = "#Read the MySQL Manual for instructions\n\n" ;
  644.   FileWrite(iFileHandle, (jk).c_str(), (jk).Length());
  645.  
  646.  
  647.   jk = "[mysqld]\n\n" ;
  648.   FileWrite(iFileHandle, (jk).c_str(), (jk).Length());
  649.  
  650.    jk = "basedir=" + TheDir() + "\n";
  651.     FileWrite(iFileHandle, (jk).c_str(), (jk).Length());
  652.  
  653.     jk = "#bind-address=" + vip + "\n" ;
  654.     FileWrite(iFileHandle, (jk).c_str(), (jk).Length());
  655.  
  656.     jk = "#datadir=" + TheDir() + "/data\n" ;
  657.     FileWrite(iFileHandle, (jk).c_str(), (jk).Length());
  658.  
  659.     jk = "#language=" + TheDir() + "/share/your language directory\n" ;
  660.     FileWrite(iFileHandle, (jk).c_str(), (jk).Length());
  661.  
  662.     jk = "#delay-key-write-for-all-tables\n" ;
  663.     FileWrite(iFileHandle, (jk).c_str(), (jk).Length());
  664.  
  665.     jk = "#log-long-format\n" ;
  666.     FileWrite(iFileHandle, (jk).c_str(), (jk).Length());
  667.  
  668.     jk = "#slow query log=#\n" ;
  669.     FileWrite(iFileHandle, (jk).c_str(), (jk).Length());
  670.  
  671.     jk = "#tmpdir=#\n" ;
  672.     FileWrite(iFileHandle, (jk).c_str(), (jk).Length());
  673.  
  674.     jk = "#ansi\n" ;
  675.     FileWrite(iFileHandle, (jk).c_str(), (jk).Length());
  676.  
  677.     jk = "#new\n" ;
  678.     FileWrite(iFileHandle, (jk).c_str(), (jk).Length());
  679.  
  680.     jk = "#port=3306\n" ;
  681.     FileWrite(iFileHandle, (jk).c_str(), (jk).Length());
  682.  
  683.     jk = "#safe\n" ;
  684.     FileWrite(iFileHandle, (jk).c_str(), (jk).Length());
  685.  
  686.     jk = "#skip-name-resolve\n" ;
  687.     FileWrite(iFileHandle, (jk).c_str(), (jk).Length());
  688.  
  689.     jk = "#skip-networking\n" ;
  690.     FileWrite(iFileHandle, (jk).c_str(), (jk).Length());
  691.  
  692.     jk = "#skip-new\n" ;
  693.     FileWrite(iFileHandle, (jk).c_str(), (jk).Length());
  694.  
  695.     jk = "#skip-host-cache\n" ;
  696.     FileWrite(iFileHandle, (jk).c_str(), (jk).Length());
  697.  
  698.     jk = "#set-variable = key_buffer=16M\n" ;
  699.     FileWrite(iFileHandle, (jk).c_str(), (jk).Length());
  700.  
  701.     jk = "#set-variable = max_allowed_packet=1M\n" ;
  702.     FileWrite(iFileHandle, (jk).c_str(), (jk).Length());
  703.  
  704.     jk = "#set-variable = thread_stack=128K\n" ;
  705.     FileWrite(iFileHandle, (jk).c_str(), (jk).Length());
  706.  
  707.     jk = "#set-variable = flush_time=1800\n\n" ;
  708.     FileWrite(iFileHandle, (jk).c_str(), (jk).Length());
  709.  
  710.   jk = "[mysqldump]\n\n" ;
  711.   FileWrite(iFileHandle, (jk).c_str(), (jk).Length());
  712.  
  713.   jk = "#quick\n" ;
  714.   FileWrite(iFileHandle, (jk).c_str(), (jk).Length());
  715.  
  716.   jk = "#set-variable = max_allowed_packet=16M\n\n" ;
  717.   FileWrite(iFileHandle, (jk).c_str(), (jk).Length());
  718.  
  719.   jk = "[mysql]\n\n" ;
  720.   FileWrite(iFileHandle, (jk).c_str(), (jk).Length());
  721.  
  722.   jk = "#no-auto-rehash\n\n" ;
  723.   FileWrite(iFileHandle, (jk).c_str(), (jk).Length());
  724.  
  725.   jk = "[isamchk]\n\n" ;
  726.   FileWrite(iFileHandle, (jk).c_str(), (jk).Length());
  727.  
  728.   jk = "#set-variable= key=16M\n\n" ;
  729.   FileWrite(iFileHandle, (jk).c_str(), (jk).Length());
  730.  
  731.   jk = "[WinMySQLadmin]\n\n" ;
  732.   FileWrite(iFileHandle, (jk).c_str(), (jk).Length());
  733.  
  734.  
  735.   if (ShareVer->Checked) { jk = "Server=" + TheDir() + "/bin/mysqld-shareware.exe\n\n";}
  736.   if (MysqldVer->Checked) {jk = "Server=" + TheDir() + "/bin/mysqld.exe\n\n";}
  737.   if (OptVer->Checked) {jk = "Server=" + TheDir() + "/bin/mysqld-opt.exe\n\n";}
  738.   if (NtVer->Checked) {jk = "Server=" + TheDir() + "/bin/mysqld-nt.exe\n\n";}
  739.   FileWrite(iFileHandle, (jk).c_str(), (jk).Length());
  740.  
  741.   jk = "user=" + Form2->Edit1->Text + "\n" ;
  742.   FileWrite(iFileHandle, (jk).c_str(), (jk).Length());
  743.  
  744.   jk = "password=" + Form2->Edit2->Text + "\n" ;
  745.   FileWrite(iFileHandle, (jk).c_str(), (jk).Length());
  746.  
  747.   FileClose(iFileHandle);
  748.   FillMyIni();
  749.  
  750. }
  751.  
  752. //---------------------------------------------------------------------------
  753. bool __fastcall TForm1::CreatingShortCut()
  754. {
  755.   // Where is The Start Menu in this Machine ?
  756.   LPITEMIDLIST  pidl;
  757.   LPMALLOC      pShellMalloc;
  758.   char          szDir[MAX_PATH + 16];
  759.   AnsiString file;
  760.   AnsiString jk = "\\WinMySQLadmin.lnk" ;
  761.  
  762.   if(SUCCEEDED(SHGetMalloc(&pShellMalloc)))
  763.    {
  764.      if(SUCCEEDED(SHGetSpecialFolderLocation(NULL,
  765.           CSIDL_STARTUP, &pidl)))
  766.       {
  767.         if(!SHGetPathFromIDList(pidl, szDir))
  768.           {
  769.             pShellMalloc->Release();
  770.             pShellMalloc->Free(pidl);
  771.             return false;
  772.            }
  773.  
  774.           pShellMalloc->Free(pidl);
  775.        }
  776.  
  777.         pShellMalloc->Release();
  778.         StrCat(szDir, jk.c_str());
  779.     }
  780.  
  781.  // the create
  782.  
  783.  IShellLink* pLink;
  784.  IPersistFile* pPersistFile;
  785.  
  786.  if(SUCCEEDED(CoInitialize(NULL)))
  787.    {
  788.      if(SUCCEEDED(CoCreateInstance(CLSID_ShellLink, NULL,
  789.                      CLSCTX_INPROC_SERVER,
  790.                      IID_IShellLink, (void **) &pLink)))
  791.        {
  792.  
  793.          pLink->SetPath((ExtractFilePath(Application->ExeName) + "WinMySQLadmin.exe").c_str());
  794.          pLink->SetDescription("WinMySQLadmin Tool");
  795.          pLink->SetShowCmd(SW_SHOW);
  796.  
  797.             if(SUCCEEDED(pLink->QueryInterface(IID_IPersistFile,
  798.                                                (void **)&pPersistFile)))
  799.             {
  800.  
  801.                 WideString strShortCutLocation(szDir);
  802.                 pPersistFile->Save(strShortCutLocation.c_bstr(), TRUE);
  803.                 pPersistFile->Release();
  804.             }
  805.                pLink->Release();
  806.         }
  807.  
  808.         CoUninitialize();
  809.     }
  810.  
  811.  
  812.   return true;
  813. }
  814.  
  815. //---------------------------------------------------------------------------
  816. AnsiString __fastcall TForm1::TheDir()
  817. {
  818.  AnsiString buffer;
  819.  char s[_MAX_PATH + 1];
  820.  
  821.  StrCopy(s, ( BaseDir->Text).c_str()) ;
  822.  
  823.  for (int i = 0; s[i] != NULL; i++)
  824.   if (s[i] != '\\')
  825.     buffer += s[i];
  826.   else
  827.     buffer += "/";
  828.  
  829.  return buffer;
  830.  
  831. }
  832.  
  833. //---------------------------------------------------------------------------
  834. void __fastcall TForm1::SpeedButton1Click(TObject *Sender)
  835. {
  836.  Application->HelpCommand(HELP_FINDER,0);
  837. }
  838. //---------------------------------------------------------------------------
  839.  
  840. void __fastcall TForm1::Timer2Timer(TObject *Sender)
  841. {
  842.  ToggleState();
  843.  
  844. }
  845. //---------------------------------------------------------------------------
  846. bool __fastcall TForm1::TheServiceStart()
  847. {
  848.   bool thatok;
  849.   char *SERVICE_NAME = "MySql";
  850.   SC_HANDLE myService, scm;
  851.   scm = OpenSCManager(0, 0, SC_MANAGER_ALL_ACCESS | GENERIC_WRITE);
  852.   if (scm)
  853.    {
  854.      myService = OpenService(scm, SERVICE_NAME, SERVICE_ALL_ACCESS);
  855.      if (myService)
  856.       thatok = true;
  857.      else
  858.       thatok = false;
  859.    }
  860.    CloseServiceHandle(myService);
  861.    CloseServiceHandle(scm);
  862.    return thatok;
  863. }
  864.  
  865. //---------------------------------------------------------------------------
  866. bool __fastcall TForm1::TheServicePause()
  867. {
  868.  
  869.   bool thatok;
  870.   char *SERVICE_NAME = "MySql";
  871.   SC_HANDLE myService, scm;
  872.   scm = OpenSCManager(0, 0, SC_MANAGER_ALL_ACCESS);
  873.  
  874.   if (scm)
  875.     {
  876.        myService = OpenService(scm, SERVICE_NAME, SERVICE_ALL_ACCESS);
  877.        if (myService)
  878.          {
  879.            // stop the service
  880.            if (IsConnect)
  881.             {
  882.                mysql_kill(MySQL,mysql_thread_id(MySQL));
  883.                StatusLine->SimpleText = "";
  884.                q = 0;
  885.             }
  886.  
  887.  
  888.             SERVICE_STATUS ss;
  889.             thatok = ControlService(myService,
  890.                             SERVICE_CONTROL_STOP,
  891.                             &ss);
  892.  
  893.          }
  894.        else
  895.        thatok = false;
  896.     }
  897.    else
  898.       thatok = false;
  899.  
  900.    CloseServiceHandle(myService);
  901.    CloseServiceHandle(scm);
  902.    return thatok;
  903. }
  904. //---------------------------------------------------------------------------
  905. bool __fastcall TForm1::TheServiceResume()
  906. {
  907.  
  908.   bool thatok;
  909.   char *SERVICE_NAME = "MySql";
  910.   SC_HANDLE myService, scm;
  911.   scm = OpenSCManager(0, 0, SC_MANAGER_ALL_ACCESS);
  912.  
  913.   if (scm)
  914.     {
  915.        myService = OpenService(scm, SERVICE_NAME, SERVICE_ALL_ACCESS);
  916.        if (myService)
  917.          {
  918.            // start the service
  919.  
  920.             thatok = StartService(myService, 0, NULL);
  921.          }
  922.        else
  923.         thatok = false;
  924.     }
  925.    else
  926.       thatok = false;
  927.  
  928.    CloseServiceHandle(myService);
  929.    CloseServiceHandle(scm);
  930.    return thatok;
  931. }
  932. //---------------------------------------------------------------------------
  933. bool __fastcall TForm1::TheServiceStatus()
  934. {
  935.   bool thatok;
  936.   bool k;
  937.   char *SERVICE_NAME = "MySql";
  938.   SC_HANDLE myService, scm;
  939.   SERVICE_STATUS ss;
  940.   DWORD dwState = 0xFFFFFFFF;
  941.   scm = OpenSCManager(0, 0, SC_MANAGER_ALL_ACCESS);
  942.  
  943.   if (scm)
  944.     {
  945.        myService = OpenService(scm, SERVICE_NAME, SERVICE_ALL_ACCESS);
  946.        if (myService)
  947.          {
  948.            memset(&ss, 0, sizeof(ss));
  949.            k = QueryServiceStatus(myService,&ss);
  950.            if (k)
  951.              {
  952.                dwState = ss.dwCurrentState;
  953.                if (dwState == SERVICE_RUNNING)
  954.                  thatok = true;
  955.              }
  956.            else
  957.             thatok = false;
  958.          }
  959.        else
  960.         thatok = false;
  961.     }
  962.    else
  963.       thatok = false;
  964.  
  965.    CloseServiceHandle(myService);
  966.    CloseServiceHandle(scm);
  967.    return thatok;
  968. }
  969. //---------------------------------------------------------------------------
  970. bool __fastcall TForm1::TheServiceCreate()
  971.  
  972. {
  973.   bool thatok;
  974.   char *SERVICE_NAME = "MySql";
  975.   char *szFullPath = vpath.c_str();
  976.   SC_HANDLE myService, scm;
  977.   scm = OpenSCManager(0, 0, SC_MANAGER_ALL_ACCESS);
  978.  
  979.   if (scm)
  980.    {  myService = CreateService(
  981.     scm,
  982.     SERVICE_NAME,
  983.     SERVICE_NAME,
  984.     SERVICE_ALL_ACCESS,
  985.     SERVICE_WIN32_OWN_PROCESS,
  986.     SERVICE_AUTO_START    ,
  987.     SERVICE_ERROR_NORMAL,
  988.     szFullPath,
  989.     NULL,
  990.     NULL,
  991.     NULL,
  992.     NULL,
  993.     NULL);
  994.  
  995.      if (myService)
  996.        thatok = true;
  997.      else
  998.        thatok = false;
  999.  
  1000.   }
  1001.  
  1002.    CloseServiceHandle(myService);
  1003.    CloseServiceHandle(scm);
  1004.    return thatok;
  1005.  
  1006. }
  1007. //---------------------------------------------------------------------------
  1008.  
  1009. void __fastcall TForm1::Swin9Click(TObject *Sender)
  1010. {
  1011.  if(Application->MessageBox("Shutdown this tool", "WinMySQLadmin 1.0", MB_YESNOCANCEL | MB_ICONQUESTION    ) == IDYES)
  1012.   Close();
  1013. }
  1014. //---------------------------------------------------------------------------
  1015.  
  1016. void __fastcall TForm1::SSW9Click(TObject *Sender)
  1017. {
  1018.  if (MySQLSignal())
  1019.     {
  1020.       if(Application->MessageBox("Shutdown the MySQL Server ", "WinMySQLadmin 1.0", MB_YESNOCANCEL | MB_ICONQUESTION    ) == IDYES)
  1021.         {
  1022.  
  1023.           if (Shutd())
  1024.             {
  1025.               IsConnect = false;
  1026.               IsVariables = false;
  1027.               IsProcess = false;
  1028.               IsDatabases = false;
  1029.               ya = false;
  1030.               ClearBox();
  1031.               Sleep(500);
  1032.               TrayMessage(NIM_MODIFY);
  1033.  
  1034.             }
  1035.           else
  1036.               Application->MessageBox("Fails to Shutdown the Server", "WinMySQLadmin 1.0", MB_OK | MB_ICONEXCLAMATION);
  1037.         }
  1038.     }
  1039.    else
  1040.      {
  1041.        if(Application->MessageBox("Start the MySQL Server ", "WinMySQLadmin 1.0", MB_YESNOCANCEL | MB_ICONQUESTION    ) == IDYES)
  1042.         {
  1043.           if (mysqldstart())
  1044.             {
  1045.               TrayMessage(NIM_MODIFY);
  1046.               ya = true;
  1047.             }
  1048.            else
  1049.              Application->MessageBox("Fails to Start the Server", "WinMySQLadmin 1.0", MB_OK | MB_ICONEXCLAMATION);
  1050.  
  1051.         }
  1052.      }
  1053. }
  1054. //---------------------------------------------------------------------------
  1055.  
  1056. void __fastcall TForm1::ShutDownBoth1Click(TObject *Sender)
  1057. {
  1058.   if (MySQLSignal())
  1059.   {
  1060.     if(Application->MessageBox("Shutdown the MySQL Server and this tool ", "WinMySQLadmin 1.0", MB_YESNOCANCEL | MB_ICONQUESTION    ) == IDYES)
  1061.      {
  1062.  
  1063.        if (Shutd())
  1064.         Close();
  1065.        else
  1066.          {
  1067.            Application->MessageBox("Fails to Shutdown the Server", "WinMySQLadmin 1.0", MB_OK | MB_ICONEXCLAMATION);
  1068.  
  1069.          }
  1070.      }
  1071.    }
  1072.  else
  1073.     if(Application->MessageBox("Shutdown this tool ", "WinMySQLadmin 1.0", MB_YESNOCANCEL | MB_ICONQUESTION    ) == IDYES)
  1074.     Close();
  1075. }
  1076. //---------------------------------------------------------------------------
  1077.  
  1078. void __fastcall TForm1::ShutDownthisTool1Click(TObject *Sender)
  1079. {
  1080.  if(Application->MessageBox("Shutdown this tool ", "WinMySQLadmin 1.0", MB_YESNOCANCEL | MB_ICONQUESTION    ) == IDYES)
  1081.    Close();
  1082. }
  1083. //---------------------------------------------------------------------------
  1084.  
  1085. void __fastcall TForm1::StopSClick(TObject *Sender)
  1086. {
  1087.   AnsiString theWarning;
  1088.   theWarning = "Are you sure to stop the Service ?\n\nAll the connections will be loss !" ;
  1089.   if (TheServiceStatus())
  1090.     {
  1091.       if(Application->MessageBox(theWarning.c_str(), "WinMySQLadmin 1.0", MB_YESNOCANCEL | MB_ICONQUESTION    ) == IDYES)
  1092.         {
  1093.           if (TheServicePause())
  1094.             {
  1095.               TrayMessage(NIM_MODIFY);
  1096.               IsConnect = false;
  1097.               IsVariables = false;
  1098.               IsProcess = false;
  1099.               IsDatabases = false;
  1100.               ya = false;
  1101.               ClearBox();
  1102.  
  1103.             }
  1104.           else
  1105.              Application->MessageBox("Fails to stop the Service", "WinMySQLadmin 1.0", MB_OK | MB_ICONEXCLAMATION);
  1106.  
  1107.         }
  1108.     }
  1109.    else
  1110.      {
  1111.        if(Application->MessageBox("Start the Service Manager for the MySQL Server ", "WinMySQLadmin 1.0", MB_YESNOCANCEL | MB_ICONQUESTION    ) == IDYES)
  1112.          {
  1113.            if (TheServiceResume())
  1114.              {
  1115.                ya = true;
  1116.                TrayMessage(NIM_MODIFY);
  1117.              }
  1118.            else
  1119.                Application->MessageBox("Fails to start the Service", "WinMySQLadmin 1.0", MB_OK | MB_ICONEXCLAMATION);
  1120.          }
  1121.      }
  1122. }
  1123. //---------------------------------------------------------------------------
  1124.  
  1125. void __fastcall TForm1::RServiceClick(TObject *Sender)
  1126. {
  1127.  if (TheServiceStart())
  1128.     {
  1129.       if(Application->MessageBox("Remove the MySQL Server service ", "WinMySQLadmin 1.0", MB_YESNOCANCEL | MB_ICONQUESTION    ) == IDYES)
  1130.        {
  1131.          if (!TheServiceRemove())
  1132.          Application->MessageBox("Fails to Remove The MySQL Server Service", "WinMySQLadmin 1.0", MB_OK | MB_ICONEXCLAMATION);
  1133.        }
  1134.     }
  1135.   else
  1136.     {
  1137.       if(Application->MessageBox("Install the MySQL Server service ", "WinMySQLadmin 1.0", MB_YESNOCANCEL | MB_ICONQUESTION    ) == IDYES)
  1138.        {
  1139.          if (!TheServerPath())
  1140.            Application->MessageBox("Please create first the my.ini setup", "WinMySQLadmin 1.0", MB_OK | MB_ICONEXCLAMATION);
  1141.          else
  1142.            {
  1143.              if (!TheServiceCreate())
  1144.               Application->MessageBox("Fails to Install The MySQL Server Service", "WinMySQLadmin 1.0", MB_OK | MB_ICONEXCLAMATION);
  1145.            }
  1146.  
  1147.        }
  1148.     }
  1149. }
  1150. //---------------------------------------------------------------------------
  1151.  
  1152. void __fastcall TForm1::StandaClick(TObject *Sender)
  1153. {
  1154.   if (MySQLSignal())
  1155.     {
  1156.       if(Application->MessageBox("Shutdown the MySQL Server ", "WinMySQLadmin 1.0", MB_YESNOCANCEL | MB_ICONQUESTION    ) == IDYES)
  1157.         {
  1158.           if (Shutd())
  1159.            {
  1160.              IsConnect = false;
  1161.              IsVariables = false;
  1162.              IsProcess = false;
  1163.              IsDatabases = false;
  1164.              ya = false;
  1165.              ClearBox();
  1166.              Sleep(500);
  1167.              TrayMessage(NIM_MODIFY);
  1168.  
  1169.             }
  1170.           else
  1171.             Application->MessageBox("Fails to Shutdown the Server", "WinMySQLadmin 1.0", MB_OK | MB_ICONEXCLAMATION);
  1172.         }
  1173.     }
  1174.    else
  1175.      {
  1176.        if(Application->MessageBox("Start the MySQL Server ", "WinMySQLadmin 1.0", MB_YESNOCANCEL | MB_ICONQUESTION    ) == IDYES)
  1177.         {
  1178.           if (mysqldstart())
  1179.             {
  1180.               StatusLine->SimpleText = "";
  1181.               TrayMessage(NIM_MODIFY);
  1182.  
  1183.             }
  1184.           else
  1185.           Application->MessageBox("Fails to Start the Server", "WinMySQLadmin 1.0", MB_OK | MB_ICONEXCLAMATION);
  1186.         }
  1187.      }
  1188. }
  1189. //---------------------------------------------------------------------------
  1190. bool __fastcall TForm1::Shutd()
  1191. {
  1192.   // from Irena
  1193.   HANDLE hEventShutdown;
  1194.   hEventShutdown=OpenEvent(EVENT_MODIFY_STATE, 0, "MySqlShutdown");
  1195.  
  1196.   if (IsConnect)
  1197.    {
  1198.      mysql_kill(MySQL,mysql_thread_id(MySQL));
  1199.      mysql_shutdown(MySQL);
  1200.      StatusLine->SimpleText = "";
  1201.  
  1202.    }
  1203.  
  1204.    q = 0;
  1205.  
  1206.  
  1207.   if(hEventShutdown)
  1208.    {
  1209.     SetEvent(hEventShutdown);
  1210.     CloseHandle(hEventShutdown);
  1211.     TrayMessage(NIM_MODIFY);
  1212.     IsConnect = false;
  1213.     return true;
  1214.    }
  1215.   else
  1216.    {
  1217.      TrayMessage(NIM_MODIFY);
  1218.      return false;
  1219.    }
  1220.  
  1221. }
  1222. //---------------------------------------------------------------------------
  1223. void __fastcall TForm1::ClearBox(void)
  1224. {
  1225.  
  1226.  st22->Text = "";
  1227.  st23->Text = "";
  1228.  st24->Text = "";
  1229.  st25->Text = "";
  1230.  st26->Text = "";
  1231.  st27->Text = "";
  1232.  st28->Text = "";
  1233.  st29->Text = "";
  1234.  Edit3->Text = "";
  1235.  Edit4->Text = "";
  1236.  Edit5->Text = "";
  1237.  Edit6->Text = "";
  1238.  
  1239. }
  1240. //---------------------------------------------------------------------------
  1241. bool __fastcall TForm1::TheServiceRemove()
  1242. {
  1243.   bool thatok;
  1244.   char *SERVICE_NAME = "MySql";
  1245.   SC_HANDLE myService, scm;
  1246.   scm = OpenSCManager(0, 0, SC_MANAGER_ALL_ACCESS);
  1247.   if (scm)
  1248.     {
  1249.       myService = OpenService(scm, SERVICE_NAME, SERVICE_ALL_ACCESS);
  1250.        if (myService)
  1251.          {
  1252.            if(DeleteService(myService))
  1253.              {
  1254.                CloseServiceHandle(myService);
  1255.                CloseServiceHandle(scm);
  1256.                thatok = true;
  1257.              }
  1258.            else
  1259.              {
  1260.                 CloseServiceHandle(myService);
  1261.                 CloseServiceHandle(scm);
  1262.                 thatok = false;
  1263.               }
  1264.  
  1265.           }
  1266.        else
  1267.           {
  1268.             CloseServiceHandle(myService);
  1269.             CloseServiceHandle(scm);
  1270.             thatok = false;
  1271.           }
  1272.     }
  1273.   else
  1274.      {
  1275.       thatok = false;
  1276.       CloseServiceHandle(scm);
  1277.      }
  1278.  
  1279.    return thatok;
  1280.  
  1281. }
  1282. //---------------------------------------------------------------------------
  1283. bool __fastcall TForm1::TheServerPath()
  1284. {
  1285.  
  1286.  TIniFile *pIniFile = new
  1287.  TIniFile(TheWinDir() + "\\my.ini");
  1288.  
  1289.  vpath = pIniFile->ReadString("WinMySQLadmin","Server","")  ;
  1290.  delete pIniFile;
  1291.  if (vpath.IsEmpty())
  1292.    return false;
  1293.  else
  1294.    return true;
  1295.  
  1296. }
  1297. //---------------------------------------------------------------------------
  1298.  
  1299. void __fastcall TForm1::Button5Click(TObject *Sender)
  1300. {
  1301.  if (!SeekErrFile())
  1302.   Application->MessageBox("Fails to find mysql.err", "WinMySQLadmin 1.0", MB_OK |MB_ICONINFORMATION);
  1303. }
  1304. //---------------------------------------------------------------------------
  1305. void __fastcall TForm1::IsMySQLInit(void)
  1306. {
  1307.   AnsiString theCommand;
  1308.   char    *host = NULL,*password=0,*user=0 ;
  1309.   TIniFile *pIniFile = new
  1310.   TIniFile(TheWinDir() + "\\my.ini");
  1311.  
  1312.   AnsiString MyUser = pIniFile->ReadString("WinMySQLadmin","user","")  ;
  1313.   AnsiString MyPass = pIniFile->ReadString("WinMySQLadmin","password","")  ;
  1314.  
  1315.   delete pIniFile;
  1316.  
  1317.  
  1318.  if (!MyUser.IsEmpty() && MyUser.Length() && !MyPass.IsEmpty() && MyPass.Length())
  1319.    {
  1320.      if (!IsConnect)
  1321.       {
  1322.  
  1323.        MySQL = mysql_init(MySQL);
  1324.        if (mysql_real_connect(MySQL, "localhost",(MyUser).c_str(), (MyPass).c_str() , 0, 0, NULL, 0))
  1325.         IsConnect = true;
  1326.        else
  1327.         {
  1328.          if(mysql_real_connect(MySQL,host,user,password , 0, 0, NULL, 0))
  1329.           {
  1330.             IsConnect = true;
  1331.             theCommand = "GRANT ALL PRIVILEGES ON *.* TO ";
  1332.             theCommand +=  "'" + MyUser + "' @localhost IDENTIFIED BY ";
  1333.             theCommand +=  "'" + MyPass + "' with GRANT OPTION";
  1334.             char* los = theCommand.c_str();
  1335.             if(!mysql_query(MySQL, los ))
  1336.              StatusLine->SimpleText = " ";
  1337.           }
  1338.  
  1339.         }
  1340.  
  1341.       }
  1342.  
  1343.   }
  1344.  else
  1345.   {
  1346.    if (!IsConnect)
  1347.     {
  1348.       MySQL = mysql_init(MySQL);
  1349.       if(mysql_real_connect(MySQL,host,user,password , 0, 0, NULL, 0))
  1350.       IsConnect = true;
  1351.     }
  1352.   }
  1353. }
  1354.  
  1355. //---------------------------------------------------------------------------
  1356.  
  1357. void __fastcall TForm1::Timer3Timer(TObject *Sender)
  1358. {
  1359.    if ((NT) && TheServiceStatus()) {IsMySQLInit(); }
  1360.  
  1361.   if ((NT) && !TheServiceStatus() && MySQLSignal()) {IsMySQLInit(); }
  1362.  
  1363.   if (!(NT) && MySQLSignal()) {IsMySQLInit(); }
  1364.  
  1365.   if (IsConnect)
  1366.    {
  1367.      GetServerStatus();
  1368.      if (!IsMySQLNode)
  1369.      GetMainRoot();
  1370.      Extended->Enabled = true;
  1371.      if (!IsProcess && !GetProcess())
  1372.       StatusLine->SimpleText = "";
  1373.      if (!IsVariables && !GetVariables())
  1374.         StatusLine->SimpleText = "";
  1375.      Timer3->Interval = 10000;
  1376.    }
  1377.   else
  1378.     Extended->Enabled = false;
  1379. }
  1380. //---------------------------------------------------------------------------
  1381. void __fastcall TForm1::GetServerStatus(void)
  1382. {
  1383.  
  1384.   GetExtendedStatus();
  1385.   Edit3->Text = mysql_get_server_info(MySQL);
  1386.   Edit4->Text = mysql_get_host_info(MySQL);
  1387.   Edit5->Text = mysql_get_client_info();
  1388.   Edit6->Text = mysql_get_proto_info(MySQL);
  1389.  
  1390.  
  1391. }
  1392.  
  1393. //---------------------------------------------------------------------------
  1394. bool __fastcall TForm1::GetProcess()
  1395. {
  1396.   MYSQL_RES *res;
  1397.   MYSQL_ROW row;
  1398.   unsigned int i;
  1399.   int k = 0;
  1400.   int therow = 1;
  1401.   new_line=1;
  1402.  
  1403.    StringGrid2->RowCount= 2;
  1404.  
  1405.    if (!(res=mysql_list_processes(MySQL)))
  1406.       {
  1407.            return false;
  1408.       }
  1409.  
  1410.       while ((row=mysql_fetch_row(res)) != 0)
  1411.        {
  1412.          mysql_field_seek(res,0);
  1413.          StringGrid2->Cells[0][0] = "PID";
  1414.          StringGrid2->Cells[1][0] = "User";
  1415.          StringGrid2->Cells[2][0] = "Host";
  1416.          StringGrid2->Cells[3][0] = "DB";
  1417.          StringGrid2->Cells[4][0] = "Command";
  1418.          StringGrid2->Cells[5][0] = "Time";
  1419.          StringGrid2->Cells[6][0] = "State";
  1420.          StringGrid2->Cells[7][0] = "Info";
  1421.          for (i=0 ; i < mysql_num_fields(res); i++)
  1422.           {
  1423.  
  1424.              if (k <= 6 )
  1425.               {
  1426.                StringGrid2->Cells[k][therow] = row[i];
  1427.                k++;
  1428.               }
  1429.              else
  1430.                {
  1431.  
  1432.                  StringGrid2->Cells[(k)][therow] = row[i];
  1433.                  k = 0;
  1434.                  therow++ ;
  1435.                  StringGrid2->RowCount++;
  1436.  
  1437.                }
  1438.  
  1439.           }
  1440.  
  1441.        }
  1442.  
  1443.       StringGrid2->RowCount--;
  1444.       mysql_free_result(res);
  1445.       StringGrid5->RowCount--;
  1446.       IsProcess = true;
  1447.       return true;
  1448.  
  1449. }
  1450. //---------------------------------------------------------------------------
  1451. bool __fastcall TForm1::GetVariables()
  1452. {
  1453.   MYSQL_RES *res;
  1454.   MYSQL_ROW row;
  1455.   unsigned int i;
  1456.   int k = 1;
  1457.   new_line=1;
  1458.   bool left = true;
  1459.   AnsiString report;
  1460.   StringGrid1->RowCount = 2;
  1461.    if (mysql_query(MySQL,"show variables") ||
  1462.       !(res=mysql_store_result(MySQL)))
  1463.       {
  1464.         return false;
  1465.       }
  1466.  
  1467.       while ((row=mysql_fetch_row(res)) != 0)
  1468.        {
  1469.          mysql_field_seek(res,0);
  1470.  
  1471.          StringGrid1->Cells[0][0] = "Variable Name";
  1472.          StringGrid1->Cells[1][0] = "Value";
  1473.  
  1474.  
  1475.          for (i=0 ; i < mysql_num_fields(res); i++)
  1476.           {
  1477.  
  1478.              if (left)
  1479.               {
  1480.                  if (treport)
  1481.                  report = GetString(row[i]);
  1482.                  StringGrid1->Cells[0][k++] = row[i];
  1483.                  left = false;
  1484.               }
  1485.              else
  1486.                {
  1487.                  if (treport)
  1488.                   Memo5->Lines->Add(report + row[i]);
  1489.                  StringGrid1->RowCount++;
  1490.                  StringGrid1->Cells[1][--k] = row[i];
  1491.                  k++;
  1492.                  left = true;
  1493.                }
  1494.  
  1495.           }
  1496.  
  1497.        }
  1498.  
  1499.     StringGrid1->RowCount--;
  1500.     mysql_free_result(res);
  1501.     IsVariables = true;
  1502.     return true;
  1503. }
  1504. //---------------------------------------------------------------------------
  1505. bool __fastcall TForm1::nice_time(AnsiString buff)
  1506. {
  1507.  
  1508.   unsigned long sec;
  1509.   unsigned long tmp;
  1510.   AnsiString  mytime;
  1511.  
  1512.   sec = StrToInt(buff);
  1513.  
  1514.   if (sec >= 3600L*24)
  1515.   {
  1516.     tmp=sec/(3600L*24);
  1517.     sec-=3600L*24*tmp;
  1518.  
  1519.     mytime = IntToStr(tmp);
  1520.     if (tmp > 1)
  1521.       mytime+= " days ";
  1522.     else
  1523.       mytime+= " day ";
  1524.  
  1525.   }
  1526.  
  1527.   if (sec >= 3600L)
  1528.   {
  1529.     tmp=sec/3600L;
  1530.     sec-=3600L*tmp;
  1531.      mytime += IntToStr(tmp);
  1532.     if (tmp > 1)
  1533.       mytime+= " hours ";
  1534.     else
  1535.       mytime+= " hour ";
  1536.   }
  1537.   if (sec >= 60)
  1538.   {
  1539.     tmp=sec/60;
  1540.     sec-=60*tmp;
  1541.     mytime += IntToStr(tmp);
  1542.     mytime+= " min ";
  1543.  
  1544.   }
  1545.   mytime += IntToStr(sec);
  1546.   mytime+= " sec ";
  1547.   st29->Text = mytime ;
  1548.  return true;
  1549. }
  1550. //---------------------------------------------------------------------------
  1551. void __fastcall TForm1::Button11Click(TObject *Sender)
  1552. {
  1553.   if (IsConnect)
  1554.   {
  1555.     if (GetVariables())
  1556.      StatusLine->SimpleText = "";
  1557.    }
  1558. }
  1559. //---------------------------------------------------------------------------
  1560.  
  1561. void __fastcall TForm1::Button10Click(TObject *Sender)
  1562. {
  1563.  if (IsConnect)
  1564.   {
  1565.     if (GetProcess())
  1566.      StatusLine->SimpleText = "";
  1567.    }
  1568. }
  1569. //---------------------------------------------------------------------------
  1570.  
  1571. void __fastcall TForm1::Button6Click(TObject *Sender)
  1572. {
  1573.   if (IsConnect)
  1574.    {
  1575.     if (mysql_refresh(MySQL,REFRESH_HOSTS))
  1576.        StatusLine->SimpleText = "";
  1577.    }
  1578. }
  1579. //---------------------------------------------------------------------------
  1580.  
  1581. void __fastcall TForm1::Button7Click(TObject *Sender)
  1582. {
  1583.   if (IsConnect)
  1584.   {
  1585.     if (mysql_refresh(MySQL,REFRESH_LOG))
  1586.      StatusLine->SimpleText = "";
  1587.   }
  1588. }
  1589. //---------------------------------------------------------------------------
  1590.  
  1591. void __fastcall TForm1::Button8Click(TObject *Sender)
  1592. {
  1593.   if (IsConnect)
  1594.   {
  1595.     if (mysql_refresh(MySQL,REFRESH_TABLES))
  1596.       StatusLine->SimpleText = "";
  1597.    }
  1598. }
  1599. //---------------------------------------------------------------------------
  1600.  
  1601. void __fastcall TForm1::Button2Click(TObject *Sender)
  1602. {
  1603.  Memo1->Enabled = true;
  1604.   Memo1->Lines->Clear();
  1605.   AnsiString asFileName = FileSearch("my.ini", TheWinDir());
  1606.   if (asFileName.IsEmpty())
  1607.    Application->MessageBox("Don't found my.ini file on the Win Directory", "WinMySQLadmin 1.0", MB_OK |MB_ICONINFORMATION);
  1608.   else
  1609.    FillMyIni();
  1610. }
  1611. //---------------------------------------------------------------------------
  1612.  
  1613. void __fastcall TForm1::Button3Click(TObject *Sender)
  1614. {
  1615.  TIniFile *pIniFile = new
  1616.  TIniFile(TheWinDir() + "\\my.ini");
  1617.  
  1618.  if (!Memo1->GetTextLen())
  1619.    Application->MessageBox("The Memo Box is Empty", "WinMySQLadmin 1.0", MB_OK |MB_ICONINFORMATION);
  1620.  else
  1621.   {
  1622.    if(Application->MessageBox("Are you sure to write the modifications into My.ini file.", "WinMySQLadmin 1.0", MB_YESNOCANCEL | MB_ICONQUESTION    ) == IDYES)
  1623.     {
  1624.       Memo1->Lines->SaveToFile(TheWinDir() + "\\my.ini");
  1625.  
  1626.       Memo1->Lines->Clear();
  1627.       Memo1->Enabled = true;
  1628.       Memo1->Lines->Clear();
  1629.       if (NtVer->Checked)
  1630.        pIniFile->WriteString("WinMySQLadmin","Server",TheDir() + "/bin/mysqld-nt.exe");
  1631.       if (MysqldVer->Checked == true)
  1632.        pIniFile->WriteString("WinMySQLadmin","Server", TheDir() + "/bin/mysqld.exe");
  1633.       if (ShareVer->Checked)
  1634.        pIniFile->WriteString("WinMySQLadmin","Server",TheDir() + "/bin/mysqld-shareware.exe");
  1635.       if (OptVer->Checked)
  1636.        pIniFile->WriteString("WinMySQLadmin","Server", TheDir() + "/bin/mysqld-opt.exe");
  1637.       FillMyIni();
  1638.       Application->MessageBox("My.ini was modificated", "WinMySQLadmin 1.0", MB_OK |MB_ICONINFORMATION);
  1639.     }
  1640.  
  1641.   }
  1642.   delete pIniFile;
  1643.   Memo1->Lines->Clear();
  1644.   FillMyIni();
  1645.  
  1646. }
  1647. //---------------------------------------------------------------------------
  1648.  
  1649. void __fastcall TForm1::Button1Click(TObject *Sender)
  1650. {
  1651.   if(CreatingShortCut())
  1652.    Application->MessageBox("The ShortCut on Start Menu was created", "WinMySQLadmin 1.0", MB_OK |MB_ICONINFORMATION);
  1653.  else
  1654.    Application->MessageBox("Fails the Operation of Create the ShortCut", "WinMySQLadmin 1.0", MB_OK |MB_ICONINFORMATION);
  1655. }
  1656. //---------------------------------------------------------------------------
  1657.  
  1658. void __fastcall TForm1::SpeedButton2Click(TObject *Sender)
  1659. {
  1660.  BROWSEINFO    info;
  1661.   char          szDir[MAX_PATH];
  1662.   char          szDisplayName[MAX_PATH];
  1663.   LPITEMIDLIST  pidl;
  1664.   LPMALLOC      pShellMalloc;
  1665.  
  1666.  
  1667.  if(SHGetMalloc(&pShellMalloc) == NO_ERROR)
  1668.     {
  1669.  
  1670.       memset(&info, 0x00,sizeof(info));
  1671.       info.hwndOwner = Handle;
  1672.       info.pidlRoot  = 0;
  1673.       info.pszDisplayName = szDisplayName;
  1674.       info.lpszTitle = "Search MySQL Base Directory";
  1675.       info.ulFlags   = BIF_RETURNONLYFSDIRS;
  1676.       info.lpfn = 0;
  1677.  
  1678.       pidl = SHBrowseForFolder(&info);
  1679.  
  1680.       if(pidl)
  1681.         {
  1682.  
  1683.           if(SHGetPathFromIDList(pidl, szDir)) {BaseDir->Text = szDir; }
  1684.  
  1685.             pShellMalloc->Free(pidl);
  1686.         }
  1687.         pShellMalloc->Release();
  1688.     }
  1689. }
  1690. //---------------------------------------------------------------------------
  1691.  
  1692. void __fastcall TForm1::Button4Click(TObject *Sender)
  1693. {
  1694.  if (IsConnect)
  1695.   {
  1696.     Memo3->Lines->Add(mysql_stat(MySQL));
  1697.   }
  1698. }
  1699. //---------------------------------------------------------------------------
  1700.  
  1701.  
  1702. void __fastcall TForm1::SpeedButton3Click(TObject *Sender)
  1703. {
  1704.   if(Showme1->Caption == "Show me")  {  TrayMessage(NIM_DELETE);
  1705.        Showme1->Caption = "Hide me";  Show();  }
  1706.   else  { TrayMessage(NIM_ADD);  TrayMessage(NIM_MODIFY);
  1707.        Showme1->Caption = "Show me";  Hide();  }
  1708. }
  1709. //---------------------------------------------------------------------------
  1710.  
  1711. void __fastcall TForm1::ExtendedClick(TObject *Sender)
  1712. {
  1713. if (ya)
  1714.  {
  1715.  Extended->Caption = "Start Extended Server Status";
  1716.  ya = false;
  1717.  ClearBox();
  1718.  }
  1719. else
  1720.  {
  1721.  Extended->Caption = "Stop Extended Server Status";
  1722.  ya = true;
  1723.  }
  1724. }
  1725. //---------------------------------------------------------------------------
  1726. void __fastcall TForm1::GetServerOptions(void)
  1727. {
  1728. AnsiString FileName;
  1729. FileName =  FileSearch("mysqld-opt.exe", ExtractFilePath(Application->ExeName));
  1730. if (FileName.IsEmpty()) {OptVer->Enabled = false; }
  1731.  
  1732. FileName =  FileSearch("mysqld-shareware.exe", ExtractFilePath(Application->ExeName));
  1733. if (FileName.IsEmpty()) {ShareVer->Enabled = false; }
  1734.  
  1735. FileName =  FileSearch("mysqld.exe", ExtractFilePath(Application->ExeName));
  1736. if (FileName.IsEmpty()) {MysqldVer->Enabled = false; }
  1737.  
  1738. FileName =  FileSearch("mysqld-nt.exe", ExtractFilePath(Application->ExeName));
  1739. if (FileName.IsEmpty()) {NtVer->Enabled = false; }
  1740.  
  1741. }
  1742. //---------------------------------------------------------------------------
  1743. void __fastcall TForm1::GetReportServer(void)
  1744. {
  1745.  
  1746.   AnsiString strspace;
  1747.   Memo5->Lines->Clear();
  1748.   Memo5->Lines->Add("This Report was made using the WinMySQLadmin 1.0 Tool");
  1749.   Memo5->Lines->Add("");
  1750.   Memo5->Lines->Add(Now());
  1751.   Memo5->Lines->Add("");
  1752.  
  1753.    preport = true;
  1754.    Memo5->Lines->Add("");
  1755.    Memo5->Lines->Add("Server Status Values");
  1756.    Memo5->Lines->Add("");
  1757.    Memo5->Lines->Add(GetString("Server Info") + mysql_get_server_info(MySQL));
  1758.    Memo5->Lines->Add(GetString("Host Info") + mysql_get_host_info(MySQL));
  1759.    Memo5->Lines->Add(GetString("Client Info") + mysql_get_client_info());
  1760.    Memo5->Lines->Add(GetString("Proto Info") + mysql_get_proto_info(MySQL));
  1761.    GetExtendedStatus();
  1762.    preport = false;
  1763.    treport = true;
  1764.    Memo5->Lines->Add("");
  1765.    Memo5->Lines->Add("Variables Values");
  1766.    Memo5->Lines->Add("");
  1767.    GetVariables();
  1768.    treport = false;
  1769.    ereport = true;
  1770.    Memo5->Lines->Add("");
  1771.    Memo5->Lines->Add("Last Lines from Err File");
  1772.    Memo5->Lines->Add("");
  1773.    SeekErrFile();
  1774.    ereport = false;
  1775.  
  1776. }
  1777.  
  1778. void __fastcall TForm1::SpeedButton4Click(TObject *Sender)
  1779. {
  1780.  if(IsConnect)
  1781.   GetReportServer();
  1782.  else
  1783.   Application->MessageBox("The Server must be connected", "WinMySQLadmin 1.0", MB_OK | MB_ICONEXCLAMATION);
  1784. }
  1785. //---------------------------------------------------------------------------
  1786.  
  1787. void __fastcall TForm1::SpeedButton5Click(TObject *Sender)
  1788. {
  1789.   AnsiString PathName;
  1790.   SaveFileDialog->FileName = PathName;
  1791.   if (SaveFileDialog->Execute() ){
  1792.     PathName= SaveFileDialog->FileName;
  1793.     Caption = ExtractFileName(PathName);
  1794.     Memo5->Lines->SaveToFile(PathName);
  1795.     Memo5->Modified = false;
  1796.   }
  1797. }
  1798. //---------------------------------------------------------------------------
  1799. String  __fastcall TForm1::GetString(String k)
  1800. {
  1801.   int i = 35 - k.Length();
  1802.   for (int y = 1 ; y <= i ;y++ )
  1803.      k+= " ";
  1804.    return k ;
  1805. }
  1806. //---------------------------------------------------------------------------
  1807. void __fastcall TForm1::SpeedButton6Click(TObject *Sender)
  1808. {
  1809.  PrinterSetupDialog1->Execute();
  1810. }
  1811. //---------------------------------------------------------------------------
  1812.  
  1813. void __fastcall TForm1::SpeedButton7Click(TObject *Sender)
  1814. {
  1815.   AnsiString PathName;
  1816.   if (PrintDialog1->Execute()){
  1817.     try {
  1818.         Memo5->Print(PathName);
  1819.     }
  1820.     catch(...){
  1821.         Printer()->EndDoc();
  1822.         throw;
  1823.     }
  1824.   }
  1825. }
  1826. //---------------------------------------------------------------------------
  1827.  
  1828. void __fastcall TForm1::SpeedButton8Click(TObject *Sender)
  1829. {
  1830.  Memo5->CutToClipboard();
  1831. }
  1832. //---------------------------------------------------------------------------
  1833.  
  1834. void __fastcall TForm1::SpeedButton9Click(TObject *Sender)
  1835. {
  1836.   Memo5->CopyToClipboard();
  1837. }
  1838. //---------------------------------------------------------------------------
  1839.  
  1840. void __fastcall TForm1::SpeedButton10Click(TObject *Sender)
  1841. {
  1842.  
  1843.  Memo5->PasteFromClipboard();
  1844. }
  1845. //---------------------------------------------------------------------------
  1846.  
  1847. void __fastcall TForm1::SpeedButton11Click(TObject *Sender)
  1848. {
  1849.  Memo5->ClearSelection();
  1850. }
  1851. //---------------------------------------------------------------------------
  1852.  
  1853. void __fastcall TForm1::SpeedButton12Click(TObject *Sender)
  1854. {
  1855.  Memo5->SelectAll();
  1856. }
  1857. //---------------------------------------------------------------------------
  1858. bool __fastcall TForm1::GetMainRoot()
  1859. {
  1860.  
  1861.  MYSQL_RES *res;
  1862.  MYSQL_ROW row;
  1863.  unsigned int i;
  1864.  AnsiString command;
  1865.  
  1866.  CleanGrid();
  1867.  CleanGridI();
  1868.  TakeIP();
  1869.  
  1870.  MySQLNode = DBView->Items->Add(NULL, mainroot.UpperCase());
  1871.  MySQLNode->ImageIndex = 0;
  1872.  
  1873.  if (!(res=mysql_list_dbs(MySQL,"%")))  { return false; }
  1874.    while ((row=mysql_fetch_row(res)) != 0) {
  1875.      mysql_field_seek(res,0);
  1876.  
  1877.      for (i=0 ; i < mysql_num_fields(res); i++)
  1878.       {
  1879.         MySQLDbs = DBView->Items->AddChild(MySQLNode, row[i]);
  1880.         MySQLDbs->ImageIndex = 1;
  1881.         MySQLDbs->SelectedIndex = 1;
  1882.  
  1883.  
  1884.       }
  1885.  
  1886.    }
  1887.  
  1888.    mysql_free_result(res);
  1889.    MySQLNode->Expanded = true;
  1890.  
  1891.  
  1892.  
  1893.  
  1894.  IsMySQLNode = true;
  1895.  return true;
  1896.  
  1897. }
  1898. //---------------------------------------------------------------------------
  1899. void __fastcall TForm1::DeleteDatabaseSClick(TObject *Sender)
  1900. {
  1901.  AnsiString alert;
  1902.  if (IsConnect)
  1903.   {
  1904.    if(DBView->Selected == MySQLNode )
  1905.     Application->MessageBox("Invalid database row selected.", "WinMySQLadmin 1.0", MB_OK | MB_ICONEXCLAMATION);
  1906.    else if ( DBView->Selected == NULL )
  1907.     Application->MessageBox("Invalid database row selected.", "WinMySQLadmin 1.0", MB_OK | MB_ICONEXCLAMATION);
  1908.    else
  1909.     {
  1910.      if (DBView->Selected->Text.UpperCase() == "MYSQL")
  1911.       Application->MessageBox("You cann't use this tool to drop the MySQL Database.", "WinMySQLadmin 1.0", MB_OK | MB_ICONEXCLAMATION);
  1912.      else {
  1913.        alert = "Are you sure to drop the < ";
  1914.        alert+= DBView->Selected->Text.c_str();
  1915.        alert+= " > database.";
  1916.       if(Application->MessageBox(alert.c_str(), "WinMySQLadmin 1.0", MB_YESNOCANCEL | MB_ICONQUESTION    ) == IDYES)
  1917.        {
  1918.          char* lese = DBView->Selected->Text.c_str();
  1919.         if (!mysql_drop_db(MySQL, lese ))
  1920.           {
  1921.             DBView->Items->Clear();
  1922.             GetMainRoot();
  1923.            }
  1924.          else
  1925.            Application->MessageBox("Fails to drop the Database.", "WinMySQLadmin 1.0", MB_OK | MB_ICONEXCLAMATION);
  1926.         }
  1927.       }
  1928.    }
  1929.   }
  1930.  else
  1931.    Application->MessageBox("The Server must be connected", "WinMySQLadmin 1.0", MB_OK | MB_ICONEXCLAMATION);
  1932. }
  1933. //---------------------------------------------------------------------------
  1934.  bool __fastcall TForm1::IsDatabase(String Name)
  1935. {
  1936.   MYSQL_RES *res;
  1937.   MYSQL_ROW row;
  1938.   unsigned int i;
  1939.   AnsiString command;
  1940.  
  1941.  
  1942.   CleanTree();
  1943.   command = "use ";
  1944.   command+= Name.c_str();
  1945.   char* das = command.c_str();
  1946.   char* lis = Name.c_str();
  1947.   if (mysql_query(MySQL, das ) ||
  1948.   !(res=mysql_list_tables(MySQL,"%")))
  1949.   return false;
  1950.  
  1951.   MySQLNodeT = TableView->Items->Add(NULL, lis);
  1952.   MySQLNodeT->ImageIndex = 1;
  1953.   MySQLNodeT->SelectedIndex = 1;
  1954.   while ((row=mysql_fetch_row(res)) != 0) {
  1955.   mysql_field_seek(res,0);
  1956.  
  1957.   for (i=0 ; i < mysql_num_fields(res); i++)
  1958.    {
  1959.  
  1960.      MySQLTbs = TableView->Items->AddChild(MySQLNodeT, row[i]);
  1961.      MySQLTbs->ImageIndex = 2;
  1962.      MySQLTbs->SelectedIndex = 2;
  1963.    }
  1964.    MySQLNodeT->Expanded = true;
  1965.   }
  1966.   mysql_free_result(res);
  1967.   return true;
  1968. }
  1969. //---------------------------------------------------------------------------
  1970.  
  1971.  
  1972. void __fastcall TForm1::DBViewClick(TObject *Sender)
  1973. {
  1974.  
  1975.  if (IsConnect)
  1976.   {
  1977.     if (DBView->Selected != MySQLNode && DBView->Selected != NULL  )
  1978.      {
  1979.        IsDatabase(DBView->Selected->Text);
  1980.  
  1981.      }
  1982.     else
  1983.      {
  1984.       CleanTree();
  1985.      }
  1986.   }
  1987. }
  1988. //---------------------------------------------------------------------------
  1989. void __fastcall TForm1::TableViewClick(TObject *Sender)
  1990. {
  1991.  if (IsConnect)
  1992.   {
  1993.     if (DBView->Selected != MySQLNodeT )
  1994.      {
  1995.        IsTable(TableView->Selected->Text);
  1996.        IsIndex(TableView->Selected->Text);
  1997.  
  1998.      }
  1999.     else
  2000.      {
  2001.       CleanGrid();
  2002.       CleanGridI();
  2003.  
  2004.      }
  2005.   }
  2006. }
  2007. //---------------------------------------------------------------------------
  2008.  bool __fastcall TForm1::IsTable(String Name)
  2009. {
  2010.  MYSQL_RES *res;
  2011.  MYSQL_ROW row;
  2012.  unsigned int i;
  2013.  int k = 0;
  2014.  int therow = 1;
  2015.  new_line=1;
  2016.  AnsiString command;
  2017.  AnsiString commandt;
  2018.  
  2019.  CleanGrid();
  2020.  CleanGridI();
  2021.  command = "use ";
  2022.  command+= DBView->Selected->Text.c_str();
  2023.  char* las = command.c_str();
  2024.  
  2025.  commandt = "desc ";
  2026.  commandt+= Name.c_str();
  2027.  char* les = commandt.c_str();
  2028.  
  2029.  if (mysql_query(MySQL, las ))
  2030.  return false;
  2031.  
  2032.  if (mysql_query(MySQL, les ) ||
  2033.  !(res=mysql_store_result(MySQL)))
  2034.  return false ;
  2035.  
  2036.  StringGrid4->Cells[0][0] = "Field";
  2037.  StringGrid4->Cells[1][0] = "Type";
  2038.  StringGrid4->Cells[2][0] = "Null";
  2039.  StringGrid4->Cells[3][0] = "Key";
  2040.  StringGrid4->Cells[4][0] = "Default";
  2041.  StringGrid4->Cells[5][0] = "Extra";
  2042.  StringGrid4->Cells[6][0] = "Previleges";
  2043.  
  2044.  
  2045.  int thecounter;
  2046.  String u = GetNumberServer();
  2047.  if ( u == "3.22")
  2048.   {
  2049.    StringGrid3->ColCount = 7;
  2050.    thecounter = 4;
  2051.   }
  2052.  else
  2053.   thecounter = 5;
  2054.  
  2055.  while ((row=mysql_fetch_row(res)) != 0)
  2056.  {
  2057.    mysql_field_seek(res,0);
  2058.  
  2059.    for (i=0 ; i < mysql_num_fields(res); i++)
  2060.     {
  2061.      if (k <= thecounter )
  2062.      {
  2063.        StringGrid4->Cells[k][therow] = row[i];
  2064.        k++;
  2065.      }
  2066.      else
  2067.      {
  2068.        StringGrid4->Cells[(k)][therow] = row[i];
  2069.        k = 0;
  2070.        therow++ ;
  2071.        StringGrid4->RowCount++;
  2072.       }
  2073.     }
  2074.  
  2075.  }
  2076.  StringGrid4->RowCount--;
  2077.  mysql_free_result(res);
  2078.  return true;
  2079. }
  2080. //---------------------------------------------------------------------------
  2081. void __fastcall TForm1::TableViewChange(TObject *Sender, TTreeNode *Node)
  2082. {
  2083. if (IsConnect)
  2084.   {
  2085.     if (DBView->Selected != MySQLNodeT )
  2086.      {
  2087.        IsTable(TableView->Selected->Text);
  2088.        IsIndex(TableView->Selected->Text);
  2089.  
  2090.      }
  2091.     else
  2092.      {
  2093.       CleanGrid();
  2094.       CleanGridI();
  2095.  
  2096.      }
  2097.   }
  2098. }
  2099. //---------------------------------------------------------------------------
  2100. void __fastcall TForm1::DBViewChange(TObject *Sender, TTreeNode *Node)
  2101. {
  2102.  if (IsConnect)
  2103.   {
  2104.     if (DBView->Selected != MySQLNode )
  2105.      {
  2106.        IsDatabase(DBView->Selected->Text);
  2107.  
  2108.      }
  2109.     else
  2110.      {
  2111.       CleanTree();
  2112.      }
  2113.   }
  2114.  
  2115. }
  2116. //---------------------------------------------------------------------------
  2117. void __fastcall TForm1::RefreshSClick(TObject *Sender)
  2118. {
  2119.  MYSQL_RES *res;
  2120.  MYSQL_ROW row;
  2121.  unsigned int i;
  2122.  AnsiString command;
  2123.  
  2124.  if (IsConnect)
  2125.   {
  2126.    IsMySQLNode = false;
  2127.    CleanTree();
  2128.    DBView->Items->Clear();
  2129.  
  2130.    TakeIP();
  2131.  
  2132.    MySQLNode = DBView->Items->Add(NULL, mainroot.UpperCase());
  2133.    MySQLNode->ImageIndex = 0;
  2134.  
  2135.    if (!(res=mysql_list_dbs(MySQL,"%")))  { /*do nothing;*/ }
  2136.     while ((row=mysql_fetch_row(res)) != 0) {
  2137.     mysql_field_seek(res,0);
  2138.  
  2139.      for (i=0 ; i < mysql_num_fields(res); i++)
  2140.       {
  2141.         MySQLDbs = DBView->Items->AddChild(MySQLNode, row[i]);
  2142.         MySQLDbs->ImageIndex = 1;
  2143.         MySQLDbs->SelectedIndex = 1;
  2144.  
  2145.       }
  2146.  
  2147.    }
  2148.  
  2149.    mysql_free_result(res);
  2150.  
  2151.  IsMySQLNode = true;
  2152.  
  2153.  MySQLNode->Expanded = true;
  2154.  
  2155.   }
  2156. }
  2157. //---------------------------------------------------------------------------
  2158.  
  2159. void __fastcall TForm1::CreateDatabaseSClick(TObject *Sender)
  2160. {
  2161.  
  2162.  if (IsConnect)
  2163.   {
  2164.    dbfrm->Show();
  2165.  
  2166.   }
  2167.  else
  2168.   ShowMessage("Precisa estar conectado");
  2169. }
  2170. //---------------------------------------------------------------------------
  2171. void __fastcall TForm1::CleanTree(void)
  2172. {
  2173.   StringGrid4->RowCount= 2;
  2174.   StringGrid4->Cells[0][1] = "";
  2175.   StringGrid4->Cells[1][1] = "";
  2176.   StringGrid4->Cells[2][1] = "";
  2177.   StringGrid4->Cells[3][1] = "";
  2178.   StringGrid4->Cells[4][1] = "";
  2179.   StringGrid4->Cells[5][1]  = "";
  2180.   TableView->Items->Clear();
  2181.  
  2182. }
  2183. //---------------------------------------------------------------------------
  2184. void __fastcall TForm1::CleanGrid(void)
  2185. {
  2186.   StringGrid4->RowCount= 2;
  2187.   StringGrid4->Cells[0][1] = "";
  2188.   StringGrid4->Cells[1][1] = "";
  2189.   StringGrid4->Cells[2][1] = "";
  2190.   StringGrid4->Cells[3][1] = "";
  2191.   StringGrid4->Cells[4][1] = "";
  2192.   StringGrid4->Cells[5][1]  = "";
  2193. }
  2194. //---------------------------------------------------------------------------
  2195. bool __fastcall TForm1::CreatingDB()
  2196. {
  2197.  
  2198.  if (mysql_create_db(MySQL, dbfrm->Edit1->Text.c_str()))
  2199.   return true;
  2200.  else
  2201.   return false;
  2202. }
  2203. //---------------------------------------------------------------------------
  2204. void __fastcall TForm1::OutRefresh(void)
  2205. {
  2206.  RefreshSClick(dbfrm->SpeedButton1);
  2207. }
  2208. //---------------------------------------------------------------------------
  2209. void __fastcall TForm1::FlushHosts1Click(TObject *Sender)
  2210. {
  2211.    if (IsConnect)
  2212.    {
  2213.     if (mysql_refresh(MySQL,REFRESH_HOSTS))
  2214.        StatusLine->SimpleText = "";
  2215.    }
  2216. }
  2217. //---------------------------------------------------------------------------
  2218.  
  2219. void __fastcall TForm1::FlushLogs1Click(TObject *Sender)
  2220. {
  2221.    if (IsConnect)
  2222.   {
  2223.     if (mysql_refresh(MySQL,REFRESH_LOG))
  2224.      StatusLine->SimpleText = "";
  2225.   }
  2226. }
  2227. //---------------------------------------------------------------------------
  2228.  
  2229. void __fastcall TForm1::FlushTables1Click(TObject *Sender)
  2230. {
  2231.   if (IsConnect)
  2232.   {
  2233.     if (mysql_refresh(MySQL,REFRESH_TABLES))
  2234.       StatusLine->SimpleText = "";
  2235.    }
  2236. }
  2237. //---------------------------------------------------------------------------
  2238. //---------------------------------------------------------------------------
  2239.  bool __fastcall TForm1::IsIndex(String Name)
  2240. {
  2241.  MYSQL_RES *res;
  2242.  MYSQL_ROW row;
  2243.  unsigned int i;
  2244.  int k = 0;
  2245.  int therow = 1;
  2246.  new_line=1;
  2247.  AnsiString command;
  2248.  AnsiString commandt;
  2249.  i = 0;
  2250.  CleanGridI();
  2251.  command = "use ";
  2252.  command+= DBView->Selected->Text.c_str();
  2253.  char* las = command.c_str();
  2254.  
  2255.  commandt = "show index from ";
  2256.  commandt+= Name.c_str();
  2257.  char* les = commandt.c_str();
  2258.  
  2259.  if (mysql_query(MySQL, las ))
  2260.  return false;
  2261.  
  2262.  if (mysql_query(MySQL, les ) ||
  2263.  !(res=mysql_store_result(MySQL)))
  2264.  return false ;
  2265.  
  2266.  StringGrid3->RowCount= 2;
  2267.  StringGrid3->Cells[0][0] = "Table";
  2268.  StringGrid3->Cells[1][0] = "Non_unique";
  2269.  StringGrid3->Cells[2][0] = "Key_name";
  2270.  StringGrid3->Cells[3][0] = "Seq_in_index";
  2271.  StringGrid3->Cells[4][0] = "Col_name";
  2272.  StringGrid3->Cells[5][0] = "Collation";
  2273.  StringGrid3->Cells[6][0] = "Card.";
  2274.  StringGrid3->Cells[7][0] = "Sub_part";
  2275.  StringGrid3->Cells[8][0] = "Packed";
  2276.  StringGrid3->Cells[9][0] = "Comment";
  2277.  
  2278.  int thecounter;
  2279.  String u = GetNumberServer();
  2280.  
  2281.  if ( u == "3.22")
  2282.   {
  2283.    StringGrid3->ColCount = 8;
  2284.    thecounter = 6;
  2285.   }
  2286.  else
  2287.   thecounter = 8;
  2288.  while ((row=mysql_fetch_row(res)) != 0)
  2289.  {
  2290.    mysql_field_seek(res,0);
  2291.  
  2292.    for (i=0 ; i < mysql_num_fields(res); i++)
  2293.     {
  2294.      if (k <= thecounter )
  2295.      {
  2296.        StringGrid3->Cells[k][therow] = row[i];
  2297.        k++;
  2298.      }
  2299.      else
  2300.      {
  2301.        StringGrid3->Cells[(k)][therow] = row[i];
  2302.        k = 0;
  2303.        therow++ ;
  2304.        StringGrid3->RowCount++;
  2305.       }
  2306.     }
  2307.  
  2308.  }
  2309.  if (i)
  2310.  StringGrid3->RowCount--;
  2311.  mysql_free_result(res);
  2312.  return true;
  2313. }
  2314. //---------------------------------------------------------------------------
  2315. void __fastcall TForm1::CleanGridI(void)
  2316. {
  2317.   StringGrid3->RowCount= 2;
  2318.   StringGrid3->Cells[0][1] = "";
  2319.   StringGrid3->Cells[1][1] = "";
  2320.   StringGrid3->Cells[2][1] = "";
  2321.   StringGrid3->Cells[3][1] = "";
  2322.   StringGrid3->Cells[4][1] = "";
  2323.   StringGrid3->Cells[5][1]  = "";
  2324.   StringGrid3->Cells[6][1]  = "";
  2325.   StringGrid3->Cells[7][1]  = "";
  2326. }
  2327. //---------------------------------------------------------------------------
  2328. bool __fastcall TForm1::CreatingTable(String TheTable)
  2329. {
  2330.  
  2331.  if (!mysql_query(MySQL, TheTable.c_str()))
  2332.   return true;
  2333.  else
  2334.   return false;
  2335. }
  2336. //---------------------------------------------------------------------------
  2337. bool __fastcall TForm1::GetExtendedStatus()
  2338. {
  2339.   if (!ya && !preport)
  2340.    return true;
  2341.  
  2342.   MYSQL_RES *res;
  2343.   MYSQL_ROW row;
  2344.   unsigned int i;
  2345.   int k = 1;
  2346.   new_line=1;
  2347.   bool left = true;
  2348.   bool open_tables = false;
  2349.   bool open_files = false;
  2350.   bool uptime = false;
  2351.   bool running_threads = false;
  2352.   bool open_streams = false;
  2353.   bool slow_queries = false;
  2354.   bool opened_tables = false;
  2355.   bool questions = false;
  2356.  
  2357.   AnsiString report;
  2358.   if (yy)
  2359.   StringGrid5->RowCount = 2;
  2360.  
  2361.    if (mysql_query(MySQL,"show status") ||
  2362.       !(res=mysql_store_result(MySQL)))
  2363.       {
  2364.         return false;
  2365.       }
  2366.  
  2367.       while ((row=mysql_fetch_row(res)) != 0)
  2368.        {
  2369.          mysql_field_seek(res,0);
  2370.  
  2371.          StringGrid5->Cells[0][0] = "Variable Name";
  2372.          StringGrid5->Cells[1][0] = "Value";
  2373.  
  2374.  
  2375.          for (i=0 ; i < mysql_num_fields(res); i++)
  2376.           {
  2377.  
  2378.              if (left)
  2379.               {
  2380.                  if (preport)
  2381.                  report = GetString(row[i]);
  2382.                  if ( (String) row[i] ==  "Open_tables")
  2383.                    open_tables = true;
  2384.                  else
  2385.                    open_tables = false;
  2386.                  if ( (String) row[i] ==  "Open_files")
  2387.                    open_files = true;
  2388.                  else
  2389.                    open_files = false;
  2390.                  if ((String) row[i] == "Uptime")
  2391.                    uptime = true;
  2392.                  else
  2393.                    uptime = false;
  2394.  
  2395.                  if ( (String) row[i] == "Opened_tables")
  2396.                     opened_tables = true;
  2397.                  else
  2398.                     opened_tables = false;
  2399.  
  2400.                  if ( (String) row[i] == "Threads_running" || (String) row[i] == "Running_threads")
  2401.                     running_threads = true;
  2402.                  else
  2403.                     running_threads = false;
  2404.  
  2405.                  if ( (String) row[i] == "Open_streams")
  2406.                     open_streams = true;
  2407.                  else
  2408.                     open_streams = false;
  2409.  
  2410.                  if ( (String) row[i] == "Slow_queries")
  2411.                     slow_queries = true;
  2412.                  else
  2413.                     slow_queries = false;
  2414.  
  2415.                  if ( (String) row[i] == "Questions")
  2416.                     questions = true;
  2417.                  else
  2418.                     questions = false;
  2419.  
  2420.                  if (yy)
  2421.                  StringGrid5->Cells[0][k++] = row[i];
  2422.  
  2423.                  left = false;
  2424.               }
  2425.              else
  2426.                {
  2427.                  if (preport)
  2428.                   Memo5->Lines->Add(report + row[i]);
  2429.                  if (open_tables)
  2430.                   st22->Text = row[i];
  2431.                  if (open_files)
  2432.                   st23->Text = row[i];
  2433.                  if (uptime)
  2434.                   nice_time(row[i]);
  2435.                  if (running_threads)
  2436.                   st27->Text = row[i];
  2437.                  if (open_streams)
  2438.                   st24->Text = row[i];
  2439.                  if (slow_queries)
  2440.                   st28->Text = row[i];
  2441.                  if (opened_tables)
  2442.                   st25->Text = row[i];
  2443.                  if (questions){
  2444.                    q++;
  2445.                    st26->Text = StrToInt64(row[i]) - q; }
  2446.  
  2447.                  if (yy){
  2448.                  StringGrid5->RowCount++;
  2449.                  StringGrid5->Cells[1][--k] = row[i];
  2450.                  k++; }
  2451.  
  2452.                  left = true;
  2453.                }
  2454.  
  2455.           }
  2456.  
  2457.        }
  2458.  
  2459.  
  2460.     if (rinit)
  2461.      StringGrid5->RowCount--;
  2462.     mysql_free_result(res);
  2463.     yy = false;
  2464.     return true;
  2465. }
  2466. //---------------------------------------------------------------------------
  2467. void __fastcall TForm1::SpeedButton13Click(TObject *Sender)
  2468. {
  2469.   yy = true;
  2470.  // rinit = true;
  2471. }
  2472. //---------------------------------------------------------------------------
  2473. String  __fastcall TForm1::GetNumberServer()
  2474. {
  2475.  String TheVersion;
  2476.  
  2477.  TheVersion  =  mysql_get_server_info(MySQL) ;
  2478.  TheVersion.SetLength(4);
  2479.  return TheVersion;
  2480.  
  2481.  
  2482. }
  2483.  
  2484. //---------------------------------------------------------------------------
  2485. void __fastcall TForm1::KillProcess1Click(TObject *Sender)
  2486. {
  2487.  
  2488.   if (IsConnect)
  2489.     KillPID();
  2490.   else
  2491.     Application->MessageBox("The Server must be connected", "WinMySQLadmin 1.0", MB_OK | MB_ICONEXCLAMATION);
  2492. }
  2493. //---------------------------------------------------------------------------
  2494. bool __fastcall TForm1::KillPID()
  2495. {
  2496.   String s  = "Are you sure to kill the process PID no. ";
  2497.   s+= StringGrid2->Cells[0][StringGrid2->Row];
  2498.   s+= " of the USER ";
  2499.   s+= StringGrid2->Cells[1][StringGrid2->Row];
  2500.   unsigned long  xx = mysql_thread_id(MySQL);
  2501.   unsigned long  yy = StrToInt(StringGrid2->Cells[0][StringGrid2->Row]);
  2502.   if ( xx != yy)
  2503.    {
  2504.      if(Application->MessageBox(s.c_str(), "WinMySQLadmin 1.0", MB_YESNOCANCEL | MB_ICONQUESTION    ) == IDYES)
  2505.       {
  2506.         if (!mysql_kill(MySQL,yy))
  2507.          {
  2508.           GetProcess();
  2509.           return true;
  2510.          }
  2511.       }
  2512.     }
  2513.   else
  2514.    {
  2515.      Application->MessageBox("From here you can't kill the PID of this tool", "WinMySQLadmin 1.0", MB_OK | MB_ICONEXCLAMATION);
  2516.      return true;
  2517.    }
  2518.  return true;
  2519. }
  2520. void __fastcall TForm1::FlushThreads1Click(TObject *Sender)
  2521. {
  2522.  if (IsConnect)
  2523.   {
  2524.     if (mysql_refresh(MySQL,REFRESH_THREADS))
  2525.       StatusLine->SimpleText = "";
  2526.    }
  2527. }
  2528. //---------------------------------------------------------------------------
  2529.  
  2530.