Autor Tema: VB.net a VC#2008 .....ayuda para migración de código  (Leído 6556 veces)

0 Usuarios y 1 Visitante están viendo este tema.

Desconectado jonathanPIC888

  • Colaborador
  • PIC18
  • *****
  • Mensajes: 326
VB.net a VC#2008 .....ayuda para migración de código
« en: 10 de Febrero de 2010, 19:15:06 »
Hola a todos.....en este momento estoy experimentando un poco con USB en modo HID usando un PIC18F2550 , programando en CCS v4.104 y Visual C#2008 para la PC.
Ahora mi consulta es la siguiente...
yo tengo un programa de control básico usando VB.net y easyHID...
Código: vb.net
  1. ' Acá está el código fuente del main...
  2.     Public Class Form1
  3.  
  4.     Private Const VendorID As Short = 6017  ' Definimos el VendorID.
  5.     Private Const ProductID As Short = 2000 ' Definimos el ProductID.
  6.  
  7.     ' Declaramos los buffer's de entrada y salida de datos.
  8.     Private Const BufferInSize As Short = 8  ' Definimos el tamaño del buffer entrada.
  9.     Private Const BufferOutSize As Short = 8 ' Definimos el tamaño del buffer de salida.
  10.     Dim BufferIn(BufferInSize) As Byte
  11.     Dim BufferOut(BufferOutSize) As Byte
  12.  
  13.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  14.         ConnectToHID(Me) ' Al ejecutarse el formulario conectamos el dispositivo al controlador.
  15.     End Sub
  16.  
  17.     Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
  18.         DisconnectFromHID() ' Si cerramos el formulario desconectamos el dispositivo del controlador.
  19.     End Sub
  20.  
  21.     ' Si se conecta el dispositivo al host...
  22.     Public Sub OnPlugged(ByVal pHandle As Integer)
  23.         If hidGetVendorID(pHandle) = VendorID And hidGetProductID(pHandle) = ProductID Then
  24.             Me.estado.BackColor = Color.Green
  25.             Me.estado_conexion.Text = "CONECTADO"
  26.         End If
  27.     End Sub
  28.  
  29.     ' Si se desconecta el dispositivo del host...
  30.     Public Sub OnUnplugged(ByVal pHandle As Integer)
  31.         If hidGetVendorID(pHandle) = VendorID And hidGetProductID(pHandle) = ProductID Then
  32.             hidSetReadNotify(hidGetHandle(VendorID, ProductID), False)
  33.             Me.estado.BackColor = Color.Red
  34.             Me.estado_conexion.Text = "DESCONECTADO"
  35.         End If
  36.     End Sub
  37.  
  38.     Public Sub OnChanged() ' LLama a todas las funciones de mensajes.
  39.         Dim pHandle As Integer
  40.         pHandle = hidGetHandle(VendorID, ProductID)
  41.         hidSetReadNotify(hidGetHandle(VendorID, ProductID), True)
  42.     End Sub
  43.  
  44.     ' Si recibimos un dato...
  45.     Public Sub OnRead(ByVal pHandle As Integer)
  46.         If hidRead(pHandle, BufferIn(0)) Then
  47.             ' Aqui se reciben los datos a partir del BufferIn(1) = dato del micro..
  48.         End If
  49.     End Sub
  50.  
  51.     Public Sub WriteSomeData() ' Si hay un dato listo para enviar...
  52.         hidWriteEx(VendorID, ProductID, BufferOut(0))
  53.     End Sub
  54.  
  55.     Private Sub boton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles boton.Click
  56.         BufferOut(1) = 1
  57.         Call WriteSomeData()
  58.     End Sub
  59. End Class
