Friday, August 26, 2016

ITaskbarList3 interface using plain Windows API

The ITaskbarList3 interface can be used by application (from Windows 7 onwards) to perform following actions:
  • Showing the progress of an operation in the taskbar
  • Showing an overlay icon over the taskbar item
  • Add a tooltip to the taskbar thumbnai
  • Adding a toolbar to the taskbar of the application
Even the interface functions are descibed at MSDN (see link below), getting it to work might not be that easy. Therefore here are the steps to make use of the ITaskbarList3 from a C/C++ application using plain Windows API:
  1. Include Shobjidl.h
  2. Initialize the COM library by calling CoInitialize() or CoInitializeEx()
  3. After your main window is created (e.g. after CreateWindowEx() call), define the message "TaskbarButtonCreated" using RegisterWindowMessage():
    UINT taskbarButtonCreatedMessageId = RegisterWindowMessage ( "TaskbarButtonCreated" );
    Store the returned message ID for later use.
  4. To ensure that your main window receives this message, enhance the message reception filter privileges:
    ChangeWindowMessageFilterEx(mainWindow, taskbarButtonCreatedMessageId, MSGFLT_ALLOW, NULL);
  5. Afterwards the message with ID returned by RegisterWindowMessage() is sent to the window procedure of your main window. If it's received, create an instance of the ITaskbarList3 interface using CoCreateInstance():
    ITaskbarList3* pTaskbar = NULL;
    LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
      if (uMsg == taskbarButtonCreatedMessageId)
      {
        HRESULT hr = CoCreateInstance(CLSID_TaskbarList, NULL, CLSCTX_INPROC_SERVER, IID_ITaskbarList3, reinterpret_cast(&pTaskbar));
      }
    ...
    }
  6. From now on, you can call the ITaskbarList3 functions using variable pTaskbar, e.g. pTaskbar->SetProgressState(hwnd, TBPF_ERROR);
  7. Before quitting the application, remember to release the interface and close the COM library:
    pTaskbar->Release();
    CoUninitialize();
I hope those steps help you in order to use this interface. For an working example with source code, see http://sunshine2k.de/c.html#windowsprogressbar.

ITaskbarList3 interface at MSDN: https://msdn.microsoft.com/en-us/library/windows/desktop/dd391692(v=vs.85).aspx

Regards, Sunshine2k

Tuesday, August 12, 2014

Where does 116444736000000000 come from?

While dealing with time conversions functions of various languages / technologies, e.g. Windows API or Java, you might come across the magic constant 116444736000000000. You can also find it in several code snippets on the web - however it never nearly explained where it comes from... This I wanna change, here and now!

A time value is simply a counter of ticks since an defined start of time, called epoch. Not only the "epoch date" differs between various systems, but also the counter resolution, that is how often it is updated.

Windows uses in most functions the FILETIME structure, which represents the actual time as the number of 100-nanosecond intervals since January 1, 1601 (UTC).

Other platforms like Java/Android or some Unix functions represent a time value as the number of seconds elapsed since 00:00:00 January 1, 1970.

To convert between both worlds, you need an offset factor and that is... 116444736000000000. This value used as a FILETIME value, meaning 100ns intervals since 01-01-1601, represents exactly the data 01-01-1970!

Proof?
We know that 1ms = 1000000 ns = 10000 * 100 ns.
So 116444736000000000*1/100 ns = 11644473600000 ms = 11644473600 s.
Between 01-01-1601 and 01-01-1970, there are exactly 134774 days.
134774 * 24 (hours per day) * 3600 (seconds per hour) = 11644473600 s.

Tuesday, April 01, 2014

How many walls/borders has a nxn grid?

While implementing an algorithm for a hobby project, I came across the question how many walls (or borders) has a nxn grid? Here I present the solutions of my line of thought: the first one is quite complicated, but was also the first which came into my mind. After convinced of the correctness of this one, a much easier idea occured... :-)

Lets start: A 1x1 grid has 4 walls which is easy to see:
 _
|_| = 1 cells --> 4 walls

