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 ' Path cartella Config Friend m_sConfigDir As String = String.Empty ' Path cartella Temp Friend m_sTempDir 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 m_sConfigDir = m_sDataRoot & "\" & CONF_DIR ' Impostazione direttorio per file temporanei m_sTempDir = m_sDataRoot & "\" & TEMP_DIR ' Impostazione path Ini file IniFile.m_sIniFile = m_sConfigDir & "\" & INI_FILE_NAME ' Impostazione path CompoIni file IniFile.m_sCompoIniFile = m_sConfigDir & "\" & COMPOINI_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 = 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) ' Leggo direttorio dei messaggi (se manca uso direttorio di configurazione) Dim sMsgDir As String = String.Empty If GetPrivateProfileString(S_GENERAL, K_MESSAGESDIR, "", sMsgDir) = 0 Then sMsgDir = m_sConfigDir End If ' Leggo elenco lingue disponibili da file ini Dim nIndex As Integer = 1 Dim ReadLanguage As Language = GetPrivateProfileLanguage(S_LANGUAGES, K_LANGUAGE & nIndex) While Not IsNothing(ReadLanguage) 'OptionModule.m_LanguageList.Add(ReadLanguage) nIndex += 1 ReadLanguage = GetPrivateProfileLanguage(S_LANGUAGES, K_LANGUAGE & nIndex) End While ' Inizializzo OptionModule 'OptionModule.InitOptionModule() ' Leggo file messaggi Dim sMsgName As String = String.Empty GetPrivateProfileString(S_GENERAL, K_MESSAGES, "", sMsgName) Dim sMsgFilePath As String = sMsgDir & "\EgalTechEng.txt" 'For Each Language In OptionModule.m_LanguageList ' If Language.Name = sMsgName Then ' OptionModule.m_SelectedLanguage = Language ' sMsgFilePath = sMsgDir & "\" & Language.FilePath ' End If 'Next If Not EgtLoadMessages(sMsgFilePath) Then EgtOutLog("Error in EgtLoadMessages") End If ' 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) 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