IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C# Discussion :

DLL en C++ utilisé dans C#


Sujet :

C#

  1. #1
    Membre du Club
    Homme Profil pro
    Inscrit en
    Mai 2008
    Messages
    84
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Mai 2008
    Messages : 84
    Points : 60
    Points
    60
    Par défaut DLL en C++ utilisé dans C#
    Bonjour tout le monde !

    Je dois programmer un logiciel pour lire sur un RFID Reader stronglink et j'ai vraiment pas grand chose pour comprendre comment ça marche : La doc avec la device est en chinois, et je n'ai pas d'exemple en C#. J'ai seulement un pdf avec les fonctions et un exemple en C++.

    Je crois être très prêt du but, j'ai la variable dans lequel, je crois, pouvoir trouver le ID, mais dans la doc C++ c'est un type char et moi en C# je chercherais plutôt un type string.

    Je vous donne la description de la function, mon code C# et l'exemple C++ :
    SL500-User-Manual.pdf
    6.5.3 INT WINAPI ISO15693_GET_SYSTEM_INFORMATION P.33

    C++
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    void CDemoDlg::OnButtonInventory() 
    {
    	WORD icdev = 0x0000;
    	int status;		
    	unsigned char pData[MAX_RF_BUFFER];
    	unsigned char len;
    	unsigned char type = 0x31;	
     
    	if(!m_bConnectDevice){		
    		MessageBox("Not connect device","Error",MB_OK|MB_ICONERROR);
    		return;
    	}
     
    	status = rf_init_type(icdev,type);
        if(status){		
    		MessageBox("rf_init_type failed","Error",MB_OK|MB_ICONERROR);
    		return;
    	}
     
    	m_edit_uid1.SetWindowText("");
    	m_edit_uid2.SetWindowText("");
    	m_edit_uid3.SetWindowText("");
    	m_edit_uid4.SetWindowText("");
     
    	 status = ISO15693_Inventorys(icdev,pData,&len);
    	 if(status){		 
    		 MessageBox("ISO15693_Inventorys failed","Error",MB_OK|MB_ICONERROR);
    		 return;
    	 }     
     
    	 for(int i = 0 ;i < len /9;i++ ){
     
    		 if(i == 0)
    			 m_edit_uid1.SetWindowTextEx(&pData[9 * i + 1],8);
    		 if(i == 1) 
    			 m_edit_uid2.SetWindowTextEx(&pData[9 * i + 1],8);
    		 if(i == 2) 
    			 m_edit_uid3.SetWindowTextEx(&pData[9 * i + 1],8);
    		 if(i == 3) 
    			 m_edit_uid4.SetWindowTextEx(&pData[9 * i + 1],8);
     
    	 }	 
     
    	  MessageBox("INVENTORY success","Success",MB_OK|MB_ICONASTERISK);
    }
    C#
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
     
    using System.Runtime.InteropServices;
     
    namespace test_RFID_reader
    {
        public partial class Form1 : Form
        {
     
            [DllImport("MasterRD.dll")]
            static extern int rf_init_com(int port, int baud);
            [DllImport("MasterRD.dll")]
            static extern int rf_ClosePort();
            [DllImport("MasterRD.dll")]
            static extern int rf_beep(short icdev, int msec);
            [DllImport("MasterRD.dll")]
            static extern int ISO15693_Inventorys(short icdev, out int pData, out int pLen);
            [DllImport("MasterRD.dll")]
            static extern int rf_get_device_number(out short icdev);
            [DllImport("MasterRD.dll")]
            static extern int ISO15693_Get_System_Information(short icdev, char model, out char pUID, out int pData, out int pLen);
     
            private int _status;
            private short _icdev = 0x0000;
            private int _pData;
            private int _pLen;
            private char _pUID;
     
            public Form1()
            {
                InitializeComponent();
     
                test();
            }
     
            private void test()
            {
                _status = rf_init_com(3, 19200);
     
                _status = rf_get_device_number(out _icdev);
                //MessageBox.Show(_icdev.ToString());
     
                //_status = rf_beep(_icdev, 1000);
     
                _status = ISO15693_Inventorys(_icdev, out _pData, out _pLen);
                //MessageBox.Show(_pData.ToString());
     
                if (_status == 0)
                {
                    ISO15693_Get_System_Information(_icdev, '1', out _pUID, out _pData, out _pLen);
                    MessageBox.Show(_pData.ToString()); // affiche : 990361856
                }
     
                //rf_ClosePort();
            }
        }
    }
    Le ID de la puce test est E00401007C3B07B9

    PS : Si un modérateur trouve un meilleur titre ne vous gêner pas à le changer.

  2. #2
    Membre actif
    Profil pro
    Inscrit en
    Février 2006
    Messages
    197
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Février 2006
    Messages : 197
    Points : 279
    Points
    279
    Par défaut
    Salut,
    Ton char pData[MAX_RF_BUFFER] est une tableau de char et pas un int.
    regarde par exemple ce poste :
    http://social.msdn.microsoft.com/For...to-c?forum=clr

    pour te donner une idée.

  3. #3
    Membre du Club
    Homme Profil pro
    Inscrit en
    Mai 2008
    Messages
    84
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Mai 2008
    Messages : 84
    Points : 60
    Points
    60
    Par défaut
    J'avais déjà essayé de le mettre en char[], mais j'avais ce message là :

    Une exception non gérée du type 'System.AccessViolationException' s'est produite dans test_RFID_reader.exe
    Informations supplémentaires*: Tentative de lecture ou d'écriture de mémoire protégée. Cela indique souvent qu'une autre mémoire est endommagée.

  4. #4
    Membre du Club
    Homme Profil pro
    Inscrit en
    Mai 2008
    Messages
    84
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Mai 2008
    Messages : 84
    Points : 60
    Points
    60
    Par défaut
    Dans le code en C++ j'ai :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    ISO15693_Inventorys(icdev,pData,&len);
    [...]
    m_edit_uid1.SetWindowTextEx(&pData[1],8);
    Mais en C# je ne trouve pas l'équivalent de '&variable' pour avoir la référence mémoire ?

  5. #5
    Membre du Club
    Homme Profil pro
    Inscrit en
    Mai 2008
    Messages
    84
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Mai 2008
    Messages : 84
    Points : 60
    Points
    60
    Par défaut
    Je viens juste d'apprendre que le "/$%"/ de id est en hexadecimal. Donc je dois trouver une bigintger et le convertir en hexa.

  6. #6
    Membre du Club
    Homme Profil pro
    Inscrit en
    Mai 2008
    Messages
    84
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Mai 2008
    Messages : 84
    Points : 60
    Points
    60
    Par défaut
    System.Numerics ne fonctionne pas ? J'ai ajouté la référance !!
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    using System.Runtime.InteropServices;
    using System.Numerics;
    
    namespace test_RFID_reader
    {
        public partial class Form1 : Form
        {
    
            [DllImport("MasterRD.dll")]
            static extern int rf_init_com(int port, int baud);
            [DllImport("MasterRD.dll")]
            static extern int rf_ClosePort();
            [DllImport("MasterRD.dll")]
            static extern int rf_beep(short icdev, int msec);
            [DllImport("MasterRD.dll")]
            static extern int ISO15693_Inventorys(short icdev, ref long pData, ref int pLen);
            [DllImport("MasterRD.dll")]
            static extern int rf_get_device_number(ref short icdev);
            [DllImport("MasterRD.dll")]
            static extern int ISO15693_Get_System_Information(short icdev, char model, ref char pUID, ref long pData, ref int pLen);
    
    
            private int _status;
            private short _icdev = 0x0000;
            private long _pData;
            private int _pLen;
            private char _pUID;
    
            public Form1()
            {
                InitializeComponent();
    
                test();
            }
    
            private void test()
            {
                _status = rf_init_com(3, 19200);
    
                _status = rf_get_device_number(ref _icdev);
                //MessageBox.Show(_icdev.ToString());
    
                //_status = rf_beep(_icdev, 1000);
    
                _status = ISO15693_Inventorys(_icdev, ref _pData, ref _pLen);
                //MessageBox.Show(_pData.ToString());
    
                if (_status == 0)
                {
                    ISO15693_Get_System_Information(_icdev, '1', ref _pUID, ref _pData, ref _pLen);
    
                    MessageBox.Show(_pData.ToString("X"));
                }
    
                //rf_ClosePort();
            }
        }
    }

  7. #7
    Membre du Club
    Homme Profil pro
    Inscrit en
    Mai 2008
    Messages
    84
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Mai 2008
    Messages : 84
    Points : 60
    Points
    60
    Par défaut
    Erreur stupide ... J'avais deux projets dans ma solution et j'ai mis la référance au mauvais projet.

  8. #8
    Membre du Club
    Homme Profil pro
    Inscrit en
    Mai 2008
    Messages
    84
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Mai 2008
    Messages : 84
    Points : 60
    Points
    60
    Par défaut
    Quelqu'un peut me dire comment on peut copier le contenu d'un pointeur ?

  9. #9
    Inactif  

    Homme Profil pro
    Ingénieur test de performance
    Inscrit en
    Décembre 2003
    Messages
    1 986
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 49
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Ingénieur test de performance
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Décembre 2003
    Messages : 1 986
    Points : 2 605
    Points
    2 605
    Par défaut
    Bonjour.

    Pour t'aider, il faut nous dire le type exact de la variable à passer à la dll.

    Si c'est un passage de string à char* ou WCHAR* :

    Ici une discussion récente :

    http://www.developpez.net/forums/d14...pencv-dll-cpp/

  10. #10
    Membre du Club
    Homme Profil pro
    Inscrit en
    Mai 2008
    Messages
    84
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Mai 2008
    Messages : 84
    Points : 60
    Points
    60
    Par défaut
    C'est justement le genre d'info qu'il me manque, la doc qu'on ma fournie est en chinois. (C'est pas une métaphore, c'est des idéogrammes)

    Pour l'instant je suis capable d'avoir une partie du ID. J'ai découvert que le string était plutôt un nombre hexadécimal (E00401007C3B07B9), mais quand je converti le long que la fonction me renvoie j'obtiens (401007C3B07B900). Je crois qu'un long n'est pas assez long, donc j'ai essayé un BigInteger, mais il me retourne tout les bytes en mémoire de l'ordi, donc ça plante.

    Je cherche à lire en se moment un certain nombre de byte en mémoire, donc j'essais de donner un maximum à mon BigInteger ou bien aller lire directement les données dans la mémoire en utilisant les pointeurs.

    Si finalement je n'y arrive pas je vais utiliser le (401007C3B07B900) je vais enlever les (00) de la fin et ajouté (E00) au début. Les chances de bug sont faible, mais... Ce serait pas une super job !

  11. #11
    Inactif  

    Homme Profil pro
    Ingénieur test de performance
    Inscrit en
    Décembre 2003
    Messages
    1 986
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 49
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Ingénieur test de performance
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Décembre 2003
    Messages : 1 986
    Points : 2 605
    Points
    2 605
    Par défaut
    Bonjour.

    Si le nombre hexa ne loge pas dans dans une variable classique à cause de sa longueur, alors le type string peut-être le plus adapté. Je n'ai pas tout suivi, mais le passage par string semble obligatoire ici.

  12. #12
    Membre du Club
    Homme Profil pro
    Inscrit en
    Mai 2008
    Messages
    84
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Mai 2008
    Messages : 84
    Points : 60
    Points
    60
    Par défaut
    Un string ne fonctionne pas :
    Une exception non gérée du type 'System.AccessViolationException' s'est produite dans mscorlib.dll

    Informations supplémentaires*: Tentative de lecture ou d'écriture de mémoire protégée. Cela indique souvent qu'une autre mémoire est endommagée.
    ( Il y a un éditeur de texte pour les erreurs ? )

    Je crois que la fonction me retourne trop de donner jusqu'à utiliser de la mémoire protégée.

  13. #13
    Inactif  

    Homme Profil pro
    Ingénieur test de performance
    Inscrit en
    Décembre 2003
    Messages
    1 986
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 49
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Ingénieur test de performance
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Décembre 2003
    Messages : 1 986
    Points : 2 605
    Points
    2 605
    Par défaut
    Bonjour.

    Le passage du type string du C# au type WCHAR* du C++ est possible. Les liens ci-dessus expliquent comment faire.

    Avez-vous essayer ?

  14. #14
    Candidat au Club
    Profil pro
    Inscrit en
    Septembre 2009
    Messages
    2
    Détails du profil
    Informations personnelles :
    Localisation : Canada

    Informations forums :
    Inscription : Septembre 2009
    Messages : 2
    Points : 2
    Points
    2
    Par défaut
    Essaye de remplacer ton code par ça:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
     
    using System.Text;
     
     
    [DllImport("MasterRD.dll")]
    static extern int ISO15693_Inventorys(ushort icdev, out StringBuilder pData, out StringBuilder pLen);
     
     
    ushort _icdev = 0x0000;
    StringBuilder _pdata = new StringBuilder(255);
    StringBuilder _pLen = new StringBuilder(255);
     
     
    _status = ISO15693_Inventorys(_icdev, out _pData, out _pLen);

  15. #15
    Membre du Club
    Homme Profil pro
    Inscrit en
    Mai 2008
    Messages
    84
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Mai 2008
    Messages : 84
    Points : 60
    Points
    60
    Par défaut
    Non je n'ai pas essayé, je regarde ça. Je sais maintenant que l'information que je cherche est un entier no signé de 9 octets.

    ************** ajout *****************
    Est-t-il possible de force le paramètre out de retourner un array de byte d'une dimension de 9 ? Le premier octet est inutile pour ce que j'ai a faire.

    **************** réponse à SLIMTITO ***************
    Nop StringBuilder ne fonctionne pas il me retourne une valeur null

  16. #16
    Membre du Club
    Homme Profil pro
    Inscrit en
    Mai 2008
    Messages
    84
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Mai 2008
    Messages : 84
    Points : 60
    Points
    60
    Par défaut
    J'y suis arrivé ! J'étais capable de trouver 8 octets sur neuf, donc le premier. Donc, j'ai eu l'idée d’attribuer l'adresse du premier octet à un byte[9] dans l’espoir que les 8 autre indexe pointe automatiquement vers les 8 autres octet suivant et ça marche.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
     
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
     
    using System.Runtime.InteropServices;
    using System.Collections;
    using System.Numerics;
    using System.Timers;
     
    namespace test_RFID_reader
    {
        public partial class Form1 : Form
        {
     
            [DllImport("MasterRD.dll")]
            static extern int rf_init_com(int port, int baud);
            [DllImport("MasterRD.dll")]
            static extern int rf_ClosePort();
            [DllImport("MasterRD.dll")]
            static extern int rf_beep(short icdev, int msec);
            [DllImport("MasterRD.dll")]
            static extern int ISO15693_Inventory(short icdev, out byte pData, out byte pLen);
            [DllImport("MasterRD.dll")]
            static extern int rf_get_device_number(ref short icdev);
            [DllImport("MasterRD.dll")]
            static extern int rf_light(short icdev, byte color);
     
     
            private int _status;
            private short _icdev = 0x0000;
            private byte[] _pData = new byte[9];
            private byte _pLen;
     
            private System.Timers.Timer aTimer = new System.Timers.Timer(10*5);
     
            public Form1()
            {
                InitializeComponent();
     
                test();
            }
     
            private void test()
            {
                aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
     
                _status = rf_init_com(3, 19200);
     
                _status = rf_get_device_number(ref _icdev);
     
     
                _status = ISO15693_Inventory(_icdev, out _pData[0], out _pLen);
                if (_status == 0)
                {
                    _status = rf_beep(_icdev, 10);
                    rf_light(_icdev, 2);
                    aTimer.Enabled = true;
     
                    string strHexByte;
                    string id = "";
                    for (var i = _pData.Length - 1; i > 0; i--)
                    {
                        strHexByte = _pData[i].ToString("X");
                        if (strHexByte.Length == 1)
                            id += "0";
                        id += strHexByte;
                    }
                    MessageBox.Show(id);
                }
     
                rf_ClosePort();
            }
     
            private void OnTimedEvent(object source, ElapsedEventArgs e)
            {
                rf_light(_icdev, 3);
                aTimer.Enabled = false;
            }
        }
    }

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 4
    Dernier message: 05/02/2006, 10h53
  2. Réponses: 9
    Dernier message: 19/12/2005, 16h41
  3. question idiote sur terme utilisé dans les offres
    Par coyott dans le forum Emploi
    Réponses: 4
    Dernier message: 24/08/2005, 17h16
  4. DLL Visual C++ appelée dans un programme Visual Basic
    Par marseillais57 dans le forum MFC
    Réponses: 7
    Dernier message: 21/07/2005, 14h57
  5. [MFC][DLL]Dialog Avec ActiveX dans une DLL ?
    Par matazz dans le forum MFC
    Réponses: 1
    Dernier message: 16/05/2005, 16h36

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo