Patrick’s development blog

Mount ntfs drive on Linux

Posted in Linux/GNU by Patrick on April 28, 2009

1. Install ntfs-3g.
2. Create a folder in /media or /mnt (mkdir /media/windows)
3. Use fdisk -l to get the name of the NTFS drive (example: dev/sda1)
4. Add the drive into /etc/fstab so it mounts automatically.

/etc/fstab
/dev/sda1 /media/windows ntfs-3g defaults 0 0

Advertisements

Recover lost partitions with Testdisk

Posted in Linux/GNU by Patrick on July 21, 2008

By mistake, I deleted some of my partitions and totally messed up my partition table a couple of days ago, removing the possibility of booting up my computer at all.  Luckily, I found a open source program called Testdisk.

Testdisk is a free data recovery software that can recover lost partitions and make partitions bootable again. Here’s how I recovered my Windows XP partition using a Ubuntu live-CD.

First, I enabled all uncommented repositories in /etc/apt/sources.list.

sudo su
nano /etc/apt/sources.list

Then I updated the package repository list and installed Testdisk:

apt-get update
apt-get install testdisk

When it’s installed, you can start it by typing its name in the terminal. You’ll be asked to create a log file (optional). After that, choose analyze and Intel/PC partition. Your partition table will be shown, wait until it’s analyzed. After the analyze is done, you can also choose to make a deep scan which is recommended. If you decide to recover a partition, just click the right arrow button so the current selection turns green and continue by pressing Enter. You’ll be asked to write this partition on the partition table. Do this.

That’s all, that worked for me at least. I only have some weird problems with my boot manager right now, but the partition is fully bootable.

Useful free software to optimize and secure your system

Posted in Security by Patrick on June 28, 2008

Here’s a list of useful programs in different categories. Most of these programs are for Windows, but some of them are also open source and works for Linux. I only list programs that are free. I might update this post over time.

———————————————

Mozilla Firefox
The web-browser Firefox, has optional add-ons that you can install which makes it possible to extends the browsers functionality. I’ve picked out three add-ons that I find very useful. They speed up the browser and adds more security.

NoScript
It blocks all Javascript, Java and other malicious code from sites you don’t trust. Ever since I installed this, i’ve never had any spywares on my XP computer at all. A most have for people who wants a secure browser. My favorite add-on.

AdBlock Plus
Get rid of all those annoying banners and ads on websites.

Flash Block
Blocks all flash animations on all websites. If you want to play a certain flash animation though, just click on it, that’s what I like most about this add-on.

———————————————

Security
Zonealarm Firewall – A free version of the popular firewall Zonealarm. This software also has program control and port stealthing for example.

AVG Anti-Virus Free Edition – Free anti-virus program, that is easy to use, provides high level of detection capability and doesn’t use so much system resources.

Ad-Aware 2008 – Scan for spywares, data-mining, advertising, and tracking components in general.

Spybot Search & Destroy – Spyware cleaner that also has real time protection.

TrueCrypt – Open Source encryption software. Can encrypt partitions, make virtual drives and encrypt harddrives completely.

———————————————

Optimize and cleaning software
JkDefrag – Free disk defragmenter program. Optimizes your harddrive.

CCleaner – System optimization tool which cleans registry and removes temporary files. It’s very fast.

How to make a simple window in SDL

Posted in Articles, SDL / OpenGL by Patrick on June 25, 2008

Creating windows in SDL is dead easy and doesn’t involve so much code as the Win32 API does for example. SDL is a popular library for game development and it’s cross platform too, so it works on Linux as well.

Install SDL for Windows
1. Download the latest version of SDL from their site
2. Choose the file SDL-devel-1.2.13-VC8.zip
3. Move all include files from the .zip file you downloaded, into the corresponding include folder in your IDE/compilator. Do the same with the files from the /lib folder.
4. In order to be able to open SDL apps without distributing the .dll files with each application. Move the SDL.dll file into the windows/ folder. When you test your application on another computer, you have to distribute the dll files however.
5. Add the SDL lib files into your linker options. In Visual C++ Express this is done by going into Project -> Project Properties -> Linker -> Input. Type the following into the Additional Dependencies field: SDL.lib SDLmain.lib

Install SDL for GNU/Linux
Use your package manager and install the development libraries for SDL. For Debian based systems, i’ve posted an article about how to install SDL here.

Install SDL for Mac OS X
There are development libraries on SDL’s homepage for Mac OS X as well. There are so many different IDE’s for Mac as well, so i’ll not go into that in this article. There are many great tutorials about this already.

Create a window in SDL

#include “SDL.h”

SDL_Surface *screen;

SDL_Event event;