Y la librería de control :
Código: vb.net
  1. Imports System
  2. Imports System.Threading
  3. Imports System.Runtime.InteropServices ' Clase para importar la DLL.
  4.  
  5.  
  6. Module HIDDLLInterface
  7.     'Declaramos todas las funciones.
  8.     Declare Function hidConnect Lib "mcHID.dll" Alias "Connect" (ByVal pHostWin As Integer) As Boolean
  9.     Declare Function hidDisconnect Lib "mcHID.dll" Alias "Disconnect" () As Boolean
  10.     Declare Function hidGetItem Lib "mcHID.dll" Alias "GetItem" (ByVal pIndex As Integer) As Integer
  11.     Declare Function hidGetItemCount Lib "mcHID.dll" Alias "GetItemCount" () As Integer
  12.     Declare Function hidRead Lib "mcHID.dll" Alias "Read" (ByVal pHandle As Integer, ByRef pData As Byte) As Boolean
  13.     Declare Function hidWrite Lib "mcHID.dll" Alias "Write" (ByVal pHandle As Integer, ByRef pData As Byte) As Boolean
  14.     Declare Function hidReadEx Lib "mcHID.dll" Alias "ReadEx" (ByVal pVendorID As Integer, ByVal pProductID As Integer, ByRef pData As Byte) As Boolean
  15.     Declare Function hidWriteEx Lib "mcHID.dll" Alias "WriteEx" (ByVal pVendorID As Integer, ByVal pProductID As Integer, ByRef pData As Byte) As Boolean
  16.     Declare Function hidGetHandle Lib "mcHID.dll" Alias "GetHandle" (ByVal pVendoID As Integer, ByVal pProductID As Integer) As Integer
  17.     Declare Function hidGetVendorID Lib "mcHID.dll" Alias "GetVendorID" (ByVal pHandle As Integer) As Integer
  18.     Declare Function hidGetProductID Lib "mcHID.dll" Alias "GetProductID" (ByVal pHandle As Integer) As Integer
  19.     Declare Function hidGetVersion Lib "mcHID.dll" Alias "GetVersion" (ByVal pHandle As Integer) As Integer
  20.     Declare Function hidGetVendorName Lib "mcHID.dll" Alias "GetVendorName" (ByVal pHandle As Integer, ByVal pText As String, ByVal pLen As Integer) As Integer
  21.     Declare Function hidGetProductName Lib "mcHID.dll" Alias "GetProductName" (ByVal pHandle As Integer, ByVal pText As String, ByVal pLen As Integer) As Integer
  22.     Declare Function hidGetSerialNumber Lib "mcHID.dll" Alias "GetSerialNumber" (ByVal pHandle As Integer, ByVal pText As String, ByVal pLen As Integer) As Integer
  23.     Declare Function hidGetInputReportLength Lib "mcHID.dll" Alias "GetInputReportLength" (ByVal pHandle As Integer) As Integer
  24.     Declare Function hidGetOutputReportLength Lib "mcHID.dll" Alias "GetOutputReportLength" (ByVal pHandle As Integer) As Integer
  25.     Declare Sub hidSetReadNotify Lib "mcHID.dll" Alias "SetReadNotify" (ByVal pHandle As Integer, ByVal pValue As Boolean)
  26.     Declare Function hidIsReadNotifyEnabled Lib "mcHID.dll" Alias "IsReadNotifyEnabled" (ByVal pHandle As Integer) As Boolean
  27.     Declare Function hidIsAvailable Lib "mcHID.dll" Alias "IsAvailable" (ByVal pVendorID As Integer, ByVal pProductID As Integer) As Boolean
  28.  
  29.     'Funciones para desplegar mensajes.
  30.  
  31.     Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Integer, ByVal hwnd As Integer, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
  32.     Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
  33.                                           (ByVal hwnd As Integer, ByVal nIndex As Integer, ByVal dwNewLong As Integer) As Integer
  34.  
  35.     Delegate Function SubClassProcDelegate(ByVal hwnd As Integer, ByVal msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
  36.     Public Declare Function DelegateSetWindowLong Lib "USER32.DLL" Alias "SetWindowLongA" _
  37.                                            (ByVal hwnd As Integer, ByVal attr As Integer, ByVal lval As SubClassProcDelegate) As Integer
  38.  
  39.  
  40.     ' Constantes de la aplicación.
  41.     Public Const WM_APP As Integer = 32768
  42.     Public Const GWL_WNDPROC As Short = -4
  43.  
  44.     ' Constantes de mensajes de HID.
  45.     Private Const WM_HID_EVENT As Decimal = WM_APP + 200
  46.     Private Const NOTIFY_PLUGGED As Short = 1
  47.     Private Const NOTIFY_UNPLUGGED As Short = 2
  48.     Private Const NOTIFY_CHANGED As Short = 3
  49.     Private Const NOTIFY_READ As Short = 4
  50.  
  51.     ' Variables locales.
  52.     Private FPrevWinProc As Integer
  53.     Private FWinHandle As Integer
  54.     Private Ref_WinProc As New SubClassProcDelegate(AddressOf WinProc)
  55.     Private HostForm As Object
  56.  
  57.     'Espera a recibir un mensaje del controlador HOST y luego se conecta a el mediante la librería de
  58.     'Funciones.
  59.     Public Function ConnectToHID(ByRef targetForm As Form) As Boolean
  60.         Dim pHostWin As Integer = targetForm.Handle.ToInt32
  61.         FWinHandle = pHostWin
  62.         pHostWin = hidConnect(FWinHandle)
  63.         FPrevWinProc = DelegateSetWindowLong(FWinHandle, GWL_WNDPROC, Ref_WinProc)
  64.         HostForm = targetForm
  65.     End Function
  66.  
  67.     ' Se desconecta del HOST.
  68.     Public Function DisconnectFromHID() As Boolean
  69.         DisconnectFromHID = hidDisconnect
  70.         SetWindowLong(FWinHandle, GWL_WNDPROC, FPrevWinProc)
  71.     End Function
  72.  
  73.     Private Function WinProc(ByVal pHWnd As Integer, ByVal pMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
  74.         If pMsg = WM_HID_EVENT Then
  75.             Select Case wParam
  76.  
  77.                 ' Mensaje de que un dispositivo se ha conectado al host.
  78.                 Case Is = NOTIFY_PLUGGED
  79.                     HostForm.OnPlugged(lParam)
  80.                     ' Mensaje de que un dispositivo se ha desconectado del host.
  81.                 Case Is = NOTIFY_UNPLUGGED
  82.                     HostForm.OnUnplugged(lParam)
  83.                     ' El controlador ha cambiado.
  84.                 Case Is = NOTIFY_CHANGED
  85.                     HostForm.OnChanged()
  86.                     ' Se he recibido un dato.
  87.                 Case Is = NOTIFY_READ
  88.                     HostForm.OnRead(lParam)
  89.             End Select
  90.         End If
  91.         WinProc = CallWindowProc(FPrevWinProc, pHWnd, pMsg, wParam, lParam)
  92.  
  93.     End Function
  94. End Module

Mi pregunta es como poder migrar este código que está en VB.net a VC#2008  :shock: No se mucho de programación de aplicaciones pero me gustaría saber como poder llamar a las funciones de mcHID.dl que es la librería de easyHID en VC# 2008.



Cualquier ayuda será bienvenida   :-/
« Última modificación: 10 de Febrero de 2010, 19:17:07 por jonathanPIC888 »

Desconectado Geo

  • Colaborador
  • PIC24F
  • *****
  • Mensajes: 922
    • Mexchip
Re: VB.net a VC#2008 .....ayuda para migración de código
« Respuesta #1 en: 11 de Febrero de 2010, 06:30:15 »
Viendo que el código ya está listo en VB, tus razones tendrás para pasar a C# ;).

Estas serían algunas equivalencias (por favor corríjanme si me equivoco):
Código: vb.net
  1. Declare Function hidConnect Lib "mcHID.dll" Alias "Connect" (ByVal pHostWin As Integer) As Boolean
Código: C#
  1. [DllImport("mcHID.dll", EntryPoint = "Connect")]
  2. public static extern bool hidConnect(int pHostWin);
------------------
Código: vb.net
  1. Delegate Function SubClassProcDelegate(ByVal hwnd As Integer, ByVal msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Código: C#
  1. Delegate int SubClassProcDelegate( int hwnd, int msg, int wParam, int lParam );
------------------
Código: vb.net
  1. Public Const WM_APP As Integer = 32768
Código: C#
  1. public const int WM_APP = 32768;
------------------
Con eso creo ya se puede avanzar con la conversión, hay una ventaja, que los tipos de datos de VB.NET y C# tienen equivalentes casi directos :).
Otra cosa, ¿estás seguro que ya buscaste y no hay algo ha hecho? Yo pensaría que alguien más ya pudo haber realizado esta migración.
La imaginación es el límite.
Visita mi blog, en inglés o en español :).
Mini curso de introducción a VHDL en MEXCHIP :-/

Desconectado jonathanPIC888

  • Colaborador
  • PIC18
  • *****
  • Mensajes: 326
Re: VB.net a VC#2008 .....ayuda para migración de código
« Respuesta #2 en: 11 de Febrero de 2010, 18:36:23 »
Hasta ahora he correjido la mayoría de los errores...pero hay uno que no entiendo  Cry
Código: C#
  1. public static bool ConnectToHID(ref Form1 targetForm)
  2.     {
  3.         int pHostWin = targetForm.Handle.ToInt32;
  4.         FWinHandle = pHostWin;
  5.         pHostWin = hidConnect(FWinHandle);
  6.         FPrevWinProc = DelegateSetWindowLong(FWinHandle, GWL_WNDPROC, Ref_WinProc);
  7.         HostForm = targetForm;
  8.     }
me tira el error: no se puede encontrar el tipo o nombre de espacio de nombres "Form" (¿falta una referencia using o una referencia de ensamblado ?)

Desconectado Geo

  • Colaborador
  • PIC24F
  • *****
  • Mensajes: 922
    • Mexchip
Re: VB.net a VC#2008 .....ayuda para migración de código
« Respuesta #3 en: 11 de Febrero de 2010, 19:39:58 »
Hasta ahora he correjido la mayoría de los errores...pero hay uno que no entiendo  Cry
Código: C#
  1. public static bool ConnectToHID(ref Form1 targetForm)
  2.     {
  3.         int pHostWin = targetForm.Handle.ToInt32;
  4.         FWinHandle = pHostWin;
  5.         pHostWin = hidConnect(FWinHandle);
  6.         FPrevWinProc = DelegateSetWindowLong(FWinHandle, GWL_WNDPROC, Ref_WinProc);
  7.         HostForm = targetForm;
  8.     }
me tira el error: no se puede encontrar el tipo o nombre de espacio de nombres "Form" (¿falta una referencia using o una referencia de ensamblado ?)

El tipo es Form, no Form1.

Código: C#
  1. public static bool ConnectToHID(ref Form targetForm)
La imaginación es el límite.
Visita mi blog, en inglés o en español :).
Mini curso de introducción a VHDL en MEXCHIP :-/

Desconectado jonathanPIC888

  • Colaborador
  • PIC18
  • *****
  • Mensajes: 326
Re: VB.net a VC#2008 .....ayuda para migración de código
« Respuesta #4 en: 11 de Febrero de 2010, 20:02:02 »
Me sigue marcando error  :5] :5] la verdad no se a que se pueda deber....modifique lo que me dijiste pero sigue con los errores  :lol:

