Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, ByVal lpOverlapped As Long) As Long
Private Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, ByVal lpOverlapped As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Const INVALID_HANDLE_VALUE = -1
Private Const GENERIC_READ = &H80000000
Private Const GENERIC_WRITE = &H40000000
Private Const OPEN_EXISTING = 3
Private Declare Function SetFilePointer Lib "kernel32" (ByVal hFile As Long, ByVal lDistanceToMove As Long, lpDistanceToMoveHigh As Long, ByVal dwMoveMethod As Long) As Long
Private Const FILE_BEGIN = 0
Private Const IMAGE_SUBSYSTEM_WINDOWS_GUI = 2    
Private Const IMAGE_SUBSYSTEM_WINDOWS_CUI = 3    
Private Const ERROR_CANNOT_WRITE_FILE = 1
Private Const ERROR_BAD_FILE_FORMAT = 2
Private Const ERROR_CANNOT_READ_FILE = 3
Private Const ERROR_CANNOT_OPEN_FILE = 4
Private Function GetErrorDesc(iError As Integer) As String
    Select Case iError
        Case ERROR_CANNOT_WRITE_FILE
            GetErrorDesc = "Cannot write to the file"
        
        Case ERROR_BAD_FILE_FORMAT
            GetErrorDesc = "Bad file format"
        
        Case ERROR_CANNOT_READ_FILE
            GetErrorDesc = "Cannot read from the file"
        
        Case ERROR_CANNOT_OPEN_FILE
            GetErrorDesc = "Cannot open the file"
    End Select
End Function
Private Sub ChangeMode(sFilename As String, iAppMode As Integer)
    Dim hFile           As Long
    Dim iError          As Integer
    Dim buffer(1024)    As Byte
    Dim lBytesReadWrite As Long
    Dim lPELocation     As Long
    
    
    hFile = CreateFile(sFilename, GENERIC_READ Or GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0)
    If hFile <> INVALID_HANDLE_VALUE Then
        
        If ReadFile(hFile, buffer(0), &H40, lBytesReadWrite, 0) <> 0 Then
            If buffer(0) = &H4D And buffer(1) = &H5A Then
                
                CopyMemory lPELocation, buffer(&H3C), 4
                If lPELocation = 0 Then
                    iError = ERROR_BAD_FILE_FORMAT
                Else
                    SetFilePointer hFile, lPELocation, ByVal 0, FILE_BEGIN
                    
                    If ReadFile(hFile, buffer(0), 2, lBytesReadWrite, 0) <> 0 Then
                        
                        If buffer(0) = &H50 And buffer(1) = &H45 Then
                            buffer(0) = iAppMode
                            SetFilePointer hFile, lPELocation + &H5C, ByVal 0, FILE_BEGIN
                            
                            If WriteFile(hFile, buffer(0), 1, lBytesReadWrite, 0) = 0 Then
                                iError = ERROR_CANNOT_WRITE_FILE
                            End If
                            
                        Else
                            iError = ERROR_BAD_FILE_FORMAT
                        End If
                    Else
                        iError = ERROR_CANNOT_READ_FILE
                    End If
                End If
                
            Else
                iError = ERROR_BAD_FILE_FORMAT
            End If
        Else
            iError = ERROR_CANNOT_READ_FILE
        End If
                
        
        CloseHandle hFile
    Else
        iError = ERROR_CANNOT_OPEN_FILE
    End If
    
    If iError = 0 Then
        MsgBox "The application mode was successfully changed", vbOKOnly Or vbInformation, ""
    Else
        MsgBox "Error: " & GetErrorDesc(iError), vbOKOnly Or vbExclamation, "Error"
    End If
End Sub
Private Sub cmdBrowse_Click()
    On Error GoTo canceled
    
    OpenDialog.Flags = cdlOFNExplorer Or cdlOFNFileMustExist Or cdlOFNHideReadOnly
    OpenDialog.ShowOpen
    txtFilename.Text = OpenDialog.FileName
    Exit Sub
canceled:
End Sub
Private Sub cmdChange_Click()
    ChangeMode txtFilename.Text, IIf(ModeOption(1).Value, IMAGE_SUBSYSTEM_WINDOWS_GUI, IMAGE_SUBSYSTEM_WINDOWS_CUI)
End Sub
 |