Solved

Mysql Pendrive data and service

Posted on 2013-07-01
9
721 Views
Last Modified: 2013-07-02
Hello,

I want to install Mysql and data, on a pendrive and the service must be started and stopped by application made in Delphi or automatically when I insert or disconnect the pendrive into the USB port.
I found a way to move the databases and modify the my.ini file but do not know how to start the service.
Seeking a solution, thanks
0
Comment
Question by:danz67
  • 6
  • 3
9 Comments
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 39292447
Assuming that you know how to start e service from delphi, you can create an autorun.inf that points to the startservice application on usb pen drive
So when you'll plug in the pen drive, the autorun.inf will execute the start service program

Another way is to create a service or application running in background to listen for the WMDeviceChange message. From lParam you can see if the device is a usb pen drive and  depending on the wParam you will know if the device was plugged in or out  and so start or stop the mysql service from there
0
 

Author Comment

by:danz67
ID: 39292654
Good, you have a sample for make?
0
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 39293160
Well, here's my little elaboration of what can be found easly googling around.

This code will detect a pen drive and the assigned drive, when inserted or removed.
You could just implement the code for start/stop the mysql service here

Create a new form and paste this code into the unit. No component is needed. Just double click on Form.oncreate and form.onclose to assign the events correctly. Finally run it and try plugging and unplagging a pen drive to see the effect

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
    FWindowHandle: HWND;
    procedure WndProc(var Msg: TMessage);
    function USBRegister: Boolean;
    procedure WMDeviceChange(var Msg: TMessage); dynamic;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  UsbMsg: String;

implementation

{$R *.dfm}

type
  PDevBroadcastHdr = ^DEV_BROADCAST_HDR;

  DEV_BROADCAST_HDR = packed record
    dbch_size: DWORD;
    dbch_devicetype: DWORD;
    dbch_reserved: DWORD;
  end;

  PDevBroadcastDeviceInterface = ^DEV_BROADCAST_DEVICEINTERFACE;

  DEV_BROADCAST_DEVICEINTERFACE = record
    dbcc_size: DWORD;
    dbcc_devicetype: DWORD;
    dbcc_reserved: DWORD;
    dbcc_classguid: TGUID;
    dbcc_name: short;
  end;

  PDevBroadcastVolume = ^TDevBroadcastVolume;

  TDevBroadcastVolume = packed record
    dbcv_size: DWORD;
    dbcv_devicetype: DWORD;
    dbcv_reserved: DWORD;
    dbcv_unitmask: DWORD;
    dbcv_flags: Word;
  end;

const
  GUID_DEVINTERFACE_USB_DEVICE: TGUID = '{A5DCBF10-6530-11D2-901F-00C04FB951ED}';
  DBT_DEVICEARRIVAL = $8000; // system   detected   a   new   device
  DBT_DEVICEREMOVECOMPLETE = $8004; // device   is   gone
  DBT_DEVTYP_DEVICEINTERFACE = $00000005;
  DBTF_MEDIA = $0001;
  DBT_DEVTYP_VOLUME = $0002;

  // device   interface   class
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  DeallocateHWnd(FWindowHandle);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  FWindowHandle := AllocateHWnd(WndProc);
  USBRegister;
end;

Procedure TForm1.WndProc(var Msg: TMessage);
begin
  if (Msg.Msg = WM_DEVICECHANGE) then
  begin
    try
      WMDeviceChange(Msg);
    except
      Application.HandleException(Self);
    end;
  end
  else
    Msg.Result := DefWindowProc(FWindowHandle, Msg.Msg, Msg.wParam, Msg.lParam);
end;

Procedure TForm1.WMDeviceChange(var Msg: TMessage);
var
  devType: Integer;
  Datos: PDevBroadcastHdr;
  pData: PDevBroadcastDeviceInterface;
  Drive: string;
  function GetDrive(pDBVol: PDevBroadcastVolume): string;
  var
    i: Byte;
    Maske: DWORD;
  begin
    Maske := pDBVol^.dbcv_unitmask;
    for i := 0 to 25 do
    begin
      if (Maske and 1) = 1 then
        Result := Char(i + Ord('A'));
      Maske := Maske shr 1;
    end;
  end;

begin
  if (Msg.wParam = DBT_DEVICEARRIVAL) or (Msg.wParam = DBT_DEVICEREMOVECOMPLETE) then
  begin
    Datos := PDevBroadcastHdr(Msg.lParam);
    devType := Datos^.dbch_devicetype;
    case devType of
      DBT_DEVTYP_DEVICEINTERFACE:
        begin // USB   Device
          pData := PDevBroadcastDeviceInterface(Msg.lParam);
          if Msg.wParam = DBT_DEVICEARRIVAL then
            UsbMsg := 'USB Device inserted. Assigned to drive %s:'
            // add here your code to start MySql service
          else
            UsbMsg := 'USB Device removed. Drive %s: no more available'
            // add here your code to stop MySql service
        end;
      DBT_DEVTYP_VOLUME:
        begin
          Drive := GetDrive(PDevBroadcastVolume(Msg.lParam));
          ShowMessage(Format(UsbMsg, [Drive]));
        end;
    end;
  end;
end;

function TForm1.USBRegister: Boolean;
var
  dbi: DEV_BROADCAST_DEVICEINTERFACE;
  Size: Integer;
  r: Pointer;
begin
  Result := False;
  Size := SizeOf(DEV_BROADCAST_DEVICEINTERFACE);
  ZeroMemory(@dbi, Size);
  dbi.dbcc_size := Size;
  dbi.dbcc_devicetype := DBT_DEVTYP_DEVICEINTERFACE;
  dbi.dbcc_reserved := 0;
  dbi.dbcc_classguid := GUID_DEVINTERFACE_USB_DEVICE;
  dbi.dbcc_name := 0;
  r := RegisterDeviceNotification(FWindowHandle, @dbi, DEVICE_NOTIFY_WINDOW_HANDLE);
  if Assigned(r) then
    Result := True;
end;

end.

Open in new window

0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

Author Comment

by:danz67
ID: 39293506
Very good, for complete all i need code to start and stop MySql service.
0
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 39293601
That's another q.
One q. about detecting pen drive in/out
Another q. for start/stop a service

That's the ExEx policy
0
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 39293628
Anyway there's an easy way to start/stop mysql

winexec('net start mysql',0);

winexec('net stop mysql',0);

Just a gift for this time ;)
0
 

Author Comment

by:danz67
ID: 39293954
well, then you can explain to me what I have to write to the file autorun.inf to start the service mysql? I'll be happy to accept your answer, thanks
0
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 39294142
I must explain again better.
Autoran.inf is a way, but it should work just to start the mysql service, because when you'll unplug the pen drive nothing other could be execute from there and the device remove message couldn't be listened.

What I've suggested is to create an application that run on the machine to listen for the usb device plug/unplug and so start/stop the mysql service.
I guess that you are distributin an application that can work with mysql databases installed on a pen drive, so that application could listen itself for the usb device changes and then start/stop mysql
0
 
LVL 22

Accepted Solution

by:
Ferruccio Accalai earned 250 total points
ID: 39294177
Or do you need that all (Mysql, application and Database) stays just on the pen drive?

In this case there's a project that does exactly this.

Take a look here (it's an italian page, but you could translate what you need to understand)

http://silverpeacock.wordpress.com/2008/09/15/start-stop-mysql-delphi-pendrive/
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now