Desconectado jonathanPIC888

  • Colaborador
  • PIC18
  • *****
  • Mensajes: 326
Re: VB.net a VC#2008 .....ayuda para migración de código
« Respuesta #5 en: 11 de Febrero de 2010, 20:25:58 »
Código: C#
  1. int pHostWin = targetForm.Handle.ToInt32;
Acá me indica que la conversión no es posible por que no se puede convertir directamente desde un formato int a int32 ??  :shock:
es otro de los errores que me tira ...




Desconectado Geo

  • Colaborador
  • PIC24F
  • *****
  • Mensajes: 922
    • Mexchip
Re: VB.net a VC#2008 .....ayuda para migración de código
« Respuesta #6 en: 12 de Febrero de 2010, 00:48:10 »
Pon el código y los errores.
La imaginación es el límite.
Visita mi blog, en inglés o en español :).
Mini curso de introducción a VHDL en MEXCHIP :-/

Desconectado jonathanPIC888

  • Colaborador
  • PIC18
  • *****
  • Mensajes: 326
Re: VB.net a VC#2008 .....ayuda para migración de código
« Respuesta #7 en: 13 de Febrero de 2010, 17:05:55 »
acá está el código con los errores todo dentro de la carpeta...estoy investigando mientras otra forma de conectarme por HID con otra librería pero quiero saber si se puede hacer con esta  :?
http://www.mediafire.com/?tlnjkejtund

