• Jueves 28 de Marzo de 2024, 13:31

Autor Tema:  Código a C++  (Leído 4667 veces)

Meta

  • Miembro MUY activo
  • ***
  • Mensajes: 140
    • Ver Perfil
Código a C++
« en: Miércoles 9 de Junio de 2010, 17:02 »
0
Buenas:

Quiero pasar este pedazo de código de C# a C++ 2010. ¿Cómo es?

Código: Text
  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6.  
  7. using System.Runtime.InteropServices; // No olvidar aqui.
  8.  
  9. namespace Puerto_paralelo
  10. {
  11.     class PortInterop
  12.     {
  13.         [DllImport("inpout32.dll", EntryPoint = "Out32")]
  14.         public static extern void Output(int adress, int value);
  15.         [DllImport("inpout32.dll", EntryPoint = "Inp32")]
  16.         public static extern int Input(int adress);
  17.     }
  18. }
  19.  
  20.  

He creado una clase como indica abajo.







A partir de aquí se me crearon el .h y el .cpp. Por aquí estoy perdido y no se como pasarlo de C# a C++ con Visual C++ 2010.





Saludo.

rfog

  • Miembro MUY activo
  • ***
  • Mensajes: 166
    • Ver Perfil
Re: Código a C++
« Respuesta #1 en: Miércoles 9 de Junio de 2010, 18:30 »
0
Para C++/CLi no necesitas nada de eso. Llama a las funciones de la DLL como si fueran nativas y listo.
Microsoft Visual C++ MVP - Mi blog sobre programación: http://geeks.ms/blogs/rfog

Meta

  • Miembro MUY activo
  • ***
  • Mensajes: 140
    • Ver Perfil
Re: Código a C++
« Respuesta #2 en: Miércoles 9 de Junio de 2010, 21:28 »
0
¿Puedes poner un ejemplo?

rfog

  • Miembro MUY activo
  • ***
  • Mensajes: 166
    • Ver Perfil
Re: Código a C++
« Respuesta #3 en: Miércoles 9 de Junio de 2010, 23:23 »
0
Incluye el fichero cabecera en donde estén declaradas las funciones Input y Output que deberían venir con la DLL.

Y luego las usas:

Outport(0x3f8,55);

int valor=Input(0x3f8);

Así o con variables manejadas. No hay más secreto.

El Interop IJW de C++/CLI hará el resto: convertirá esos enteros manejados en enteros nativos y viceversa.
Microsoft Visual C++ MVP - Mi blog sobre programación: http://geeks.ms/blogs/rfog

Eternal Idol

  • Moderador
  • ******
  • Mensajes: 4696
  • Nacionalidad: ar
    • Ver Perfil
Re: Código a C++
« Respuesta #4 en: Miércoles 9 de Junio de 2010, 23:53 »
0
Si tenes la libreria de importacion enlaza estaticamente la DLL como te dijo rfog sino cargala dinamicante (LoadLibrary) y usa un par de punteros a funcion (con GetProcAddress):
http://www.hytherion.com/beattidp/comput/pport.htm

Nacional y Popular En mi país la bandera de Eva es inmortal.


Queremos una Argentina socialmente justa, económicamente libre y  políticamente soberana.
¡Perón cumple, Evita dignifica!


La mano invisible del mercado me robo la billetera.

Meta

  • Miembro MUY activo
  • ***
  • Mensajes: 140
    • Ver Perfil
Re: Código a C++
« Respuesta #5 en: Jueves 10 de Junio de 2010, 00:17 »
0
Outport(0x3f8,55);

int valor=Input(0x3f8);


No entiendo lo del 3F8. ¿No será 378?

En cuanto al 55. ¿Qué significa el 55?
El 378 es la dirección.

Puse algo así:
Código: C++
  1.  
  2. #include "StdAfx.h"
  3. #include "PortInterop.h"
  4.  
  5. PortInterop::PortInterop(void)
  6. {
  7.     Outport(0x378,55);
  8.  
  9.     int valor=Input(0x378);
  10. }
  11.  
  12.  

ME dice esto:
Citar
------ Operación Generar iniciada: proyecto: Puerto Paralelo cpp, configuración: Debug Win32 ------
  PortInterop.cpp
