Archive for the ‘Programming’ Category.

Containerize An Inno Installed Application

Introduction

In this article, we are going to create a Docker Image from an Inno Installer. The installer will install a .net 4.8 framework console application into the docker image.

Typically, when building docker images you should try to build the image from the source code but in the real world this is not always possible and in this article, we will assume that you do not have access to the source code for the installer or the console application that this being installed.

We will be using an installer called mysetup.exe, which installs an application called HelloWorldConsole.exe to the folder c:\program files (x86)\My Program\.

Containerize Procedure

In this procedure, we are going to create a Docker Image that will execute the installer and configure the container to run the application HelloWorldConsole.exe on startup.

  1. In the same folder as the installer mysetup.exe create a text file called Dockerfile.
  2. Using a text editor add the following code to the Dockerfile.
    FROM mcr.microsoft.com/dotnet/framework/runtime:4.8
    WORKDIR /app
    COPY mysetup.exe
    RUN mysetup.exe /VERYSILENT
    ENTRYPOINT ["/program files (x86)/My Program/HelloWorldConsole.exe"]
  3. Open the command prompt and navigate to the folder containing the Dockerfile and execute the command to create the docker image. The command will create a new docker image called installhelloworldconsole.
    docker image build --tag installhelloworldconsole .
  4. To create a container using the image run the command docker run. This command will create a container using the image created in the previous step and call the container hello2.
    docker run --name hello2 installhelloworldconsole
  5. After the container has booted, the container will execute the program c:\program files (x86)\My Program\HelloWorldConsole.exe
  6. You can confirm the program has been executed by checking the docker logs using the command docker logs and the name of the container.
    docker logs hello2

Summary

In this article, we create a docker container using a pre-existing installer.

Containerize .net Framework 4.8 Console Application

Introduction

In this article, we are going to create a Docker Image for a .net framework console application. The Docker Image will enable us to execute the console application within a Docker Container.

The Console Application

The console application that we will be using is called HelloWorldConsole.exe. When executed from the command line the application displays “Hello World”.

The application is a C# (c sharp) program that has been compiled using .net framework 4.7 and here is the source code for the application which we will be using within this article. Although in this article we will only be using the compiled application.

using System;

namespace HelloWorldConsole
{
    internal class Program
    {
        static void Main()
        {
            Console.WriteLine("Hello World");
        }
    }
}

Containerizing Procedure

In this procedure, we are going to create a Dockerfile. The file will describe how to create the Docker Image for the console application HelloWorldConsole.exe.

  1. In the same directory as the HelloWorldConsole.exe application create a text file called Dockerfile no extension.
  2. Using a text editor add the following code to the Dockerfile
    FROM mcr.microsoft.com/dotnet/framework/runtime:4.8
    WORKDIR /app
    COPY . .
    ENTRYPOINT ["/app/HelloWorldConsole.exe"]
  3. Open a command prompt and change the directory to the folder containing the Dockerfile
  4. Execute the docker command to build the image file. The command will create a new docker image called helloworldconsole. Docker image names are all lowercase. The full stop (period) at the end of the command is very important as this informs the command to look for the Dockerfile in the current working directory.

    docker image build --tag helloworldconsole .

  5. Execute the docker command to run the container using the image built in the previous step. The name argument will give set the container name to hello1 and helloworldconsole is the name of the image that will be used to create the container.

    docker run --name hello1 helloworldconsole

    There will be a short delay while the container boots and then the container will execute the HelloWorldConsole.exe console application.

  6. The output from the console application will be stored in the docker logs. You can review the log messages by using the docker log command with the name of the container.

    docker logs hello1

  7. The container hello1 can be stopped using the cocker stop command with the name of the container. To confirm the container has stopped execute the command docker container ls command.

    docker stop hello1

    docker container ls



Summary

In this article, we created a Docker image called helloworldconsole. Then using the image helloworldconsole we created a docker container called hello1. The container executed the console application HelloWorldConsole.exe. We viewed the output of the console application using docker logs and then stopped the container using the command docker stop.

How To Use Extension Methods in C#

Extension Methods can extend any type with additional methods
Extension Methods requirements:

  • Class must be static
  • The function must be static
  • The function must use ‘this’ in the arguments to denote the type that is being extended.
using System;

namespace ExtensionMethodExample
{
    internal class Program
    {
        static void Main()
        {
            string hello = "Hello";
            Console.WriteLine(hello.Shout());

            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }
    }

    public static class StringExtensions
    {
        public static string Shout(this string message)
        {
            return message.ToUpper();
        }
    }
}

How To Change The Label Font in TKinter

The font attribute of the label widget can be used to change the font style and size.

First, we will create a window with a label using the default font and size.

import tkinter as tk

win = tk.Tk()
win.geometry("750x450")

title_label = tk.Label(win, text="hello")
title_label.pack(pady=10)

win.mainloop()

Below is the window with the label’s default font and style.

Next, we will add the font attribute to the label widget to customize the font style and size. In this example, we are going to change the font style to “Impact” and the font size to 20. The possible font styles are listed on the page https://learn.microsoft.com/en-us/typography/font-list/.

import tkinter as tk

win = tk.Tk()
win.geometry("750x450")