Desconectado Geo

  • Colaborador
  • PIC24F
  • *****
  • Mensajes: 922
    • Mexchip
Re: VB.net a VC#2008 .....ayuda para migración de código
« Respuesta #8 en: 14 de Febrero de 2010, 02:50:25 »
La biblioteca ya está declarada, los problemas se deben en gran parte a la forma en que se intenta sobreescribir los procedimientos de ventana del Form1, lo cual no es algo que haya hecho y por ahí no te puedo ayudar mucho en estos momentos.

De cualquier forma, las funciones de la biblioteca ya deberían poder ser llamadas.
La imaginación es el límite.
Visita mi blog, en inglés o en español :).
Mini curso de introducción a VHDL en MEXCHIP :-/

Desconectado jonathanPIC888

  • Colaborador
  • PIC18
  • *****
  • Mensajes: 326
Re: VB.net a VC#2008 .....ayuda para migración de código
« Respuesta #9 en: 15 de Febrero de 2010, 17:22:08 »
Bueno he correjido muchos de los errores del programa  :), pero siguen habiendo errores que no entiendo  :z)

Pongo el código del programa:
primero el código del formulario....
Código: C#
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Diagnostics;
  7. using System.Windows.Forms;
  8. using System.Linq;
  9. using System.Xml.Linq;
  10.  
  11. namespace WindowsApplication1
  12. {
  13.         public partial class Form1
  14.         {
  15.  
  16.                 internal Form1()
  17.                 {
  18.                         InitializeComponent();
  19.                 }
  20.                 private const short VendorID = 6017; // Definimos el VendorID.
  21.                 private const short ProductID = 2000; // Definimos el ProductID.
  22.  
  23.                 // Declaramos los buffer's de entrada y salida de datos.
  24.                 private const short BufferInSize = 8; // Definimos el tamaño del buffer entrada.
  25.                 private const short BufferOutSize = 8; // Definimos el tamaño del buffer de salida.
  26.                 private byte[] BufferIn = new byte[BufferInSize + 1];
  27.                 private byte[] BufferOut = new byte[BufferOutSize + 1];
  28.  
  29.                 private void Form1_Load(object sender, System.EventArgs e)
  30.                 {
  31.                         HIDDLLInterface.ConnectToHID(ref this); // Al ejecutarse el formulario conectamos el dispositivo al controlador.
  32.                 }
  33.  
  34.                 private void Form1_FormClosed(object sender, System.Windows.Forms.FormClosedEventArgs e)
  35.                 {
  36.                         HIDDLLInterface.DisconnectFromHID(); // Si cerramos el formulario desconectamos el dispositivo del controlador.
  37.                 }
  38.  
  39.                 // Si se conecta el dispositivo al host...
  40.                 public void OnPlugged(int pHandle)
  41.                 {
  42.                         if (HIDDLLInterface.hidGetVendorID(pHandle) == VendorID && HIDDLLInterface.hidGetProductID(pHandle) == ProductID)
  43.                         {
  44.                                 this.estado.BackColor = Color.Green;
  45.                                 this.estado_conexion.Text = "CONECTADO";
  46.                         }
  47.                 }
  48.  
  49.                 // Si se desconecta el dispositivo del host...
  50.                 public void OnUnplugged(int pHandle)
  51.                 {
  52.                         if (HIDDLLInterface.hidGetVendorID(pHandle) == VendorID && HIDDLLInterface.hidGetProductID(pHandle) == ProductID)
  53.                         {
  54.                                 HIDDLLInterface.hidSetReadNotify(HIDDLLInterface.hidGetHandle(VendorID, ProductID), false);
  55.                                 this.estado.BackColor = Color.Red;
  56.                                 this.estado_conexion.Text = "DESCONECTADO";
  57.                         }
  58.                 }
  59.  
  60.                 public void OnChanged() // LLama a todas las funciones de mensajes.
  61.                 {
  62.                         int pHandle = 0;
  63.                         pHandle = HIDDLLInterface.hidGetHandle(VendorID, ProductID);
  64.                         HIDDLLInterface.hidSetReadNotify(HIDDLLInterface.hidGetHandle(VendorID, ProductID), true);
  65.                 }
  66.  
  67.                 // Si recibimos un dato...
  68.                 public void OnRead(int pHandle)
  69.                 {
  70. //INSTANT C# TODO TASK: In VB, the following line changed the value of the array element BufferIn(0) as a side effect. It will need to be recoded since C# does not allow passing array elements as 'ref' arguments.
  71.                         if (HIDDLLInterface.hidRead(pHandle, ref BufferIn[0]))
  72.                         {
  73.                                 // Aqui se reciben los datos a partir del BufferIn(1) = dato del micro..
  74.                         }
  75.                 }
  76.  
  77.                 public void WriteSomeData() // Si hay un dato listo para enviar...
  78.                 {
  79.                         byte tempVar = BufferOut[0];
  80.                         HIDDLLInterface.hidWriteEx(VendorID, ProductID, ref tempVar);
  81.                                 BufferOut[0] = tempVar;
  82.                 }
  83.  
  84.                 private void boton_Click(object sender, System.EventArgs e)
  85.                 {
  86.                         BufferOut[1] = 1;
  87.                         WriteSomeData();
  88.                 }
  89.         }
  90.  
  91. }

