Solved

Mysql Pendrive data and service

Posted on 2013-07-01
9
718 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
 

Author Comment

by:danz67
ID: 39293506
Very good, for complete all i need code to start and stop MySql service.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

I have been using r1soft Continuous Data Protection (http://www.r1soft.com/linux-cdp/) for many years now with the mySQL Addon and wanted to share a trick I have used several times. For those of us that don't have the luxury of using all transact…
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

758 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

19 Experts available now in Live!

Get 1:1 Help Now