Solved

Mysql Pendrive data and service

Posted on 2013-07-01
9
723 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

726 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