unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, winsock; type TForm1 = class(TForm) bc: TButton; l1: TLabel; ehost: TEdit; Label1: TLabel; Label2: TLabel; Label3: TLabel; Label4: TLabel; euser: TEdit; epassword: TEdit; emasterdb: TEdit; procedure bcClick(Sender: TObject); private { Déclarations privées } public { Déclarations publiques } procedure connexion; end; var Form1: TForm1; type my_bool = byte; gptr = pansiChar; my_ulonglong = int64; const MYSQL_ERRMSG_SIZE = 200; type my_socket = TSocket; type PVio = ^TVio; TVio = record end; type PNET = ^TNET; TNET = record vio: PVio; fd: my_socket; fcntl: longint; buff, buff_end, write_pos, read_pos: pByte; last_error: array [0..MYSQL_ERRMSG_SIZE - 1] of char; last_errno, max_packet, timeout, pkt_nr: longword; error: byte; return_errno, compress: my_bool; no_send_ok: my_bool; // needed if we are doing several // queries in one command ( as in LOAD TABLE ... FROM MASTER ), // and do not want to confuse the client with OK at the wrong time remain_in_buf, length, buf_length, where_b: longword; return_status: pLongword; reading_or_writing: byte; save_char: char; end; type mysql_status = ( MYSQL_STATUS_READY, MYSQL_STATUS_GET_RESULT, MYSQL_STATUS_USE_RESULT ); const FIELD_TYPE_DECIMAL = 0; FIELD_TYPE_TINY = 1; FIELD_TYPE_SHORT = 2; FIELD_TYPE_LONG = 3; FIELD_TYPE_FLOAT = 4; FIELD_TYPE_DOUBLE = 5; FIELD_TYPE_NULL = 6; FIELD_TYPE_TIMESTAMP = 7; FIELD_TYPE_LONGLONG = 8; FIELD_TYPE_INT24 = 9; FIELD_TYPE_DATE = 10; FIELD_TYPE_TIME = 11; FIELD_TYPE_DATETIME = 12; FIELD_TYPE_YEAR = 13; FIELD_TYPE_NEWDATE = 14; FIELD_TYPE_ENUM = 247; FIELD_TYPE_SET = 248; FIELD_TYPE_TINY_BLOB = 249; FIELD_TYPE_MEDIUM_BLOB = 250; FIELD_TYPE_LONG_BLOB = 251; FIELD_TYPE_BLOB = 252; FIELD_TYPE_VAR_STRING = 253; FIELD_TYPE_STRING = 254; type enum_field_types = FIELD_TYPE_DECIMAL..FIELD_TYPE_STRING; type PMYSQL_FIELD = ^TMYSQL_FIELD; TMYSQL_FIELD = record name: pansiChar; // Name of column table: pansiChar; // Table of column if column was a field org_table : pansiChar; db : pansiChar; def: pansiChar; // Default value (set by mysql_list_fields) length: longword; // Width of column max_length: longword; // Width of column flags: longword; // Div flags decimals: longword; // Number of decimals in field _type: enum_field_types; // Type of field. Se mysql_com.h for types end; type PMYSQL_FIELDS = ^TMYSQL_FIELDS; TMYSQL_FIELDS = array[0..MaxInt div SizeOf(TMYSQL_FIELD) - 1] of TMYSQL_FIELD; type PUSED_MEM = ^TUSED_MEM; // struct for once_alloc TUSED_MEM = record next: PUSED_MEM; // Next block in use left: longword; // memory left in block size: longword; // size of block end; type error_proc = procedure; type PMEM_ROOT = ^TMEM_ROOT; TMEM_ROOT = record free: PUSED_MEM; used: PUSED_MEM; pre_alloc: PUSED_MEM; min_malloc: longword; block_size: longword; error_handler: error_proc; end; type PMYSQL_OPTIONS = ^TMYSQL_OPTIONS; TMYSQL_OPTIONS = record connect_timeout, client_flag: longword; compress, named_pipe: my_bool; port: longword; host, init_command, user, password, unix_socket, db: pansiChar; my_cnf_file, my_cnf_group, charset_dir, charset_name: pansiChar; use_ssl: my_bool; // if to use SSL or not ssl_key: pansiChar; // PEM key file ssl_cert: pansiChar; // PEM cert file ssl_ca: pansiChar; // PEM CA file ssl_capath: pansiChar; // PEM directory of CA-s? end; type PCHARSET_INFO = ^TCHARSET_INFO; TCHARSET_INFO = record // Omitted: Structure not necessarily needed. // Definition of struct charset_info_st can be // found in include/m_ctype.h end; type PMYSQL = ^TMYSQL; TMYSQL = record net: TNET; // Communication parameters connector_fd: gptr; // ConnectorFd for SSL host, user, passwd, unix_socket, server_version, host_info, info, db: pansiChar; port, client_flag, server_capabilities: longword; protocol_version: longword; field_count: longword; server_status: longword; thread_id: longword; // Id for connection in server affected_rows: my_ulonglong; insert_id: my_ulonglong; // id if insert on table with NEXTNR extra_info: my_ulonglong; // Used by mysqlshow packet_length: longword; status: mysql_status; fields: PMYSQL_FIELDS; field_alloc: TMEM_ROOT; free_me: my_bool; // If free in mysql_close reconnect: my_bool; // set to 1 if automatic reconnect options: TMYSQL_OPTIONS; scramble_buff: array [0..8] of char; charset: PCHARSET_INFO; server_language: longword; end; type mysql_option = ( MYSQL_OPT_CONNECT_TIMEOUT, MYSQL_OPT_COMPRESS, MYSQL_OPT_NAMED_PIPE, MYSQL_INIT_COMMAND, MYSQL_READ_DEFAULT_FILE, MYSQL_READ_DEFAULT_GROUP, MYSQL_SET_CHARSET_DIR, MYSQL_SET_CHARSET_NAME, MYSQL_OPT_LOCAL_INFILE ); function mysql_init(_mysql: PMYSQL): PMYSQL; stdcall; external 'libmysql.dll' name 'mysql_init'; function mysql_options(_mysql: PMYSQL; option: mysql_option; const arg: pansiChar): longint; stdcall; external 'libmysql.dll' name 'mysql_options'; function mysql_real_connect(_mysql: PMYSQL; const host, user, passwd, db: pansiChar; port: longword; const unix_socket: pansiChar; clientflag: longword): PMYSQL; external 'libmysql.dll' name 'mysql_real_connect'; implementation {$R *.dfm} procedure TForm1.bcClick(Sender: TObject); begin connexion; end; procedure Tform1.connexion; Var portip: integer; // myres: PMYSQL_RES; // myrow: PMYSQL_ROW; // req: string; // i,j, debut, sep:integer; var host, masterdb, user, password: ansistring; phost, pmasterdb, puser, ppassword: pansichar; mysqlc: pmysql; begin host:=ansistring(ehost.text); user:=ansistring(euser.Text); password:=ansistring(epassword.Text); masterdb:=ansistring(emasterdb.Text); phost:=pansichar(host); puser:=pansichar(user); ppassword:=pansichar(password); pmasterdb:=pansichar(masterdb); portip:=3306; MySQLC := mysql_init(nil); if mysqlc<>nil then begin mysql_options(mysqlC,MYSQL_OPT_COMPRESS,nil); //plusieurs tentatives de syntaxe mais toutes arrivent au même résultat if mysql_real_connect(mysqlC, phost, puser, ppassword, pmasterdb, portip, nil, 0)=nil then //if mysql_real_connect(mysqlC, pansiChar(ansistring(ehost.text)), // pansiChar(ansistring(euser.text)), // pansiChar(ansistring(epassword.text)), // pansichar(ansistring(emasterdb.text)), // portip, nil, 0)=nil then begin l1.caption:='La connexion a echouée'; exit; end else begin l1.caption:='Connexion OK'; end; end; end; end.