Lets have a look at a 2x2 grid:
 _ _
|_|_|
|_|_| = 2 cells --> 12 walls (feel free to count it :-)

So my idea to get the equation of a nxn grid is based on following image / procedure:

   1 2 ... n
   _ _     _
1 |_|_|...|_|
2 |_|_|_|_|_|   
   . _|   .
   _ _|   .
n |_|_|... _|
  
1. Start with the (orange) cell in the topleft corner which has 4 walls: 4
2. Lets move in the top row further to the right (cyan). For each of the remaining n-1 cells in this row, there are 3 walls per cell left: (n-1) * 3
3. The same applies if we go down in the first column step by step until the red cell. So for each cell there are also 3 walls left: (n-1) * 3
4.  Then there is (n-1) x (n-1) grid left. However if we go there row by row and column by column, it turns out that for each cell only two more walls come along (the wall at the bottom and at the right), because the left and top wall were then already taken into account. So (n-1) * (n-1) * 2 walls are left.

So in total we have come up with following equation for an nxn grid:
x = 4 + (n-1)*3 + (n-1)*3 + (n-1)*(n-1)*2
   = 4 + 3n - 3 + 3n - 3 + (n^2 -2n +1)*2
   = 4 + 6n - 6 + 2n^2 - 4n + 2
   = 2n^2 + 2n
   = 2n (n+1)

Manually checking of the cases n=3 and n=4 turned out to be correct, so I guess it's ok.

However, afterwards a more simple solution came into my mind by just looking at the vertical walls only and at the horizontal walls separately:

   1 2 ... n
   _ _     _
1 |_|_|   |_|
2 |_|_|   |_|
  .
   _ _     _
n |_|_|...|_| 

1. If just looking at the vertical walls (red), we see that there are n rows, each having n+1 walls: n * (n+1) walls in total.
2. The same applies for the horizontal walls (cyan): n columns * (n+1) walls.

This is in total:
y = n * (n+1) + n * (n+1)
  = n^2 + n * n^2 + n
  = 2n^2 + 2n
  = 2n (n+1)
  = x

That's it :-)

Monday, February 10, 2014

My Book Word Edition + Twonky Server: How to access the root folder of the NAS

I own a Western Digital 'My Book World Edition' (that's a simple NAS) for some years now, mostly as backup and central data access point.
After having purchased a smart TV, finally I was motivated to activate the TwonkyMedia server to stream media via DNLA.
Everthing went fine, however only the media files in the \public folder of the NAS were indexed by Twonky and I did not manage to get access to the whole NAS via the Twonky browser. Pretty bad as I was used to my private well-known folder structure and not willing to change it.
I did not manage to fix the problem for some time, and for some reason I did not find anything useful on the net, until I came across this topic on WD community forum:

http://community.wd.com/t5/Other-Network-Drives/USB-Media-Share-TwonkyMedia-Server/td-p/28117

Here the main information with the single command to adjust the start location, thanks to user garyg:
This is because the shares start at /DataVolume/Public.  If you change the default start location to /shares, or maybe it's /Shares, you will be able to access anything which has been defined as a share on the MyBook.

The command to fix the USB sharing problem is:
         http://<nas>:9000/rpc/set_option?contentbase=/shares

where <nas> is the name or IP address of the My Book.  This command is entered into your web browser's address bar.  The browser will respond with a page that says "/shares".

Thursday, November 15, 2012

My alternative to HTML frames

Maybe some of you have noticed that I have updated my homepage. Well, the content remained more or less the same but the layout was finetuned.
Furthermore, the previous pages were created using a WYSIWYG tool which resulted in pretty ugly, overbloated HTML. Of course it was also hard to maintain and lacked of HTML validity.

The new pages were manually written (hm okay.. copy-pasted). The biggest difference is the fact that they are not frame-based anymore but solely CSS-based (I avoided to use tables for solely layout purposes as much as I could).
But when searching the net for alternatives to frames, my plan turned out to be more difficult than expected. Using I-Frames was no choice. PHP is not supported by my webspace and I was not willing to pay more just for some 'include' directives.
On the other hand, each change of the main layout (header at the top, meu at the left border) would have meant to manually adapt each html file... this is unacceptable.

So what to do?
Actually I came out with my own solution:
1. I started by coding a main layout template file which contains the general layout without any content:



2. This template was then used as base for every html page of my site. Well, so far no advantage. But in each file I put some markers to identify which parts of the html source belongs to the template and which parts contain the actual new content. These markers are just pre-defined HTML comments like:


here comes may cool individual HTML content



3. Then I programmed a fancy tool which compares all files belonging to my site to the latest template. The markers help to identify the regions which belong to the template file and which are individual content. When the template changes, my tool can update the page files in a way that only the template regions are updated but the actual content is not touched. Here a screenshot of my tool:


A single button click will then update all sites :-)