y el código de la librería :

Código: C#
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Diagnostics;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Runtime.InteropServices; // Clase para importar la DLL.
  9. using System.Threading;
  10. using System.Windows.Forms;
  11. using System.Xml.Linq;
  12.  
  13.  
  14. namespace WindowsApplication1
  15. {
  16.         internal static class HIDDLLInterface
  17.         {
  18.                 //Declaramos todas las funciones.
  19.                 [System.Runtime.InteropServices.DllImport("mcHID.dll", EntryPoint="Connect", ExactSpelling=true, CharSet=System.Runtime.InteropServices.CharSet.Ansi, SetLastError=true)]
  20.                 public static extern bool hidConnect(int pHostWin);
  21.                 [System.Runtime.InteropServices.DllImport("mcHID.dll", EntryPoint="Disconnect", ExactSpelling=true, CharSet=System.Runtime.InteropServices.CharSet.Ansi, SetLastError=true)]
  22.                 public static extern bool hidDisconnect();
  23.                 [System.Runtime.InteropServices.DllImport("mcHID.dll", EntryPoint="GetItem", ExactSpelling=true, CharSet=System.Runtime.InteropServices.CharSet.Ansi, SetLastError=true)]
  24.                 public static extern int hidGetItem(int pIndex);
  25.                 [System.Runtime.InteropServices.DllImport("mcHID.dll", EntryPoint="GetItemCount", ExactSpelling=true, CharSet=System.Runtime.InteropServices.CharSet.Ansi, SetLastError=true)]
  26.                 public static extern int hidGetItemCount();
  27.                 [System.Runtime.InteropServices.DllImport("mcHID.dll", EntryPoint="Read", ExactSpelling=true, CharSet=System.Runtime.InteropServices.CharSet.Ansi, SetLastError=true)]
  28.                 public static extern bool hidRead(int pHandle, ref byte pData);
  29.                 [System.Runtime.InteropServices.DllImport("mcHID.dll", EntryPoint="Write", ExactSpelling=true, CharSet=System.Runtime.InteropServices.CharSet.Ansi, SetLastError=true)]
  30.                 public static extern bool hidWrite(int pHandle, ref byte pData);
  31.                 [System.Runtime.InteropServices.DllImport("mcHID.dll", EntryPoint="ReadEx", ExactSpelling=true, CharSet=System.Runtime.InteropServices.CharSet.Ansi, SetLastError=true)]
  32.                 public static extern bool hidReadEx(int pVendorID, int pProductID, ref byte pData);
  33.                 [System.Runtime.InteropServices.DllImport("mcHID.dll", EntryPoint="WriteEx", ExactSpelling=true, CharSet=System.Runtime.InteropServices.CharSet.Ansi, SetLastError=true)]
  34.                 public static extern bool hidWriteEx(int pVendorID, int pProductID, ref byte pData);
  35.                 [System.Runtime.InteropServices.DllImport("mcHID.dll", EntryPoint="GetHandle", ExactSpelling=true, CharSet=System.Runtime.InteropServices.CharSet.Ansi, SetLastError=true)]
  36.                 public static extern int hidGetHandle(int pVendoID, int pProductID);
  37.                 [System.Runtime.InteropServices.DllImport("mcHID.dll", EntryPoint="GetVendorID", ExactSpelling=true, CharSet=System.Runtime.InteropServices.CharSet.Ansi, SetLastError=true)]
  38.                 public static extern int hidGetVendorID(int pHandle);
  39.                 [System.Runtime.InteropServices.DllImport("mcHID.dll", EntryPoint="GetProductID", ExactSpelling=true, CharSet=System.Runtime.InteropServices.CharSet.Ansi, SetLastError=true)]
  40.                 public static extern int hidGetProductID(int pHandle);
  41.                 [System.Runtime.InteropServices.DllImport("mcHID.dll", EntryPoint="GetVersion", ExactSpelling=true, CharSet=System.Runtime.InteropServices.CharSet.Ansi, SetLastError=true)]
  42.                 public static extern int hidGetVersion(int pHandle);
  43.                 [System.Runtime.InteropServices.DllImport("mcHID.dll", EntryPoint="GetVendorName", ExactSpelling=true, CharSet=System.Runtime.InteropServices.CharSet.Ansi, SetLastError=true)]
  44.                 public static extern int hidGetVendorName(int pHandle, string pText, int pLen);
  45.                 [System.Runtime.InteropServices.DllImport("mcHID.dll", EntryPoint="GetProductName", ExactSpelling=true, CharSet=System.Runtime.InteropServices.CharSet.Ansi, SetLastError=true)]
  46.                 public static extern int hidGetProductName(int pHandle, string pText, int pLen);
  47.                 [System.Runtime.InteropServices.DllImport("mcHID.dll", EntryPoint="GetSerialNumber", ExactSpelling=true, CharSet=System.Runtime.InteropServices.CharSet.Ansi, SetLastError=true)]
  48.                 public static extern int hidGetSerialNumber(int pHandle, string pText, int pLen);
  49.                 [System.Runtime.InteropServices.DllImport("mcHID.dll", EntryPoint="GetInputReportLength", ExactSpelling=true, CharSet=System.Runtime.InteropServices.CharSet.Ansi, SetLastError=true)]
  50.                 public static extern int hidGetInputReportLength(int pHandle);
  51.                 [System.Runtime.InteropServices.DllImport("mcHID.dll", EntryPoint="GetOutputReportLength", ExactSpelling=true, CharSet=System.Runtime.InteropServices.CharSet.Ansi, SetLastError=true)]
  52.                 public static extern int hidGetOutputReportLength(int pHandle);
  53.                 [System.Runtime.InteropServices.DllImport("mcHID.dll", EntryPoint="SetReadNotify", ExactSpelling=true, CharSet=System.Runtime.InteropServices.CharSet.Ansi, SetLastError=true)]
  54.                 public static extern void hidSetReadNotify(int pHandle, bool pValue);
  55.                 [System.Runtime.InteropServices.DllImport("mcHID.dll", EntryPoint="IsReadNotifyEnabled", ExactSpelling=true, CharSet=System.Runtime.InteropServices.CharSet.Ansi, SetLastError=true)]
  56.                 public static extern bool hidIsReadNotifyEnabled(int pHandle);
  57.                 [System.Runtime.InteropServices.DllImport("mcHID.dll", EntryPoint="IsAvailable", ExactSpelling=true, CharSet=System.Runtime.InteropServices.CharSet.Ansi, SetLastError=true)]
  58.                 public static extern bool hidIsAvailable(int pVendorID, int pProductID);
  59.  
  60.                 //Funciones para desplegar mensajes.
  61.  
  62.                 [System.Runtime.InteropServices.DllImport("user32", EntryPoint="CallWindowProcA", ExactSpelling=true, CharSet=System.Runtime.InteropServices.CharSet.Ansi, SetLastError=true)]
  63.                 public static extern int CallWindowProc(int lpPrevWndFunc, int hwnd, int Msg, int wParam, int lParam);
  64.                 [System.Runtime.InteropServices.DllImport("user32", EntryPoint="SetWindowLongA", ExactSpelling=true, CharSet=System.Runtime.InteropServices.CharSet.Ansi, SetLastError=true)]
  65.                 public static extern int SetWindowLong(int hwnd, int nIndex, int dwNewLong);
  66.  
  67.                 public delegate int SubClassProcDelegate(int hwnd, int msg, int wParam, int lParam);
  68.                 [System.Runtime.InteropServices.DllImport("USER32.DLL", EntryPoint="SetWindowLongA", ExactSpelling=true, CharSet=System.Runtime.InteropServices.CharSet.Ansi, SetLastError=true)]
  69.                 public static extern int DelegateSetWindowLong(int hwnd, int attr, SubClassProcDelegate lval);
  70.  
  71.  
  72.                 // Constantes de la aplicación.
  73.                 public const int WM_APP = 32768;
  74.                 public const short GWL_WNDPROC = -4;
  75.  
  76.                 // Constantes de mensajes de HID.
  77.                 private const decimal WM_HID_EVENT = WM_APP + 200;
  78.                 private const short NOTIFY_PLUGGED = 1;
  79.                 private const short NOTIFY_UNPLUGGED = 2;
  80.                 private const short NOTIFY_CHANGED = 3;
  81.                 private const short NOTIFY_READ = 4;
  82.  
  83.                 // Variables locales.
  84.                 private static int FPrevWinProc;
  85.                 private static int FWinHandle;
  86.                 private static SubClassProcDelegate Ref_WinProc = new SubClassProcDelegate(WinProc);
  87.                 private static object HostForm;
  88.  
  89.                 //Espera a recibir un mensaje del controlador HOST y luego se conecta a el mediante la librería de
  90.                 //Funciones.
  91.                 public static bool ConnectToHID(ref Form targetForm)
  92.                 {
  93.                         bool pHostWin = Convert.ToBoolean(targetForm.Handle);
  94.             FWinHandle = Convert.ToInt32(FWinHandle);
  95.                         pHostWin = hidConnect(FWinHandle);
  96.                         FPrevWinProc = DelegateSetWindowLong(FWinHandle, GWL_WNDPROC, Ref_WinProc);
  97.                         HostForm = targetForm;
  98. //INSTANT C# NOTE: Inserted the following 'return' since all code paths must return a value in C#:
  99.                         return false;
  100.                 }
  101.  
  102.                 // Se desconecta del HOST.
  103.                 public static bool DisconnectFromHID()
  104.                 {
  105.                         bool tempDisconnectFromHID = false;
  106.                         tempDisconnectFromHID = hidDisconnect();
  107.                         SetWindowLong(FWinHandle, GWL_WNDPROC, FPrevWinProc);
  108.                         return tempDisconnectFromHID;
  109.                 }
  110.  
  111.                 private static int WinProc(int pHWnd, int pMsg, int wParam, int lParam)
  112.                 {
  113.                         if (pMsg == WM_HID_EVENT)
  114.                         {
  115.                                 switch (wParam)
  116.                                 {
  117.  
  118.                                         // Mensaje de que un dispositivo se ha conectado al host.
  119.                                         case NOTIFY_PLUGGED:
  120.                                                 HostForm.OnPlugged(lParam);
  121.                                                 // Mensaje de que un dispositivo se ha desconectado del host.
  122.                                                 break;
  123.                                         case NOTIFY_UNPLUGGED:
  124.                                                 HostForm.OnUnplugged(lParam);
  125.                                                 // El controlador ha cambiado.
  126.                                                 break;
  127.                                         case NOTIFY_CHANGED:
  128.                                                 HostForm.OnChanged();
  129.                                                 // Se he recibido un dato.
  130.                                                 break;
  131.                                         case NOTIFY_READ:
  132.                                                 HostForm.OnRead(lParam);
  133.                                                 break;
  134.                                 }
  135.                         }
  136.                         return CallWindowProc(FPrevWinProc, pHWnd, pMsg, wParam, lParam);
  137.  
  138.                 }
  139.  
  140.         internal static void ConnectToHID(ref Form1 form1)
  141.         {
  142.             throw new NotImplementedException();
  143.         }
  144.     }
  145. }