int main(int argc, char *argv[]) {
SDL_Init(SDL_INIT_VIDEO);

screen = SDL_SetVideoMode(640, 480, 16, SDL_SWSURFACE);

SDL_WM_SetCaption(“Simple Window”, “Simple Window”);

bool done=false;

while(!done) {
while(SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
done=true;
}
}

// fill the screen with black color
SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format, 0, 0, 0));

// update the screen buffer
SDL_Flip(screen);
}

SDL_Quit();

return 0;
}

First I include SDL and create a surface called screen. This surface is going to represent the window, it’s called a display surface. A surface in SDL is a rectangular area which contains pixels. Surfaces are also used to represent images, text and pixels in general.

The main function in a SDL program most have the arguments int argc, char *argv[] or int argc, char **argv (technically the same) in order to work.

The initialization of the window is done by the following lines:

SDL_Init(SDL_INIT_VIDEO);
screen = SDL_SetVideoMode(640, 480, 16, SDL_SWSURFACE);

SDL_Init initializes SDL and should be called first. The second line sets the video mode to a specified width and height (640×480). The third parameter of SDL_SetVideoMode represents bits per pixels and the fourth parameter is a flag used to specify some settings for the video mode and how it is going to be created. Here’s a list of all the flags that can be used as the fourth parameter. It’s also possible to combine flags with each other.

Flags that can be used in SDL_SetVideoMode(..)
SDL_SWSURFACE – creates the video surface in the system memory
SDL_HWSURFACE – creates the video surface in the video memory
SDL_ASYNCBLIT – enable asynchronous display mode, may speed up on some cpu’s
SDL_ANYFORMAT – if requested bpp value cannot be used, SDL generates an appropriate video display
SDL_HWPALETTE – this flag gives SDL some kind of palette access
SDL_DOUBLEBUF – enable double buffering, most be used with the HWSURFACE flag
SDL_FULLSCREEN – fullscreen mode will be used if possible
SDL_OPENGL – creates a opengl rendering context
SDL_OPENGL – same as above but it uses normal blitting operations (blitting = showing surfaces)
SDL_RESIZABLE – makes the window resizable
SDL_NOFRAME – creates a window without a frame or titlebar if possible

Read more about SDL_SetVideoMode at the documentation.

————————————————

Set window title
The next line after SDL_SetVideoMode is the setcaption line. This is straightforward as it is. It sets the window title. This is optional, but simple enough so it would be a waste not to use it.

Main loop
The first while loop contains an expression which uses a boolean variable (done). In this program, this variable is set to true when we want to close the program. This while loop is often called the main loop. It’s in the main loop where all the processing is done, that is, drawing, moving things, updating the screen, etc. You can say this is where all the program code is supposed to be, except the initialization code.

Event loop
At the start of the game loop, a new while loop is located. This is the event loop, where all events are processed in the program. Events are messages that is sent to the program whenever the user do something, for example moves the mouse, clicks a button or closes the window. It’s up to us which events we want to handle. In this example, the only event that’s being checked is the SDL_QUIT event. This event is sent to the program when the user clicks on the close button in the top right corner of the application. If we left this event out, we would have to kill the application in order to close it, the close button wouldn’t work.

Fill the background color and update the screen
After the event loop, there are two lines. The first one fills the display surface (screen) with a black color. The second line updates the screen buffer, we have to call this at the end of the main loop, to make sure that the display surface is updated. Well, the SDL_FillRect function call isn’t really necessary in this program since we doesn’t draw anything, not the SDL_Flip() call either. The background color of the application is by default black. However, don’t forget to fill the background or add a background image, if you draw something on the screen though.

Win32 API – Creating a window

Posted in Uncategorized by Patrick on March 29, 2008

Thing aren’t exactly going fast forward with this project or learning about the Win32 API, due to my lack of motivation of reading stuff. I’ve managed to take my time to finish the application window for today at least. This is the complete code.

Create an application window

#include <windows.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("Simple Window");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;

wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;

wndclass.lpszClassName = szAppName;

if (!RegisterClass(&wndclass)) {
    MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
    return 0;
}

hwnd = CreateWindow(szAppName,
TEXT("Simple Application Window"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);

ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);

while (GetMessage(&msg, NULL, 0, 0)) {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}

return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;

switch (message) {
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);

GetClientRect(hwnd, &rect);

DrawText(hdc, TEXT("Hello world!"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);

EndPaint(hwnd, &ps);

return 0;

case WM_DESTROY:
PostQuitMessage(0);
return 0;
}

return DefWindowProc(hwnd, message, wParam, lParam);

}

It’s a lot of code to explain, so i’ll try to cover the most relevant parts of the code in this post.

The window class
All kinds of windows and “controls” like buttons and check boxes are based on a certain window class. In my program above, this class is instantiated to use for the window. The window procedure that processes messages to the window is identified by the window class.

