survcopt
Messages postés224Date d'inscriptionmardi 27 mai 2003StatutMembreDernière intervention13 mai 2022
-
4 juin 2007 à 16:42
survcopt
Messages postés224Date d'inscriptionmardi 27 mai 2003StatutMembreDernière intervention13 mai 2022
-
4 juin 2007 à 19:10
Bonjour j'ai une structure de type
public struct GM_Rectangle_t
{
public double MinX;
public double MinY;
public double MaxX;
public double MaxY;
};
Et je voudrais declarer un un pointeur sur cette structure car j'ai toujours ma Super dll en C qui me réclame comme paramètre :
const GM_Rectangle_t* aWorldBounds,
donc apres avoir rempli ma structure
GM_Rectangle_t WorldBounds;
WorldBounds.MinX = 1;
WorldBounds.MaxX = 2;
WorldBounds.MinY = 1;
WorldBounds.MaxY = 2;
Est ce que la fonction
IntPtr WorldBoundsPtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(WorldBounds));
Me déclare correctement mon pointeur ?
Comment puis-je verifier le contenu de ce que pointe mon pointeur ?
Lutinore
Messages postés3246Date d'inscriptionlundi 25 avril 2005StatutMembreDernière intervention27 octobre 201241 4 juin 2007 à 18:32
oups j'ai oublié des parenthèses :
// passage par adresse mais les structs doivent être déclarées sur la pile,
// c-à-d dans la fonction et pas en champs de la classe par exemple.
unsafe
{
GM_ConvertCoordPixelToGround( 1, 1, out x, out y, ( IntPtr )( &worldBounds ), ( IntPtr )( &pixelRect ) );
}
Lutinore
Messages postés3246Date d'inscriptionlundi 25 avril 2005StatutMembreDernière intervention27 octobre 201241 4 juin 2007 à 17:11
Salut, là ton pointeur il ne contient rien du tout, tu as seulement alloué un bloc de mémoire non managée égale à la taille de ta structure, maintenant faudrait la remplir avec StructureToPtr comme je t'ai montré >ici< mais tu te compliques la vie, pour passer une structure comme un pointeur suffit d'utiliser le mot clé 'ref' et c'est le runtime qui va s'occuper tout seul du marshaling.
survcopt
Messages postés224Date d'inscriptionmardi 27 mai 2003StatutMembreDernière intervention13 mai 20223 4 juin 2007 à 17:23
Merci à toi mais la ca ne marche pas ...
Et je commence à tourner en rond avec cette dll
La déclaration dans le .h est
GM_Error_t32 GM_ConvertCoordPixelToGround
(
sint32 aPixelX, // IN: X Coord in pixel space
sint32 aPixelY, // IN: Y Coord in pixel space
double* aGroundX, // OUT: X Coord in current projection
double* aGroundY, // OUT: Y Coord in current projection
const GM_Rectangle_t* aWorldBounds, // IN: World bounds to convert from or NULL for last drawn
const GM_PixelRect_t* aPixelRect // IN: Pixel bounds to convert from or NULL for last drawn
);
je la déclare
[
DllImport(DLLFileName, EntryPoint =
"GM_ConvertCoordPixelToGround")]
public
static
extern
GM_Error_t32 GM_ConvertCoordPixelToGround(
Int32 aPixelX,
Int32 aPixelY,
out
double aGroundX,
out
double aGroundY,
ref
GM_Rectangle_t aWorldBounds,
ref
GM_Rectangle_t aPixelRect);
j'ai utilisé le mot ref pour passer ma structure par pointeur et je l'appelle ainsi
LastGMError =
GlobalMapperDLL.GM_ConvertCoordPixelToGround(e.X, e.Y,
out XGround,
out YGround,
ref WorldBounds,
ref PixelRect);
Et la dll me renvoi via LastGMError
comme quoi les paramètres ne sont pas bon.
Je pense que la procedure est bonne, si pour toi c'est Ok j'irai voir du côté de mes paramètres.
Merci
survcopt
Messages postés224Date d'inscriptionmardi 27 mai 2003StatutMembreDernière intervention13 mai 20223 4 juin 2007 à 18:00
Pour passer un pointeur nul il faut changer la déclaration
public
static
extern
GM_Error_t32 GM_ConvertCoordPixelToGround(
Int32 aPixelX,
Int32 aPixelY,
out
double aGroundX,
out
double aGroundY,
IntPtr aWorldBounds,
IntPtr aPixelRect);
et ensuite lancer
IntPtr
WorldBoundsPtr =
new
IntPtr(0);
IntPtr PixelRectPtr =
new
IntPtr(0);
LastGMError =
GlobalMapperDLL.GM_ConvertCoordGroundToPixel(XGround, YGround,
out X,
out Y ,
ref WorldBounds,
ref PixelRect);
Comme ca il n'y a plus de probleme ca marche
Lutinore
Messages postés3246Date d'inscriptionlundi 25 avril 2005StatutMembreDernière intervention27 octobre 201241 4 juin 2007 à 18:31
Il faut changer la déclaration comme tu l'as fait.. et même si ça change rien, IntPtr.Zero c'est mieux que new IntPtr( 0 ).
// passage par adresse mais les structs doivent être déclarées sur la pile,
// c-à-d dans la fonction et pas en champs de la classe par exemple.
unsafe
{
GM_ConvertCoordPixelToGround( 1, 1, out x, out y, ( IntPtr )&worldBounds, ( IntPtr )&pixelRect );
}