C# et dll C : Attempted to read or write protected memory

louvinon Messages postés 6 Date d'inscription jeudi 10 septembre 2009 Statut Membre Dernière intervention 10 septembre 2009 - 16 mars 2009 à 13:15
Lutinore Messages postés 3246 Date d'inscription lundi 25 avril 2005 Statut Membre Dernière intervention 27 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 :



DllExport UTI_RC Fonction1(VOID * Handle, BYTE * Ano) {...}
extern "C" DllExport UTI_RC CreateUtiPInvoke(void ** phandle, DSPType FnDisplay)  {...}
extern "C" DllExport UTI_RC DeleteUtiPInvoke( void * handle ) {...}
extern "C" DllExport UTI_RC StreamSet( void * handle, LPCSTR szAlias, BYTE BufferIn[], int iSizeBufferIn ) {...}
extern "C" DllExport UTI_RC StreamGet( void * handle, LPCSTR szAlias, BYTE** pBufferOut, int* piSizeBufferOut ) {...}
DllExport UTI_RC SYSOUTGet( void * handle, BYTE** pBufferOut, int* piSizeBufferOut ) {...}
DllExport UTI_RC CreateAlias (void * handle, LPCSTR szAlias, LPCSTR szPDSName, LPCSTR szMemberName) {...}
DllExport UTI_RC DeleteAlias (void * handle, LPCSTR szAlias) {...}
DllExport UTI_RC Clear (void * handle) {...}
DllExport UTI_RC StreamGetBana (void * handle) {...}
extern "C" DllExport UTI_RC StreamRename( void * handle, LPCSTR szAlias1,LPCSTR szAlias2) {...}
DllExport UTI_RC Conv_LB2PDF(void * handle,CHAR *imgpath, ULONG  pdfoptions) {...}
DllExport int UTICNVLB (CNVLB_ERREUR * erreur) {...}
DllExport UTI_RC ExceptionInfoGet(void * handle, UTI_EXCEPTION_INFO * pException) {...}
DllExport UTI_RC SYSOUTGet( void * handle, BYTE** pBufferOut, int* piSizeBufferOut ) {...}


Il y a aussi du Maschall :



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.


Merci de m'orienter.


 


 


 


 


 

1 réponse

Lutinore Messages postés 3246 Date d'inscription lundi 25 avril 2005 Statut Membre Dernière intervention 27 octobre 2012 41
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.
0
Rejoignez-nous