y por último la lista de los errores generados:
Código: C#
  1. Error   1 No se puede pasar '<this>' como argumento out o ref porque es de sólo lectura        C:\Documents and Settings\Flia. Moyano\Escritorio\Electrónica\easyHID c#\Form1.cs      32      37      easyHID_VB.net 2008
  2. Error   2       'object' no contiene una definición de 'OnPlugged' ni se encontró ningún método de extensión 'OnPlugged' que acepte un primer argumento de tipo 'object' (¿falta una directiva de uso o una referencia de ensamblado?)    C:\Documents and Settings\Flia. Moyano\Escritorio\Electrónica\easyHID c#\Module1.cs    121     16      easyHID_VB.net 2008
  3. Error   3       'object' no contiene una definición de 'OnUnplugged' ni se encontró ningún método de extensión 'OnUnplugged' que acepte un primer argumento de tipo 'object' (¿falta una directiva de uso o una referencia de ensamblado?)        C:\Documents and Settings\Flia. Moyano\Escritorio\Electrónica\easyHID c#\Module1.cs    125     16      easyHID_VB.net 2008
  4. Error   4       'object' no contiene una definición de 'OnChanged' ni se encontró ningún método de extensión 'OnChanged' que acepte un primer argumento de tipo 'object' (¿falta una directiva de uso o una referencia de ensamblado?)    C:\Documents and Settings\Flia. Moyano\Escritorio\Electrónica\easyHID c#\Module1.cs    129     16      easyHID_VB.net 2008
  5. Error   5       'object' no contiene una definición de 'OnRead' ni se encontró ningún método de extensión 'OnRead' que acepte un primer argumento de tipo 'object' (¿falta una directiva de uso o una referencia de ensamblado?)  C:\Documents and Settings\Flia. Moyano\Escritorio\Electrónica\easyHID c#\Module1.cs    133     16      easyHID_VB.net 2008



 

anything