Imports System.Threading Imports System.Math Imports EgtUILib Friend Class MainWindowModel #Region "FIELDS" ' Mutex che permette di controllare il numero massimo di istanze aperta contemporaneamente Private m_objMutex As New Mutex ' Variabile che indica il numero di istanze aperte del programma Friend m_nInstance As Integer = 0 ' Path cartella Data Friend m_sDataRoot As String = String.Empty ' Livello dell'utilizzatore Friend m_nUserLevel As Integer = 1 ' '' Livello della chiave inserita nel PC ''Friend m_nKeyLevel As Integer = 0 ' '' Opzioni attive sulla chiave ''Friend m_nKeyOptions As UInteger = 0 Private m_nDebug As Integer = 0 #End Region #Region "CONSTRUCTOR" Sub New() ' Impostazione path radice per i dati m_sDataRoot = System.AppDomain.CurrentDomain.BaseDirectory If EgtUILib.GetPrivateProfileString(S_DATA, K_DATAROOT, "", m_sDataRoot, m_sDataRoot & "\" & DAT_FILE_NAME) = 0 Then m_sDataRoot = System.AppDomain.CurrentDomain.BaseDirectory End If ' Impostazione direttorio di configurazione IniFile.m_sConfigDir = m_sDataRoot & "\" & CONF_DIR ' Impostazione direttorio per file temporanei IniFile.m_sTempDir = m_sDataRoot & "\" & TEMP_DIR ' Impostazione path Ini file IniFile.m_sIniFile = IniFile.m_sConfigDir & "\" & INI_FILE_NAME ' Verifico indice di istanza ManageIstance() ' Imposto tipo di chiave EgtSetLockType(KEY_TYPE.HW) ' Leggo e imposto chiave di protezione Dim sLicFileName As String = String.Empty GetPrivateProfileString(S_GENERAL, K_LICENCE, LIC_FILE_NAME, sLicFileName) Dim sLicFile As String = m_sConfigDir & "\" & sLicFileName Dim sKey As String = String.Empty EgtUILib.GetPrivateProfileString(S_LICENCE, K_KEY, "", sKey, sLicFile) EgtSetKey(sKey) ' Recupero livello e opzioni della chiave Dim bKey As Boolean = EgtGetKeyLevel(3279, 16, 1, IniFile.m_nKeyLevel) And EgtGetKeyOptions(3279, 16, 1, IniFile.m_nKeyOptions) 'Inizializzazione generale di EgtInterface m_nDebug = GetPrivateProfileInt(S_GENERAL, K_DEBUG, 0) Dim sLogFile As String = IniFile.m_sTempDir & "\" & GENLOG_FILE_NAME.Replace("#", m_nInstance.ToString()) Dim sLogMsg As String = "User " & Environment.UserName & "\" & Environment.MachineName & " (" & m_nInstance.ToString() & ")" & vbLf & My.Application.Info.Description.ToString() & " ver. " & My.Application.Info.Version.Major.ToString() & "." & My.Application.Info.Version.Minor.ToString() & (ChrW(97 - 1 + My.Application.Info.Version.Build)).ToString() & My.Application.Info.Version.Revision.ToString() EgtInit(m_nDebug, sLogFile, sLogMsg) ' carico direttorio doors GetPrivateProfileString(S_DOORS, K_BASEDIR, "", IniFile.m_sDoorsDirPath) ' L'inidirro del file di Default IniFile.m_DefaultIniFile = IniFile.m_sDoorsDirPath & "\Compo\Default.ini" 'Inizializzo OptionModule OptionModule.InitOptionModule() ' imposto dir font Nfe e font default Dim sNfeDir As String = String.Empty GetPrivateProfileString(S_GEOMDB, K_NFEFONTDIR, "", sNfeDir) Dim sDefFont As String = String.Empty GetPrivateProfileString(S_GEOMDB, K_DEFAULTFONT, "", sDefFont) EgtSetFont(sNfeDir, sDefFont) ' imposto dir di default per libreria Lua e lancio libreria di base Dim sLuaLibsDir As String = String.Empty GetPrivateProfileString(S_LUA, K_LIBSDIR, "", sLuaLibsDir) EgtSetLuaLibs(sLuaLibsDir) Dim sLuaBaseLib As String = String.Empty GetPrivateProfileString(S_LUA, K_BASELIB, "EgtBase", sLuaBaseLib) EgtLuaRequire(sLuaBaseLib) ' Imposto path IniFile EgtSetIniFile(IniFile.m_sIniFile) End Sub #End Region #Region "METHODS" ''' ''' Funzione che permette di gestire il numero di istanze del programma attive contemporaneamente ''' Private Sub ManageIstance() Dim bCreated As Boolean Try m_objMutex = New Mutex(False, "Global\EgtDOORCreator", bCreated) Catch bCreated = False End Try If bCreated Then ' Prima istanza m_nInstance = 1 ' Aggiorno stato istanze attive WritePrivateProfileString(S_GENERAL, K_INSTANCES, m_nInstance.ToString) Else ' Leggo il massimo numero di istanze ammesse Dim nMaxInst As Integer = GetPrivateProfileInt(S_GENERAL, K_MAXINST, 1) nMaxInst = Max(1, Min(nMaxInst, 32)) ' Cerco il primo indice di istanza libero (max 32) Dim nTmp As Integer = GetPrivateProfileInt(S_GENERAL, K_INSTANCES, 0) m_nInstance = 1 Dim nMask As Integer = 1 While (nTmp And nMask) <> 0 And m_nInstance <= m_nInstance m_nInstance += 1 nMask *= 2 End While ' Se l'indice supera il massimo If m_nInstance > nMaxInst Then ' porto in primo piano la prima istanza Dim bFound As Boolean = False ' processi del programma a 32 bit Dim localProc As Process() = Process.GetProcessesByName("EgtDOORCreatorR32") For Each p As Process In localProc If p.Id <> Process.GetCurrentProcess().Id Then bFound = True ShowWindow(p.MainWindowHandle, SW.SHOWMAXIMIZED) Exit For End If Next ' se non trovati processi a 32 bit provo a 64 bit If Not bFound Then localProc = Process.GetProcessesByName("EgtDOORCreatorR64") For Each p As Process In localProc If p.Id <> Process.GetCurrentProcess().Id Then bFound = True ShowWindow(p.MainWindowHandle, SW.SHOWMAXIMIZED) Exit For End If Next End If ' esco dal programma End End If ' Aggiorno stato istanze attive nTmp += (1 << (m_nInstance - 1)) WritePrivateProfileString(S_GENERAL, K_INSTANCES, nTmp.ToString()) End If End Sub #End Region End Class