title_label = tk.Label(win, text="hello", font= 'Impact 17')
title_label.pack(pady=10)


win.mainloop()

Below is the output with the label’s customized font style and size.

Displaying An Image With Python Tkinter

The aim of the tutorial is to display an image using Tkinter. The image will change when a button is pressed.

Step 1

Create the Tkinter window.

import tkinter as tk

win = tk.Tk()
win.geometry("750x450")

win.mainloop()

When executed the code should generate a window 750 by 450.

TK window

Step 2

Display the image in the window. The code will load the image into a variable called photo. The code then adds a Label to the window of the type image and populates the label with the photo. In this example, the image is called car-jump.gif and the python code will assume the image file is in the same directory as the python code.

import tkinter as tk

win = tk.Tk()
win.geometry("750x450")

photo = tk.PhotoImage(file='car-jump.gif')
image = tk.Label(win, image=photo)
image.pack()

win.mainloop()

When executed the code will create a window with an image.

Step 4

Add a button to the window. First, we are going to add a button to the window and confirm the button will print a message when clicked. In the next step, we will link the button to the image.

import tkinter as tk


def change_image():
    print("Button has been clicked")


win = tk.Tk()
win.geometry("750x450")

photo = tk.PhotoImage(file='car-jump.gif')
image = tk.Label(win, image=photo)
image.pack()

button = tk.Button(win, text="Click to change image", command=change_image)
button.pack()

win.mainloop()

When executed the window should show the image and a button.

Tk window with image and button

In the console window, you should see the message “Button has been clicked”, every time the button has been clicked.

Step 5

In this step we will add the code to the change_image function that will change the image from car to a flower. In this example, the code assumes the flower.gif file will be in the same folder as the python code.

import tkinter as tk


def change_image():
    global show_car, image
    filename = 'not set'
    if show_car:
        filename = 'car-jump.gif'
        show_car = False
    else:
        filename = 'flower.gif'
        show_car = True
    print("Setting image to " + filename)
    photo2 = tk.PhotoImage(file=filename)
    image.configure(image=photo2)
    image.image = photo2


win = tk.Tk()
win.geometry("750x450")

photo = tk.PhotoImage(file='car-jump.gif')
image = tk.Label(win, image=photo)
image.pack()

button = tk.Button(win, text="Click to change image", command=change_image)
button.pack()

show_car = False
win.mainloop()

When executed the code will show the car image.

When the button is clicked the image will change to a flower.

Free Disk Space Inno

To show a label on the Inno wizard page displaying the amount of require disk space. Add the following code section to the installer script.

[Code]
procedure InitializeWizard;
begin
WizardForm.DiskSpaceLabel.Visible := True; // False to hide
end;

Download installer script FreeDiskSpace.iss

Check DotNet Framework is installed during Inno Setup

To check that DotNet framework is installed during an Inno install. Add the following code section to the install script.

; Check if dot net is insalled
[code]
function FrameworkIsNotInstalled: Boolean;
begin
Result := not RegKeyExists(HKEY_LOCAL_MACHINE, 'Software\Microsoft\.NETFramework\policy\v4.0');
end;

// Install dot net with feedback
[Code]
procedure InstallFramework;
var
StatusText: string;
ResultCode: Integer;
begin
StatusText := WizardForm.StatusLabel.Caption;
WizardForm.StatusLabel.Caption := 'Installing .NET framework...';
WizardForm.ProgressGauge.Style := npbstMarquee;
try
if not Exec(ExpandConstant('{tmp}\dotNetFx40_Full_x86_x64.exe'), '/q /noreboot', '', SW_SHOW, ewWaitUntilTerminated, ResultCode) then
begin
// you can interact with the user that the installation failed
MsgBox('.NET installation failed with code: ' + IntToStr(ResultCode) + '.',
mbError, MB_OK);
end;
finally
WizardForm.StatusLabel.Caption := StatusText;
WizardForm.ProgressGauge.Style := npbstNormal;
end;
end;

In the files section add the dotnet installer file. Note the flags AfterInstall and Check that will call the functions to check if DotNet is installed and instal if required.

Source: "C:\Example\dotNetFx40_Full_x86_x64.exe"; DestDir: {tmp}; Flags: deleteafterinstall; AfterInstall: InstallFramework; Check: FrameworkIsNotInstalled

Download the installer script CheckDotNet.iss

Mamadou Claquette hackeur ou fdp

vrm la wey c pa bien

CLIQUE CONCOMBRE DES MERS

Check if a program exists before installing with Inno

To check if a program exists before installing with Inno add the following code section to the installer script. In this example we will be testing for the file “c:\Example\Test.exe”. If the test.exe file exist a message box showing “Program Already Exists” will be displayed and the installer will terminate.

[Code]
function IsMyProgramInstalled: boolean;
begin
result := FileExists('C:\Example\Test.exe');
end;

function InitializeSetup: boolean;
begin
result := not IsMyProgramInstalled;
if not result then
MsgBox('Program Already Exists', mbError, MB_OK);
end;

Download the installer script IsMyProgramInstalled.iss

Create empty folders using Inno

When installing a program using Inno, to create an empty directory you will need to add a dirs section to the installer script.

[Dirs]
Name: "{app}\Example1"

Download installer script CreateEmptyDirectories.iss