PortInterop.cpp(6): error C3861: 'Outport': no se encontró el identificador
PortInterop.cpp(8): error C3861: 'Input': no se encontró el identificador
========== Generar: 0 correctos, 1 incorrectos, 0 actualizados, 0 omitidos ==========

En C#, al hacer doble click en el botón, podemos ver el código fuente que es este:
Código: C#
  1.  
  2.  private void button_D0_ON_Click(object sender, EventArgs e)
  3.         {
  4.             D0 = 1;
  5.             opciones();
  6.         }
  7.  
  8.  


Código: C#
  1.  
  2. #region Opciones.
  3.         public void opciones()
  4.         {
  5.             int value = 0;
  6.  
  7.             if (D0 == 1)
  8.             {
  9.                 value += (int)Math.Pow(2, 0);
  10.                 LoadNewPict_D0();
  11.             }
  12.             else
  13.                 LoadOldPict_D0();
  14.             value += 0;
  15.  
  16.             if (D1 == 1)
  17.             {
  18.                 value += (int)Math.Pow(2, 1);
  19.                 LoadNewPict_D1();
  20.             }
  21.             else
  22.                 LoadOldPict_D1();
  23.             value += 0;
  24.  
  25.             if (D2 == 1)
  26.             {
  27.                 value += (int)Math.Pow(2, 2);
  28.                 LoadNewPict_D2();
  29.             }
  30.             else
  31.                 LoadOldPict_D2();
  32.             value += 0;
  33.  
  34.             if (D3 == 1)
  35.             {
  36.                 value += (int)Math.Pow(2, 3);
  37.                 LoadNewPict_D3();
  38.             }
  39.             else
  40.                 LoadOldPict_D3();
  41.             value += 0;
  42.  
  43.             if (D4 == 1)
  44.             {
  45.                 value += (int)Math.Pow(2, 4);
  46.                 LoadNewPict_D4();
  47.             }
  48.             else
  49.                 LoadOldPict_D4();
  50.             value += 0;
  51.  
  52.             if (D5 == 1)
  53.             {
  54.                 value += (int)Math.Pow(2, 5);
  55.                 LoadNewPict_D5();
  56.             }
  57.             else
  58.                 LoadOldPict_D5();
  59.             value += 0;
  60.  
  61.             if (D6 == 1)
  62.             {
  63.                 value += (int)Math.Pow(2, 6);
  64.                 LoadNewPict_D6();
  65.             }
  66.             else
  67.                 LoadOldPict_D6();
  68.             value += 0;
  69.  
  70.             if (D7 == 1)
  71.             {
  72.                 value += (int)Math.Pow(2, 7);
  73.                 LoadNewPict_D7();
  74.             }
  75.             else
  76.                 LoadOldPict_D7();
  77.             value += 0;
  78.  
  79.             try
  80.             {
  81.                 Puerto_paralelo.PortInterop.Output(adress, value);
  82.             }
  83.             catch (DllNotFoundException)
  84.             {
  85.                 Alerta_1();
  86.             }
  87.  
  88.         }
  89.  
  90.         #endregion
  91.  
  92.  

