C# et dll C : Attempted to read or write protected memory
louvinon
Messages postés6Date d'inscriptionjeudi 10 septembre 2009StatutMembreDernière intervention10 septembre 2009
-
16 mars 2009 à 13:15
Lutinore
Messages postés3246Date d'inscriptionlundi 25 avril 2005StatutMembreDernière intervention27 octobre 2012
-
16 mars 2009 à 16:45
Bonjour,
j'ai récupéré un projet dans ma société qui cause beaucoup de problèmes.
En effet, il y a souvent une exception "Attempted to read or write protected memory. This is often an indication that other memory has been corrupted".
C'est un site web C# (nous sommes maintenant sous Visual Studio 2005 Framewrok 2.0, avant nous étions en 2003 FrameWork 1.1 et il me semble pas qu'on avait ce problème) qui importe une DLL C.
Après quelques lectures sur google et le forum, j'ai lu que cela pouvait venir :
- de l'importation
-de la sécurité accrue avec le framework 2.0
J'ai besoin de votre aide car là, je n'y arrive plus, et les heureus sup ne suffisent pas :sm:
Déjà, je vous montre comment est géré l'importation pouir que vous puissiez m'informer si des trucs clochent.
Dans le code C#, voici comment sont déclarées les fonctions de la DLL :
[ DllImport( maDLL.dll )]
private static extern int Fonction1(IntPtr handle, byte[] Fn1);
[ DllImport( maDLL.dll )]
private static extern int CreateUtiPInvoke(out IntPtr handle, DSPType FnDisplay);
[ DllImport( maDLL.dll )]
private static extern int DeleteUtiPInvoke( IntPtr handle );
[ DllImport( maDLL.dll )]
private static extern int StreamSet( IntPtr handle, string Alias, byte[] BufferIn, int iSizeBufferIn );
[ DllImport( maDLL.dll )]
private static extern int StreamGet( IntPtr handle, string Alias, out IntPtr BufferOut, out int iSizeBufferOut );
[ DllImport( maDLL.dll )]
private static extern int SYSOUTGet( IntPtr handle, out IntPtr BufferOut, out int iSizeBufferOut );
[ DllImport( maDLL.dll )]
private static extern int CreateAlias( IntPtr handle, string Alias, string PDSName, string MemberName );
[ DllImport( maDLL.dll )]
private static extern int DeleteAlias( IntPtr handle, string Alias);
[ DllImport( maDLL.dll )]
private static extern int Clear( IntPtr handle);
[ DllImport( maDLL.dll )]
private static extern int StreamGetBana( IntPtr handle);
[ DllImport( maDLL.dll )]
private static extern int StreamRename( IntPtr handle, string Alias1, string Alias2);
[ DllImport( maDLL.dll )]
private static extern int Conv_LB2PDF( IntPtr handle, string ImgPath, ulong Options);
[ DllImport( maDLL.dll )]
private static extern int Uticnvlb (IntPtr handle, byte[] Fn1);
[ DllImport( UtiPInvoke.maDLL.dll )]
private static extern int ExceptionInfoGet( IntPtr handle, out UTI_EXCEPTION_INFO e);
[ DllImport( UtiPInvoke.maDLL.dll )]
private static extern int SYSOUTGet( IntPtr handle, out IntPtr BufferOut, out int iSizeBufferOut );
Dans le projet construisant la DLL (il est dans des fichiers .cpp mais écrit en majorité en c), voici les définitions :
typedef _Packed struct
{
UTI_RC netrc;
int rc;
int iLine;
char szType[15+1];
char szMsg[255+1];
char szFunction[63+1];
char szFile[255+1];
} UTI_EXCEPTION_INFO;
[ StructLayout( LayoutKind.Sequential)]
public struct UTI_EXCEPTION_INFO
{
public int netrc;
public int rc;
public int Line;
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=16 )]
public string strType;
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=256 )]
public string strMsg;
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=64 )]
public string strFunction;
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=256 )]
public string strFile;
} ;
N'hésitez surtout pas à me demander plus de renseignements.
Je dois corriger ce problème au plus vite car il est vraiment très problématique.
Lutinore
Messages postés3246Date d'inscriptionlundi 25 avril 2005StatutMembreDernière intervention27 octobre 201241 16 mars 2009 à 16:45
Salut, y'a bcp de code là.. faudrait épurer un peu et juste cibler le code qui lève une exception. Peut être un problème d'alignement, trouve la définition de _Packed dans le header C++ stp.