What do you think of this solution?
Well, I must admit it took some time to code it and get it correctly working, but it was fun to write it and it works like a charm.
.. I love my own-invented solution!

Saturday, December 03, 2011

Reason for homogeneous (4D) coordinates in computer graphics

Homogeneous coordinates are used computer graphics - you can read this statement in every 3D computer graphics related book or article. If you ever asked yourself why this is the case, then you are at the right place...
The reason for this is to handle rotation, scaling and translation in a common way. While you can handle rotation and scaling using 3x3 matrices and vectors with 3 components, translation cannot be processed - unfortunately this is normally not further explained although it is quite easy to see:
Suppose we have a point p with coordinate (x, y, z) and we want to translate it by a distance defined by the translation vector t=(tx, ty, tz).
Of course, what you need to do to get new position p' of point p after translation is to add t to p:
p' = p + t, which means
p'.x = p.x + tx
p'.y = p.y + ty
p'.z = p.z + tz
If you try to create a 3x3 matrix M to perform this operation by p' = M * p, you get into trouble. What we require is:

x'   x + tx   |a b c|   |x|
y' = y + ty = |d e f| * |y|
z'   z + tz   |g h i|   |z|

x + tx = ax + by + cz (1)
y + ty = dx + ey + fz
z + tz = gx + hy + iz

Looking at equation (1) we directly see that a = 1 and we are left with tx = by + cz. But the translation in x-direction must be indepent of y and z coordinates, so b and c must be zero.

x + tx = 1*x + 0*y + c*z = x   => tx = 0
So only the primitive translation by zero is possible. The same applies for the equations for y and z.

Using homogeneous coordinates, that is to add a forth component w = 1 to each point, we can derive a 4x4 matrix for translation without any problems.


x + tx   |a b c d|   |x|
y + ty = |e f g h| * |y|
z + tz   |i j k l|   |z|
1        |m n o p|   |1|

we get e.g. for x-coordinate:

x + tx = ax + by + cz + d, 
so a = 1 and d = tx which leads us to the final translation matrix:

    |1 0 0 tx|
M = |0 1 0 ty|
    |0 0 1 tz|
    |0 0 0 1 |

That's it :-)




Sunday, January 09, 2011

Browsing old reversing sites - the good old times

I just browsed through an old backup folder called "Reversing favourites" ... man this contains really OLD urls of reversing sites: groups, tutorials and so on!
Of course, I clicked several of them, full of hope, just to get disappointed that ca. 90% of the urls do not exist anymore. But some of the linked site still exist, but of course not updated for several years - just lying dead on some server.
I engrossed the thoughts what really cool sites existed at that time, now approximately 10 years ago. Here are some that are still online but out-of-date - maybe some of you want also fall in some nostalgy.
So here are my three top sites that are still online available:

  • Immortal Descendants - this is one of my all-time favourite reversing sites. The tutorials and the code snippets were outstanding at that time, at least for me. I linked a working archived version of the site, the original URL was http://www.immortaldescendants.org.
  • BIW Reversing - not that old but still no update in the last two years
  • Crackstore - one of the first reversing sites I have ever visited
Let's see, next time I post the URLS of the top site of nowadays... stay tuned!