EDITO:
Estoy viendo el Test2.c y me sale esto:
Citar
/**************************************************/ /*** ***/ /*** Test2.c -- test interface to inpout32.dll ***/ /*** ( http://www.logix4u.net/inpout32.htm ) ***/ /*** ***/ /*** Copyright (C) 2005, Douglas Beattie Jr. ***/ /*** ***/ /***  ***/ /*** http://www.hytherion.com/beattidp/ ***/ /*** ***/ /**************************************************/ /* Last Update: 2006.05.14 */ /*******************************************************/ /* */ /* Builds with Borland's Command-line C Compiler */ /* (free for public download from Borland.com, at */ /* http://www.borland.com/bcppbuilder/freecompiler ) */ /* */ /* Compile with: */ /* */ /* BCC32 -IC:BORLANDBCC55INCLUDE TEST2.C */ /* */ /* */ /* Be sure to change PPORT_BASE (Port Base address) */ /* accordingly if your LPT port is addressed */ /* elsewhere. */ /* */ /*******************************************************/ #include #include #include /* Definitions in the build of inpout32.dll are: */ /* short _stdcall Inp32(short PortAddress); */ /* void _stdcall Out32(short PortAddress, short data); */ /* prototype (function typedef) for DLL function Inp32: */ typedef short (_stdcall *inpfuncPtr)(short portaddr); typedef void (_stdcall *oupfuncPtr)(short portaddr, short datum); #define PPORT_BASE 0x378 // Prototypes for Test functions void test_read8(void); void test_write(void); void test_write_datum(short datum); /* After successful initialization, these 2 variables will contain function pointers. */ inpfuncPtr inp32fp; oupfuncPtr oup32fp; /* Wrapper functions for the function pointers - call these functions to perform I/O. */ short Inp32 (short portaddr) { return (inp32fp)(portaddr); } void Out32 (short portaddr, short datum) { (oup32fp)(portaddr,datum); } int main(void) { HINSTANCE hLib; short x; int i; /* Load the library */ hLib = LoadLibrary("inpout32.dll"); if (hLib == NULL) { fprintf(stderr,"LoadLibrary Failed.n"); return -1; } /* get the address of the function */ inp32fp = (inpfuncPtr) GetProcAddress(hLib, "Inp32"); if (inp32fp == NULL) { fprintf(stderr,"GetProcAddress for Inp32 Failed.n"); return -1; } oup32fp = (oupfuncPtr) GetProcAddress(hLib, "Out32"); if (oup32fp == NULL) { fprintf(stderr,"GetProcAddress for Oup32 Failed.n"); return -1; } /*******************************************************/ /** IF WE REACHED HERE, INITIALIZED SUCCESSFUL ******/ /*******************************************************/ /* now test the functions */ /***** Read 8 bytes at I/O base address */ test_read8(); /***** Write 0x75 to data register and verify */ test_write(); /***** One more time, different value */ test_write_datum(0xAA); /* finished - unload library and exit */ FreeLibrary(hLib); return 0; } /* TEST: Read inputs of 8 registers from PORT_BASE address */ void test_read8(void) { short x; int i; /* Try to read 0x378..0x37F, LPT1: */ for (i=PPORT_BASE; (i<(PPORT_BASE+8)); i++) { x = Inp32(i); printf("Port read (%04X)= %04Xn",i,x); } } /* TEST: Write constant 0x77 to PORT_BASE (Data register) */ void test_write(void) { short x; int i; /***** Write the data register */ i=PPORT_BASE; x=0x75; /***** Write the data register */ Out32(i,x); printf("Port write to 0x%X, datum=0x%2Xn" ,i ,x); /***** And read back to verify */ x = Inp32(i); printf("Port read (%04X)= %04Xn",i,x); /***** Set all bits high */ x=0xFF; Out32(i,x); /***** Now, set bi-directional and read again */ Out32(PPORT_BASE+2,0x20); // Activate bi-directional x = Inp32(i); printf("Set Input, read (%04X)= %04Xn",i,x); Out32(PPORT_BASE+2,0x00); // Set Output-only again x = Inp32(i); printf("Reset Ouput, read (%04X)= %04Xn",i,x); } /* TEST: Write data from parameter */ void test_write_datum(short datum) { short x; int i; i=PPORT_BASE; x = datum; /***** Write the data register */ Out32(i,x); printf("Port write to 0x%X, datum=0x%2Xn" ,i ,x); /***** And read back to verify */ x = Inp32(i); printf("Port read (%04X)= %04Xn",i,x); }

Voy a investigar el enlace a ver que tal. Recuerda que uso el Visual C++ 2010.

Eternal Idol

  • Moderador
  • ******
  • Mensajes: 4696
  • Nacionalidad: ar
    • Ver Perfil
Re: Código a C++
« Respuesta #6 en: Jueves 10 de Junio de 2010, 00:58 »
0
Ah, es .NET y no C++, entonces lo muevo a donde corresponde.

Nacional y Popular En mi país la bandera de Eva es inmortal.


Queremos una Argentina socialmente justa, económicamente libre y  políticamente soberana.
¡Perón cumple, Evita dignifica!


La mano invisible del mercado me robo la billetera.

Meta

  • Miembro MUY activo
  • ***
  • Mensajes: 140
    • Ver Perfil
Re: Código a C++
« Respuesta #7 en: Jueves 10 de Junio de 2010, 01:36 »
0
Puede descargar el cósigo fuente del C# completo y funcionamuy bien. Se incluye la librería inpout32.dll.
El mensaje contiene 1 archivo adjunto. Debes ingresar o registrarte para poder verlo y descargarlo.