In my program, you can see that WNDCLASS wndclass; is definied in the WinMain function. This class/structure has to be registered using a call to RegisterClass after all fields of the structure are initialized.

Window class definition

WNDCLASS wndclass; wndclass.style = CS_HREDRAW | CS_VREDRAW; wndclass.lpfnWndProc = WndProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = hInstance; wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH); wndclass.lpszMenuName = NULL; wndclass.lpszClassName = szAppName;

wndclass.style definies how the window should be created. The flags I specified in this example makes sure the program repaints every time the window is resized either horizontal or vertical. (CS_HREDRAW | CS_VREDRAW)

wndclass.lpfnWndProc This field sets the window procedure for this class (WndProc). The window procedure is the functon that handles all the messages. This window procedure will now process messages to all windows based on this class.

wndclass.cbClsExtra = 0, wndclass.cbWndExtra = 0
These fields are used internally to maintain some space, I have no idea what. All I know is that this program doesn’t use this extra space.

wndclass.hInstance = hInstance this line sets the instance of the program. It’s the same as the parameter.

wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION)
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW)
These lines sets the icon and cursor type. Since I call these functions with the first parameter as NULL, a predetermined icon will be choosed.

wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
The background of the application will become white.

wndclass.lpszMenuName = NULL
I don’t use a menu in this program so I set this value to NULL. I’ll most likely become familiar with this later though, let’s save it for that time.

wndclass.lpszClassName
Last but not least, the application name. In this case, the name is of the type TCHAR.

This instance has to be registered as well.

Register the window class

if (!RegisterClass(&wndclass)) { MessageBox(NULL, TEXT("Show some error message"), szAppName, MB_ICONERROR); return 0; }

After we’ve registered the window class, the actual window creation code comes into play. HWND hwnd definied at the top of the “main” function is a handle to the window. The created window will be saved into this variable.

Create the actual window

hwnd = CreateWindow(szAppName, TEXT("Simple Application Window"), WS_OVERLAPPEDWINDOW, 200, 250, 640, 480, NULL, NULL, hInstance, NULL);

The above example will simply create a window with a size of 640×480 pixels at the start position (200,250).

Right now, the window has only been created internally in Windows. In order to make the window appear on the video display, it’s necessary to make a call to ShowWindow and UpdateWindow.

Display window

ShowWindow(hwnd, iCmdShow); UpdateWindow(hwnd);

Like usual, it’s nothing really that’s necessary to memorize. It’s good to know though, that hwnd is the window handler we just created using CreateWindow and iCmdShow is used to keep track of the current state of the window (maximized, minimized, etc). ShowWindow shows the window on the screen, it sends the WM_PAINT message to the window procedure.

The last part of the application that makes it complete, is the event handling. Before going on to the window procedure, a while loop that retrieves all the messages from the message queue is necessary.

Retrieve all messages

while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); }

Where msg is a part of the MSG structure and the NULL parameters indicate that we want to retrieve ALL messages for this program. The DispatchMessage() function call sends the message to the window procedure. It’s the windows procedure that takes care of the actual event checking.

The window procedure

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRUCT ps; RECT rect; switch (message) { case WM_PAINT: hdc = BeginPaint(hwnd, &ps); GetClientRect(hwnd, &rect); DrawText(hdc, TEXT("Hello world!"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER); EndPaint(hwnd, &ps); return 0; case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hwnd, message, wParam, lParam); }

This is where we take care of the input and what actions that we take depending on which message we get. There isn’t much to explain about this code without going into too much background. The last return statement that returns the DefWindowProc(…) call, is the default processing of all messages that the window procedure doesn’t process. It’s important to remember.

The WM_PAINT code block is executed whenever the window is updated. In other words, it’s the code that displays the text on the screen. I won’t go into detail how it draws the text right now, because I honestly have no idea right now and I want this post to end.

Tagged with: , , , , , ,

Preparing the Win32 API

Posted in Uncategorized by Patrick on March 25, 2008

I’ve started to learn a little Windows coding using its API today. I’m preparing it for my chat application i’m going to make in C++. Here’s how a typical hello world program looks like when coding in win32. It displays a message box on the screen which says “Hello world”.

Hello world in Win32

#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {
MessageBox(NULL, TEXT(“Hello world!”), TEXT(“Message”), 0);
return 0;
}

WinMain is the same as the main() function in C and C++ (the entry point of the application).

The hInstance parameter is a handler to the current window. It seems that the second parameter hPrevInstance isn’t used anymore. It was used to keep track of other instances of the same application. Last time it was used was in 16-bit Windows. The parameter is always NULL.

The third parameter szCmdLine takes care of the commands the application is started with.

The last parameter iCmdShow indicates how the program should be displayed, for example if it’s going to be maximized, minimized or hidden.