Está en la página 1de 11

Dll EN C#. Programación II. Lic.

Luis Antonio Amaya

Crear dll BIBLIOTECA DE ACCESO DINAMICO

CONSTRUIREMOS UNA DLL QUE HAGA UNA SIMPLE SUMA:

El código:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace sumar
{
public class Class1
{
public double suma(double valor1, double valor2)
{
return valor1 + valor2;
}
}
}

Guardar el proyecto y una vez termine de escribir el código, presione ejecutar para generar la dll.

Aparecerá un cuadro de error pero ya se habrá creado nuestra DLL en la carpeta debug de
nuestro proyecto.
Dll EN C#. Programación II. Lic. Luis Antonio Amaya

Usando la dll
Crear otro proyecto con windowsform

Clik derecho en…

Click derecho en references y dar clik en agregar referencia….

Dar clik en examinar…veremos el siguiente cuadro….


Dll EN C#. Programación II. Lic. Luis Antonio Amaya
Dll EN C#. Programación II. Lic. Luis Antonio Amaya

Buscamos nuestra dll recién creada que se llama sumar.dll y dar click en aceptar.

Diseñar una interfaz como la siguiente:

Se hace referencia a la clase


contenedora

Primero debemos agregar nuestra dll:

using sumar; Referenciamos a la sub-


clase

El código del botón calcular es:

Class1 usodemidll = new Class1();// instanciamos…


double resultado = usodemidll.suma(int.Parse(textBox1.Text), int.Parse(textBox2.Text));//en una variable
del tipo double, almacenamos lo que nos devuelva la dll al procesar los datos

MessageBox.Show(resultado.ToString());

TAREA: CREAR UNA CLASE QUE CONTENGA LAS CUATRO OPERACIONES BÁSICAS, EN LA DIVISIÓN CONSIDERE LA DIVISIÓN POR CERO!
Dll EN C#. Programación II. Lic. Luis Antonio Amaya
Dll EN C#. Programación II. Lic. Luis Antonio Amaya

Creación de controles por el usuario.


Crearemos un control que permita reproducir archivos wav.

Creamos un nuevo proyecto, agregamos nuevo elemento “control de usuario”


Dll EN C#. Programación II. Lic. Luis Antonio Amaya

Disponemos los siguientes botones:

Agregar:

using System.IO;
using System.Media;
Dll EN C#. Programación II. Lic. Luis Antonio Amaya

luego…..

public partial class repromusik : UserControl


{
private SoundPlayer soundplayer;

además….

public repromusik()
{
InitializeComponent();
soundplayer = new SoundPlayer();

Código botón abrir:

openFileDialog1.ShowDialog();
if (openFileDialog1.ShowDialog() == DialogResult.OK) {
soundplayer.SoundLocation = openFileDialog1.FileName;
}

Código de reproducir:
soundplayer.Play();

Código de Parar:
soundplayer.Stop();
Dll EN C#. Programación II. Lic. Luis Antonio Amaya

Se ejecuta…aparecerá el formulario por defecto, se detiene la ejecución….


A continuación dar click en el formulario que por defecto nos parece y la pantalla será igual a la figura:

Mi Control!

Lo arrastramos al formulario como si fuese un control prediseñado….


Dll EN C#. Programación II. Lic. Luis Antonio Amaya

Realice una dll que pueda realizar:

 EL MAYOR DE DOS NUMEROS


 LAS OPERACIONES SUMA, RESTA MULTIPLCIAR Y DIVIDIR
 ESCRIBIR LAS INICIALES DEL NOMBRE Y CONTAR LAS LETRAS
 EL AREA DE UN TRIANGULO.(B*H)/2

ESCRIBIR EN EL REGISTRO DE WINDOWS


Si necesitamos en nuestra aplicación guardar datos como por ejemplo las
veces que se a utilizado nuestra aplicación, podemos hacer uso del registro de
Windows
PARA ESCRIBIR EN EL REGISTRO DE WINDOWS, AGREGAR:

using Microsoft.Win32;

APLICACION SHARE
Dll EN C#. Programación II. Lic. Luis Antonio Amaya

PARA GUARDAR EN UN BOTON:

RegistryKey reg = Registry.CurrentUser.CreateSubKey("SOFTWARE\\MIPROGRA\\PRUEBA");


reg.SetValue("URL", “UGB”);

PARA LEER DEL REGISTRO:

RegistryKey reg = Registry.CurrentUser.CreateSubKey("SOFTWARE\\ MIPROGRA \\PRUEBA");


string URL = (string)reg.GetValue("URL");
MessageBox.Show(URL);

También podría gustarte