Imports System.Runtime.InteropServices
Imports System.Security

Public Module EgtInterface

#If DEBUG Then
   Const EgtIntDll32 As String = "EgtBasisD32.dll"
#Else
   Const EgtIntDll32 As String = "EgtBasisR32.dll"
#End If

   <DllImport(EgtIntDll32, CharSet:=CharSet.Unicode, EntryPoint:="EgtSetKey"), SuppressUnmanagedCodeSecurity()>
   Public Function EgtSetKey(sKey As String) As Boolean
   End Function

   <DllImport(EgtIntDll32, CharSet:=CharSet.Unicode, EntryPoint:="EgtGetKeyLevel"), SuppressUnmanagedCodeSecurity()>
   Public Function EgtGetKeyLevel(nProd As Integer, nVer As Integer, nLev As Integer, ByRef nKLev As Integer) As Boolean
   End Function

   <DllImport(EgtIntDll32, CharSet:=CharSet.Unicode, EntryPoint:="EgtGetKeyOptions"), SuppressUnmanagedCodeSecurity()>
   Public Function EgtGetKeyOptions(nProd As Integer, nVer As Integer, nLev As Integer,
                                    ByRef nOpt1 As UInteger, ByRef nOpt2 As UInteger) As Boolean
   End Function

   <DllImport(EgtIntDll32, CharSet:=CharSet.Unicode, EntryPoint:="EgtGetKeyLeftDays"), SuppressUnmanagedCodeSecurity()>
   Public Function EgtGetKeyLeftDays(ByRef nLeftDays As Integer) As Boolean
   End Function

   <DllImport(EgtIntDll32, CharSet:=CharSet.Unicode, EntryPoint:="EgtGetKeyAssLeftDays"), SuppressUnmanagedCodeSecurity()>
   Public Function EgtGetKeyAssLeftDays(ByRef nAssLeftDays As Integer) As Boolean
   End Function

   <DllImport(EgtIntDll32, CharSet:=CharSet.Unicode, EntryPoint:="EgtGetKeyOptLeftDays"), SuppressUnmanagedCodeSecurity()>
   Public Function EgtGetKeyOptLeftDays(ByRef nOptLeftDays As Integer) As Boolean
   End Function

   <DllImport(EgtIntDll32, CharSet:=CharSet.Unicode, EntryPoint:="EgtFreeMemory"), SuppressUnmanagedCodeSecurity()>
   Public Function EgtFreeMemory(sB As IntPtr) As Boolean
   End Function

   <DllImport(EgtIntDll32, CharSet:=CharSet.Unicode, EntryPoint:="EgtLoadMessages"), SuppressUnmanagedCodeSecurity()>
   Public Function EgtLoadMessages(sMsgFilePath As String) As Boolean
   End Function

   <DllImport(EgtIntDll32, CharSet:=CharSet.Unicode, EntryPoint:="EgtGetMsg"), SuppressUnmanagedCodeSecurity()>
   Private Function EgtGetMsg_32(nId As Integer) As IntPtr
   End Function
   Public Function EgtMsg(nId As Integer) As String
      Return Marshal.PtrToStringUni(EgtGetMsg_32(nId))
      ' Non è necessario liberare la memoria nativa perchè usa un buffer statico
   End Function

   <DllImport(EgtIntDll32, CharSet:=CharSet.Unicode, EntryPoint:="EgtInitLogger"), SuppressUnmanagedCodeSecurity()>
   Public Function EgtInitLogger(nDebug As Integer, sLogFilePath As String) As Boolean
   End Function

   <DllImport(EgtIntDll32, CharSet:=CharSet.Unicode, EntryPoint:="EgtOutLog"), SuppressUnmanagedCodeSecurity()>
   Private Function EgtOutLog(sMsg As String, nDebugLevel As Integer) As Boolean
   End Function

   <DllImport(EgtIntDll32, CharSet:=CharSet.Unicode, EntryPoint:="EgtGetStringUtf8FromIni"), SuppressUnmanagedCodeSecurity()>
   Private Function EgtGetStringUtf8FromIni_32(sSec As String, sKey As String, sDef As String, ByRef sVal As IntPtr, sIniFile As String) As Boolean
   End Function
   Public Function EgtGetStringUtf8FromIni(sSec As String, sKey As String, sDef As String, ByRef sVal As String, sIniFile As String) As Boolean
      Dim psVal As IntPtr
      Dim bOk As Boolean = EgtGetStringUtf8FromIni_32(sSec, sKey, sDef, psVal, sIniFile)
      If bOk Then
         sVal = Marshal.PtrToStringUni(psVal)
         EgtFreeMemory(psVal)
      Else
         sVal = String.Empty
      End If
      Return bOk
   End Function

   <DllImport(EgtIntDll32, CharSet:=CharSet.Unicode, EntryPoint:="EgtWriteStringUtf8ToIni"), SuppressUnmanagedCodeSecurity()>
   Public Function EgtWriteStringUtf8toIni(sSec As String, sKey As String, sVal As String, sIniFile As String) As Boolean
   End Function

End Module
