Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Mysql Pendrive data and service

Posted on 2013-07-01
9
Medium Priority
?
734 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 23

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 23

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
Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 

Author Comment

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

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 23

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 23

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 23

Accepted Solution

by:
Ferruccio Accalai earned 1000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

604 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