|
|
Delphi: AjpdSoft Conectar unidad de red Código fuente Delphi
![]() Nueva aplicación desarrollada por AjpdSoft que permite conectar unidades de red a partir de una ruta de red (nombre de servidor - nombre del recurso compartido). Esta aplicación también permite realizar la desconexión de una unidad de red existente. La aplicación ha sido desarrollada en Borland (ahora Codegear) Delphi 6, es 100% Open Source por lo que podréis descargar el código fuente aquí.
Características más importantes de AjpdSoft Conectar unidad de redHemos añadido esta nueva aplicación a nuestro proyecto AjpdSoft, desarrollada por nuestros técnicos. AjpdSoft Conectar unidad de red permite conectar y desconectar unidades de red en segundos. Permite guardar los últimos valores utilizados. Esta aplicación puede conectar una unidad de red (o desconectar) de forma automática y transparente para el usuario. Tiene las siguientes características:
Algunas opciones de AjpdSoft Conectar unidad de redA continuación os mostramos captura de pantalla de las opciones más importantes de la aplicación:
Si todo es correcto tras desconectarla la aplicación nos mostrará este mensaje:
Datos técnicos de AjpdSoft Conectar unidad de redLa aplicación está desarrollada en Borland (Codegear) Delphi 6, los datos de configuración son guardados en un fichero de texto plano con formato de fichero INI. Para la encriptación de la contraseña se ha utilizado el algoritmo AES. Para usuarios desarrolladoresLa aplicación utiliza los siguientes componentes (para Delphi 6):
Descarga de la aplicación completa gratuitaPor supuesto, como siempre, incluimos el código fuente 100% Open Source (freeware) gratis de la aplicación completa. Si aún no te has registrado puedes hacerlo desde aquí gratuitamente, luego descarga el código fuente desde este enlace: Código fuente Source Code Open Source de AjpdSoft Conectar unidad de red
AnexoListado del código fuente (source code) en Delphi 6A continuación os mostramos el listadod el código fuente (en Borland Delphi 6) de la aplicación AjpdSoft Conectar unidad de red: unit UnidadMenuPrincipal; {$R WinXP.res} interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Buttons, shellapi, aes, base64, inifiles, ThemeMgr; type TformMenuPrincipal = class(TForm) GroupBox1: TGroupBox; Label1: TLabel; txtRuta: TEdit; Label2: TLabel; txtUsuario: TEdit; txtContrasena: TEdit; Label3: TLabel; bConectar: TBitBtn; LWEB: TLabel; Label4: TLabel; lsLetra: TComboBox; GroupBox2: TGroupBox; bDesconectar: TBitBtn; lsLetraDesconectar: TComboBox; Label5: TLabel; opReconectar: TCheckBox; ThemeManager1: TThemeManager; GroupBox3: TGroupBox; opConectarUnidadInicio: TCheckBox; opDesconectarInicio: TCheckBox; opMostrarAvisos: TCheckBox; opCerrarAlIniciar: TCheckBox; Label6: TLabel; procedure LWEBClick(Sender: TObject); procedure bConectarClick(Sender: TObject); procedure bDesconectarClick(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end; var formMenuPrincipal: TformMenuPrincipal; implementation {$R *.dfm} function cifrarTexto (Str, Clave : String) : String; var Src: TStringStream; Dst: TMemoryStream; Size: Integer; Key: TAESKey; ExpandedKey: TAESExpandedKey; begin Result:= EmptyStr; Src:= TStringStream.Create(Str); try Dst:= TMemoryStream.Create; try // Preparamos la clave, lo ideal es que tenga 32 caracteres FillChar(Key,Sizeof(Key),#0); if Length(Clave) > Sizeof(Key) then move(PChar(Clave)^,Key,Sizeof(key)) else move(PChar(Clave)^,Key,Length(Clave)); AEsExpandKey(ExpandedKey,Key); // Guardamos el tamaño del texto original Size:= Src.Size; Dst.WriteBuffer(Size,Sizeof(Size)); // Ciframos el texto AESEncryptStreamECB(Src,Dst,ExpandedKey); // Lo codificamos a base64 Result:= BinToStr(Dst.Memory,Dst.Size); finally Dst.Free; end; finally Src.Free; end; end; function descifrarTexto (Str, Clave : String): String; var Src: TMemoryStream; Dst: TStringStream; Size: Integer; Key: TAESKey; ExpandedKey: TAESExpandedKey; begin if Str <> '' then begin Result:= EmptyStr; Src:= TMemoryStream.Create; try Dst:= TStringStream.Create(Str); try StrToStream(Str,Src); Src.Position:= 0; FillChar(Key,Sizeof(Key),#0); if Length(Clave) > Sizeof(Key) then move(PChar(Clave)^,Key,Sizeof(key)) else move(PChar(Clave)^,Key,Length(Clave)); AESExpandKey(ExpandedKey,Key); // Leemos el tamaño del texto Src.ReadBuffer(Size,Sizeof(Size)); AESDecryptStreamECB(Src,Dst,ExpandedKey); Dst.Size:= Size; Result:= Dst.DataString; finally Dst.Free; end; finally Src.Free; end; end else Result := EmptyStr; end; //Lee un booleano de un INI function leBoolINI (clave, cadena : string; defecto : boolean) : boolean; begin with tinifile.create (changefileext(paramstr(0),'.ini')) do try result := readbool (clave, cadena, defecto); finally free; end; end; //Lee una cadena de texto de un INI function leCadINI (clave, cadena : string; defecto : string) : string; begin with tinifile.create (changefileext(paramstr(0),'.ini')) do try result := readString (clave, cadena, defecto); finally free; end; end; //escribe un Booleano en un INI procedure esBoolINI (clave, cadena : string; valor : boolean); begin with tinifile.create (changefileext(paramstr(0),'.ini')) do try writeBool (clave, cadena, valor); finally free; end; end; //escribe una cadena de texto en un INI procedure esCadINI (clave, cadena, valor : string); begin with tinifile.create (changefileext(paramstr(0),'.ini')) do try writeString (clave, cadena, valor); finally free; end; end; function conectarUnidad(letraUnidad : string; rutaUnidad : string; contrasenaUsuario : string; nombreUsuario : string; mostrarError : Boolean; reconectarAlIniciar : Boolean): DWORD; var nRes: TNetResource; errCode: DWORD; dwFlags: DWORD; begin { FillChar(NRes, SizeOf(NRes), #0); nRes.dwType := RESOURCETYPE_DISK; nRes.lpLocalName := PChar(letraUnidad); nRes.lpRemoteName := PChar(rutaUnidad); if reconectarAlIniciar then dwFlags := CONNECT_UPDATE_PROFILE and CONNECT_INTERACTIVE else dwFlags := CONNECT_INTERACTIVE; errCode := WNetAddConnection3(formMenuPrincipal.Handle, nRes, nil, nil, dwFlags); if (errCode <> NO_ERROR) and (mostrarError) then begin Application.MessageBox(PChar('No se ha podido conectar la unidad:' + #13#10 + SysErrorMessage(GetLastError)), 'Error al conectar unidad de red', MB_ICONERROR + MB_OK); end; Result := errCode;} FillChar(NRes, SizeOf(NRes), #0); nRes.dwType := RESOURCETYPE_DISK; nRes.lpLocalName := PChar(letraUnidad); nRes.lpRemoteName := PChar(rutaUnidad); if reconectarAlIniciar then dwFlags := CONNECT_UPDATE_PROFILE and CONNECT_INTERACTIVE else dwFlags := CONNECT_INTERACTIVE; if (contrasenaUsuario = '') and (nombreUsuario = '') then errCode := WNetAddConnection3(formMenuPrincipal.Handle, nRes, nil, nil, dwFlags) else errCode := WNetAddConnection3(formMenuPrincipal.Handle, nRes, pchar(contrasenaUsuario), pchar(nombreUsuario), dwFlags); if (errCode <> NO_ERROR) and (mostrarError) then begin Application.MessageBox(PChar('No se ha podido conectar la unidad [' + letraUnidad + ']' + #13#10 + SysErrorMessage(GetLastError)), 'Error al conectar unidad de red', MB_ICONERROR + MB_OK); end; Result := errCode; end; function desconectarUnidad (letraUnidad : string; mostrarError : Boolean; forzarDesconexion : Boolean; guardar : Boolean): DWORD; var dwFlags: DWORD; errCode: DWORD; begin if guardar then dwFlags := CONNECT_UPDATE_PROFILE else dwFlags := 0; errCode := WNetCancelConnection2(PChar(letraUnidad), dwFlags, forzarDesconexion); if (errCode <> NO_ERROR) and (mostrarError) then begin Application.MessageBox(PChar('Error al desconectar unidad de red:' + #13#10 + SysErrorMessage(GetLastError)), 'Error al desconectar', MB_ICONERROR + MB_OK); end; Result := errCode; end; procedure TformMenuPrincipal.LWEBClick(Sender: TObject); begin ShellExecute(Handle, Nil, PChar('http://www.ajpdsoft.com'), Nil, Nil, SW_SHOWNORMAL); end; procedure TformMenuPrincipal.bConectarClick(Sender: TObject); begin if conectarUnidad(lsLetra.Text, txtRuta.Text, txtContrasena.Text, txtUsuario.Text, true, opReconectar.Checked) = 0 then Application.MessageBox(PChar('Conexión a unidad de red realizada ' + ' correctamente.'), 'Conexión realizada', MB_ICONINFORMATION + MB_OK); end; procedure TformMenuPrincipal.bDesconectarClick(Sender: TObject); begin if desconectarUnidad(lsLetra.Text, true, true, true) = 0 then Application.MessageBox(PChar('Unidad de red desconectada correctamente.'), 'Desconexión realizada', MB_ICONINFORMATION + MB_OK); end; procedure TformMenuPrincipal.FormClose(Sender: TObject; var Action: TCloseAction); begin esCadINI('Conectar', 'Ruta', txtRuta.Text); esCadINI('Conectar', 'Usuario', txtUsuario.Text); esCadINI('Conectar', 'Contraseña', Trim (cifrarTexto (txtContrasena.Text, '00ajpdsoft11'))); esCadINI('Conectar', 'Letra', lsLetra.Text); esBoolINI('Conectar', 'Volver a conectar', opReconectar.Checked); esCadINI('Desconectar', 'Letra', lsLetraDesconectar.Text); esBoolINI('Opciones inicio', 'Conectar al iniciar', opConectarUnidadInicio.Checked); esBoolINI('Opciones inicio', 'Desconectar al iniciar', opDesconectarInicio.Checked); esBoolINI('Opciones inicio', 'Mostrar mensajes', opMostrarAvisos.Checked); esBoolINI('Opciones inicio', 'Cerrar al iniciar', opCerrarAlIniciar.Checked); end; procedure TformMenuPrincipal.FormCreate(Sender: TObject); begin txtRuta.Text := leCadINI('Conectar', 'Ruta', ''); txtUsuario.Text := leCadINI('Conectar', 'Usuario', ''); txtContrasena.Text := descifrarTexto (leCadINI('Conectar', 'Contraseña', ''), '00ajpdsoft11'); lsLetra.Text := leCadINI('Conectar', 'Letra', ''); opReconectar.Checked := leBoolINI('Conectar', 'Volver a conectar', false); lsLetraDesconectar.Text := leCadINI('Desconectar', 'Letra', ''); opConectarUnidadInicio.Checked := leBoolINI('Opciones inicio', 'Conectar al iniciar', false); opDesconectarInicio.Checked := leBoolINI('Opciones inicio', 'Desconectar al iniciar', false); opMostrarAvisos.Checked := leBoolINI('Opciones inicio', 'Mostrar mensajes', false); opCerrarAlIniciar.Checked := leBoolINI('Opciones inicio', 'Cerrar al iniciar', false); if opDesconectarInicio.Checked then begin if desconectarUnidad(lsLetra.Text, opMostrarAvisos.Checked, true, true) = 0 then if opMostrarAvisos.Checked then Application.MessageBox(PChar('Unidad de red desconectada ' + 'correctamente.'), 'Desconexión realizada', MB_ICONINFORMATION + MB_OK); end; if opConectarUnidadInicio.Checked then begin if conectarUnidad(lsLetra.Text, txtRuta.Text, txtContrasena.Text, txtUsuario.Text, opMostrarAvisos.Checked, opReconectar.Checked) = 0 then if opMostrarAvisos.Checked then Application.MessageBox(PChar('Conexión a unidad de red ' + 'realizada correctamente.'), 'Conexión realizada', MB_ICONINFORMATION + MB_OK); end; if opCerrarAlIniciar.Checked then if HiWord(GetKeyState(VK_SHIFT)) = 0 then Application.Terminate; end; end.
Artículos relacionados
CréditosAplicación desarrollada íntegramente por Alonsojpd, miembro fundador del proyecto AjpdSoft. Nota: Revisado por AjpdSoft el 18-09-2010. Anuncios
Enviado el Jueves, 19 febrero a las 23:46:48 por ajpdsoft
|
|