nybibi
Messages postés5Date d'inscriptionvendredi 7 décembre 2007StatutMembreDernière intervention11 décembre 2007
-
10 déc. 2007 à 11:10
nybibi
Messages postés5Date d'inscriptionvendredi 7 décembre 2007StatutMembreDernière intervention11 décembre 2007
-
11 déc. 2007 à 10:18
Bonjour! Je débute en visual c# 2005 et visual c++2005(ou 6), je dois mettre en place une appli avec ces deux langages qui va gérer un pointeur de ronde... Seulement je n'arrive pas à charger les dll qui me sont fournit afin d'en exploiter les fonctions, j'ai essayé différentes façon à partir de bouquin mais rien n'y fait, je n'avance pas d'un poil. Est ce que qqun aurait une idée sur le bon code à mettre en place pour cette appli?
Lutinore
Messages postés3246Date d'inscriptionlundi 25 avril 2005StatutMembreDernière intervention27 octobre 201241 10 déc. 2007 à 13:31
Salut, on ne peut pas utiliser si facilement des DLLs entre le C# et le C/C++, les ajouter simplement au projet ne fontionne pas, il faut passer par P/Invoke ( DllImport etc.. ) :
Lutinore
Messages postés3246Date d'inscriptionlundi 25 avril 2005StatutMembreDernière intervention27 octobre 201241 11 déc. 2007 à 00:14
En C# dans 99% des cas on n'utilise pas LoadLibrary/FreeLibrary, c'est l'attribut DllImport qui s'en charge. Attention aux handles qui devraient être des IntPtr et non des int et aux char qui sont codés sur 2 octets en C# etc..
Pour passer une structure tu dois complètement la redéfinir dans le code C# :
[ StructLayout( LayoutKind.Sequential ) ]
private struct MyStruct
{
int x;
int y;
nybibi
Messages postés5Date d'inscriptionvendredi 7 décembre 2007StatutMembreDernière intervention11 décembre 2007 10 déc. 2007 à 16:03
Merci. Entretemps j'ai réussi à charger ma dll sur c# mais il ya une erreur qui apparait quand j'essai de connecter mon appli au pointeur de ronde. Mais bon! Ceci est du au pointeur de ronde et non au programme. Voisi le début de mon code sachant que l'appli n'est pas fini:
using
System;<?xml:namespace prefix o ns "urn:schemas-microsoft-com:office:office" /??>
using
System.Collections.Generic;
using
System.Text;
using
System.Runtime.InteropServices;
/*
extern "C"{
struct ConfigStruct{
char internal_id[15]; // read only
char user_info[17]; // read/write
uchar config_data[32]; // read/write
uchar probe_time[20]; // read/write "mm/dd/yyyy hh:mm:ss" format
uchar product_id; // read only
uchar sw_version; // read only
uchar hw_version; // read only
ulong battery_voltage; // read only
ulong next_event_addr; // read only
ulong events_since_reset; // read/write (reset to 0, clears memory)
uchar vib_duration; // read/write
uchar vib_intensity; // read/write
uchar rep_read_delay; // read/write
uchar irda_trigger_count; // read/write
uchar irda_pause; // read/write
uchar irda_duration; // read/write
ulong low_bat_threshold; // read/write
bool irda_short_enable; // read/write
bool data_read_enable; // read/write }; }
*/
namespace
Pulsestardll
{
class
Program
{
[DllImport("kernel32")]
public
extern
static
int LoadLibrary(string lpLibFileName);
[DllImport("kernel32")]
public
extern
static
int SetDllDirectory(string lpLibFileName);
[DllImport("kernel32")]
public
extern
static
bool FreeLibrary(int hLibModule);
[DllImport("kernel32", CharSet = CharSet.Ansi)]
public
extern
static
int GetProcAddress(int hModule, string lpProcName);
[DllImport("Invoke", CharSet = CharSet.Unicode)]
public
extern
static
int InvokeFunc(int funcptr, int hwnd, string message, string title, int flags);
[DllImport("pulsestar")]
public
extern
static
int psOpenPort(ushort a,Int32 b,bool c);
[DllImport("pulsestar")]
public
extern
static
unsafe
int psWakeProbe(char[] a, Int32 b);
// public extern static unsafe int psWakeProbe(byte* a, Int32 b);
[DllImport("pulsestar")]
public
extern
static
unsafe
int psReadConfig(byte[] tab);
[DllImport("pulsestar")]
public
extern
static
int psClosePort();
static
void Main(string[] args)
{
SetDllDirectory("c:\");
int hdll = LoadLibrary("ps_comm");
int hdll2 = LoadLibrary("pulsestar");
unsafe
{
byte[] confTab = newbyte[98];
int a = psOpenPort(1, 1, false);
if (a != 1000)
{
Console.WriteLine("Erreur d'ouverture de port, réinitialisation du programme");
FreeLibrary(hdll);
FreeLibrary(hdll2);
Console.ReadKey();
return;
}
char[] strb = "50020A468E61B6".ToCharArray();
a = psWakeProbe(strb, 10);
if
(a != 1000)
{
Console.WriteLine("Erreur de connection");
a = psClosePort();
if (a != 1000)
{
Console.WriteLine("Erreur de fermeture de port");
}
FreeLibrary(hdll);
FreeLibrary(hdll2);
Console.ReadKey();
return;
}
a = psReadConfig(confTab);
if (a != 1000)
{
Console.WriteLine("Impossible de lire les données");
FreeLibrary(hdll);
FreeLibrary(hdll2);
Console.ReadKey();
return;
}
for (int i = 0; i < 98; i++) { }
int z = psClosePort();
if (a != 1000)
{
Console.WriteLine("Erreur de fermeture de port");
FreeLibrary(hdll);
}
Voici la déclaration de l'une des fonctions:
publicexternstaticint psOpenPort(ushort a,Int32 b,bool c);
Mais je n'arrive pas encore à créer un type de paramètre("configStruct") pour une des autres fonctions tel que:
publicexternstaticint psReadConfig (ConfigStruct*cstruct);