PDA

Ver la Versión Completa : Abrir y cerrar bandeja de discos con Java


REHome
16-11-2017, 20:14:57
Buenas a todos y a todas:

https://thumbs.dreamstime.com/t/disco-insterted-al-dvd-o-al-lector-de-cd-30302263.jpg

Quiero pasar este código en consola de C#, VB .net o el C++ CLR a Java, usen Eclipse o NetBeans. Lo que hace el código es si pulsas A o la letra C abre o cierra la bandeja del lector de discos. A parte de C#, también está en C++ CLR y VB .net por si lo entienden mejor. Lo que hace el código es abrir y cerrar la bandeja de discos del lector, sea IDE o SATA.

Código C#:
using System;
using System.Runtime.InteropServices;
using System.Text;

namespace Lector_teclado_consola_cs
{
class Program
{
[DllImport("winmm.dll")]
public static extern Int32 mciSendString(string lpstrCommand, StringBuilder lpstrReturnString,
int uReturnLength, IntPtr hwndCallback);

public static StringBuilder rt = new StringBuilder(127);

public static void DoEvents()
{
// Dispatcher.CurrentDispatcher.Invoke(DispatcherPriority.Background, new ThreadStart(delegate { }));
Console.SetCursorPosition(0, 6);
Console.Write("Abriendo...");
}

public static void DoEvents2()
{
// Dispatcher.CurrentDispatcher.Invoke(DispatcherPriority.Background, new ThreadStart(delegate { }));
Console.SetCursorPosition(0, 6);
Console.Write("Cerrando...");
}

static void Main(string[] args)
{
// Título de la ventana.
Console.Title = "Control lector de bandeja. C#";

// Tamaño ventana consola.
Console.WindowWidth = 29; // X. Ancho.
Console.WindowHeight = 8; // Y. Alto.

// Cursor invisible.
Console.CursorVisible = false;

// Posición del mansaje en la ventana.
Console.SetCursorPosition(0, 0);
Console.Write(@"Control bandeja del lector:

A - Abrir bandeja.
C - Cerrar bandeja.
===========================");



ConsoleKey key;
//Console.CursorVisible = false;
do
{
key = Console.ReadKey(true).Key;

string mensaje = string.Empty;

//Asignamos la tecla presionada por el usuario
switch (key)
{
case ConsoleKey.A:
// mensaje = "Abriendo...";
Console.SetCursorPosition(0, 6);
DoEvents();
mciSendString("set CDAudio door open", rt, 127, IntPtr.Zero);
mensaje = "Abierto.";
break;

case ConsoleKey.C:
// mensaje = "Cerrando...";
Console.SetCursorPosition(0, 6);
DoEvents2();
mciSendString("set CDAudio door closed", rt, 127, IntPtr.Zero);
mensaje = "Cerrado.";
break;
}

Console.SetCursorPosition(0, 6);
Console.Write(" ");
Console.SetCursorPosition(0, 6);
Console.Write(mensaje);

}
while (key != ConsoleKey.Escape);
}
}
}

Código VB .net:
Imports System.Runtime.InteropServices
Imports System.Text

Module Module1
<DllImport("winmm.dll")>
Public Function mciSendString(lpstrCommand As String, lpstrReturnString As StringBuilder, uReturnLength As Integer, hwndCallback As IntPtr) As Int32
End Function

Public rt As New StringBuilder(127)

Public Sub DoEvents()
' Dispatcher.CurrentDispatcher.Invoke(DispatcherPriority.Background, new ThreadStart(delegate { }));
Console.SetCursorPosition(0, 6)
Console.Write("Abriendo...")
End Sub

Public Sub DoEvents2()
' Dispatcher.CurrentDispatcher.Invoke(DispatcherPriority.Background, new ThreadStart(delegate { }));
Console.SetCursorPosition(0, 6)
Console.Write("Cerrando...")
End Sub

Sub Main()
' Título de la ventana.
Console.Title = "Control lector de bandeja. Visual Basic"

' Tamaño ventana consola.
Console.WindowWidth = 29 ' X. Ancho.
Console.WindowHeight = 8 ' Y. Alto.
' Cursor invisible.
Console.CursorVisible = False

' Posición del mansaje en la ventana.
Console.SetCursorPosition(0, 0)
Console.Write("Control bandeja del lector:" & vbCr & vbLf & vbCr & vbLf &
"A - Abrir bandeja." & vbCr & vbLf &
"C - Cerrar bandeja." & vbCr & vbLf &
"===========================")

Dim key As ConsoleKey
'Console.CursorVisible = false;
Do
key = Console.ReadKey(True).Key

Dim mensaje As String = String.Empty

'Asignamos la tecla presionada por el usuario
Select Case key
Case ConsoleKey.A
' mensaje = "Abriendo...";
Console.SetCursorPosition(0, 6)
DoEvents()
mciSendString("set CDAudio door open", rt, 127, IntPtr.Zero)
mensaje = "Abierto."
Exit Select

Case ConsoleKey.C
' mensaje = "Cerrando...";
Console.SetCursorPosition(0, 6)
DoEvents2()
mciSendString("set CDAudio door closed", rt, 127, IntPtr.Zero)
mensaje = "Cerrado."
Exit Select
End Select

Console.SetCursorPosition(0, 6)
Console.Write(" ")
Console.SetCursorPosition(0, 6)

Console.Write(mensaje)
Loop While key <> ConsoleKey.Escape
End Sub

End Module


Código C++ CLR:
#include "stdafx.h"

using namespace System;
using namespace System::Runtime::InteropServices;
using namespace System::Text;

[DllImport("winmm.dll")]
extern Int32 mciSendString(String^ lpstrCommand, StringBuilder^ lpstrReturnString,
int uReturnLength, IntPtr hwndCallback);

static void DoEvents()
{
Console::SetCursorPosition(0, 6);
Console::Write("Abriendo...");
}

static void DoEvents2()
{
Console::SetCursorPosition(0, 6);
Console::Write("Cerrando...");
}

int main(array<System::String ^> ^args)
{
StringBuilder^ rt = gcnew StringBuilder(127);

// Título de la ventana.
Console::Title = "Control lector de bandeja. C++ CLR";

// Tamaño ventana consola.
Console::WindowWidth = 29; // X. Ancho.
Console::WindowHeight = 8; // Y. Alto.

// Cursor invisible.
Console::CursorVisible = false;

// Posición del mansaje en la ventana.
Console::SetCursorPosition(0, 0);
Console::WriteLine("Control bandeja del lector : \n\n" +
"A - Abrir bandeja. \n" +
"C - Cerrar bandeja. \n" +
"========================== \n");
//Console::WriteLine("A - Abrir bandeja.");
//Console::WriteLine("C - Cerrar bandeja.");
//Console::Write("==========================");

ConsoleKey key;
//Console::CursorVisible = false;
do
{
key = Console::ReadKey(true).Key;

String^ mensaje = "";

//Asignamos la tecla presionada por el usuario
switch (key)
{
case ConsoleKey::A:
mensaje = "Abriendo...";
Console::SetCursorPosition(0, 6);
DoEvents();
mciSendString("set CDAudio door open", rt, 127, IntPtr::Zero);
mensaje = "Abierto.";
break;

case ConsoleKey::C:
mensaje = "Cerrando...";
Console::SetCursorPosition(0, 6);
DoEvents2();
mciSendString("set CDAudio door closed", rt, 127, IntPtr::Zero);
mensaje = "Cerrado.";
break;
}

Console::SetCursorPosition(0, 6);
Console::Write(" ");
Console::SetCursorPosition(0, 6);
Console::Write(mensaje);

} while (key != ConsoleKey::Escape);
return 0;
}

Esto de Java ya es complicado. ¿Verdad?

Saludos.