Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
C# / VB.NET - Puntatore a Delegate
Forum - C# / VB.NET - Puntatore a Delegate

Avatar
lorenzo (Normal User)
Guru


Messaggi: 1178
Iscritto: 15/04/2008

Segnala al moderatore
Postato alle 14:21
Sabato, 20/02/2010
Codice sorgente - presumibilmente VB.NET

  1. Private Delegate Function GetName() As String
  2.     Private Delegate Function GetDesc() As String
  3.  
  4.     Public Delegate Sub Start()
  5.  
  6.  
  7. <DllImport("kernel32")> _
  8.     Private Shared Function LoadLibrary(ByVal name As String) As IntPtr
  9.     End Function
  10.  
  11.     <DllImport("kernel32")> _
  12.     Private Shared Function FreeLibrary(ByVal hmodule As IntPtr) As Boolean
  13.     End Function
  14.  
  15.     <DllImport("kernel32")> _
  16.     Private Shared Function GetProcAddress(ByVal hmodule As IntPtr, ByVal name As String) As IntPtr
  17.     End Function
  18.  
  19.     Private Function OpenPlugin(ByVal path As String) As PluginInfo
  20.         Dim ptr As IntPtr = LoadLibrary(path)
  21.         Dim paddr As IntPtr
  22.         Dim pinfo As PluginInfo = Nothing
  23.  
  24.  
  25.         If ptr.ToInt32() = 0 Then
  26.             MsgBox("Errore caricamento plugin")
  27.         Else
  28.             Try
  29.                 pinfo = New PluginInfo
  30.                 paddr = GetProcAddress(ptr, "GetName")
  31.                 pinfo.name = Marshal.GetDelegateForFunctionPointer(paddr, GetType(GetName)).DynamicInvoke
  32.                 pinfo.desc = Marshal.GetDelegateForFunctionPointer(GetProcAddress(ptr, "GetDesc"), GetType(GetDesc)).DynamicInvoke
  33.                 pinfo.start = Marshal.GetDelegateForFunctionPointer(GetProcAddress(ptr, "Start"), GetType(Start))
  34.                 pinfo.active = False
  35.                 'FreeLibrary(ptr)
  36.             Catch ex As Exception
  37.                 Application.Exit()
  38.             End Try
  39.         End If
  40.  
  41.         OpenPlugin = pinfo
  42.     End Function



dove pinfo è una struttura del tipo:
Codice sorgente - presumibilmente VB.NET

  1. Structure PluginInfo
  2.         Dim name As String
  3.         Dim desc As String
  4.         Dim active As Boolean
  5.         Dim start As Start
  6.     End Structure



il problema è che mi segnala sempre, alla prima chiamata del primo metodo di Invoke(quello di GetName), un'eccezione di accesso in lettura/scrittura a memoria protetta. Ho provato a chiamare da un programmino in C++ le stesse funzioni dinamicamente e funzionano alla perfezione.

Che dite?


ps: la dll è così
Codice sorgente - presumibilmente C++

  1. //file .h
  2. extern "C"
  3. {
  4.         _declspec(dllexport) char * GetName();
  5.         _declspec(dllexport) char * GetDesc();
  6.         _declspec(dllexport) void Start();
  7. }
  8. //file .cpp
  9. extern "C"
  10. {
  11.         char * GetName()
  12.         {
  13.                 return "Plugin di test";
  14.         }
  15.         char * GetDesc()
  16.         {
  17.                 return "Questo test serve per vedere se tutto funziona";
  18.         }
  19.         void Start()
  20.         {
  21.                 int i = 0;
  22.                 while(i++ < 10) MessageBox(0, "test", "test", 0);
  23.         }
  24. }


Ultima modifica effettuata da lorenzo il 20/02/2010 alle 14:25
PM Quote