Posts

Showing posts from 2007

smallest EXEcutable

This is an interesting page discussing techniques to achieve the smallest EXE possible. http://www.phreedom.org/solar/code/tinype/ More, it says that if you add an UNC path as a name of an imported DLL in an executable, Windows will download and execute that file ! the server needs to be a WebDAV server.

the one and only, the TB_ADDSTRING message !

If you try to use TB_ADDSTRING with the string from the resource then you might had blown your head trying to figure out what the heck is happening, why does it fail: const int iIndex = ::SendMessage( hToolBar, TB_ADDSTRING, hResource, IDS_TBSEARCH); // string id from module's resource iIndex is returned -1 (indicating an error). Now, I looked at my MSDN from disk and then looked at the online version. Nada. But the old friend Mr. Google saved the day (he always does). I got this page: The problem with TB_ADDSTRING or the AddString method that is simply a wrapper to this message is that the first character in the string resource is used as separator and is replaced with a NULL character. So your string resource must be something similar to: "|Button text||" where the pipe character is replaced with '\0'. This is because the string you pass with TB_ADDSTRING message must have a double null terminator. You can also use the TB_ADDSTRINGS messa...

MAKEINTRESOURCE macro

Many Win32 API functions use a LPCTSTR parameter as resource name or type. For example, a trivial one: HICON LoadIcon(HINSTANCE hInstance, LPCTSTR lpIconName); And the documentation for the lpIconName states that: Pointer to a null-terminated string that contains the name of the icon resource to be loaded. Alternatively, this parameter can contain the resource identifier in the low-order word and zero in the high-order word. Use the MAKEINTRESOURCE macro to create this value. Now, how the LoadIcon code knows to diferentiate between a resource string name and a predefined constant like IDI_ASTERISK which is defined as: #define IDI_ASTERISK MAKEINTRESOURCE(32516) We are used to the wizard generated resource IDs in our applications but a resource can have any null terminated string as name and type too. Look to the FindResource function for example. All resource Win32API functions use the macro IS_INTRESOURCE(id) BOOL IS_INTRESOURCE( WORD wInteger ); #define I...

Navigating to an embedded resource in .NET

This task is very common but since i did not find a clear and simple solution i decided to write it. I have a simple C# application which has a webbrowser control and i want to use the res:// protocol to navigate to an embedded resource, a html file from the assembly, like this: webbrowser.Navigate("res://myApp.exe/Help.htm"); since managed resources are not native ones, adding an html file to the project and select "embedded resource" wont help. if you open the assembly with a resource editor you won;t see the html resource. hence, the res:// protocol wont work. so i wrote this simple console application which accepts as parameters the resource file you want to add. Usage: AddResourceManaged.exe ExePath ResFile ResName [ResType] adds a new resource to an exe file ExePath - path of the exe ResFile - path of the file to be added as a resource ResName - name of the resource ResType - (optional) type of the resource; if omitted, the application will try to use o...

AIAB (Asynchronous Invocation Application Block)

i'll edit this post from time to time to update it untill it's finished. Worker Thread and Service Agent execution. If an exception is thrown from the SA, the exceeption is logged (using the Exception management mechanism). The default exception publisher logs to System Event log. This is because the worker thread has a try-catch inside the thread proc. And this makes that the while loop inside the thread procedure to continue. The Worker thread has a serviceAgentRequest memeber variable which is set \ cleared to null on SA creation execution \ SA execution returns. Worker thred checks for this variable at the loop begin and resubmits the request (it wont look at the requests queue). So, when an exception is thrown from a SA during execution, the SA will be resubmitted indefinetely until the SAMonitor aborts the worker thread. Regarding the SAMonitor: Note that in the case of the exeception thrown from SA, the SAMonitor is called to add the worker thread at each...

Opening CHM Help files from Network or Internet

Due to a security patch Windows has restricted access to a CHM. More info here: http://support.microsoft.com/kb/896358. The attached reg script fixes one of the 4 problems described in the KB. It works for most of the CHM files. Here is the file. Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\HTMLHelp\1.x\ItssRestrictions] "MaxAllowedZone"=dword:00000004

Win32 HANDLEs

Each time when you use a win32 API which uses a HANDLE remember that the handle Must be valid (not closed). It is important to understand the concepts of the handle being signaled and being closed. Signaled state refers to the handle state. It can be signaled or unsignaled. Wait functions for example, like WaitForSingleObject, block current thread until the object gets signaled. A handle is said to be closed when its reference count reaches zero value. When this happens the handle is closed and doing any waiting on it is error prone. A handle is a pointer on an object. Wait functions check the state of the object, hence it is a must that the handler (the pointer) is valid. Otherwise, waiting functions wont work properly (while waiting on an invalid handle, you might wait indefinetly). One example of bogus use is with MFC AfxBeginThread function. Its default behaviour is to auto 'delete' the thread, i.e to close its handle. Its bad to use the returned handle after the thread is ...

Monstrous compilation error on any MFC application

Ok, I've just created a simple MFC DLL, hit the compile button and I got this monstrous error: c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\excpt.h(36): error C2144: syntax error : 'int' should be preceded by ';' c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\excpt.h(36): error C2501: 'x' : missing storage-class or type specifiers And i was like, "WTF !?". When I see these kind of errors i instantly get several thoughts but in a 'chain of responsability' pattern way: 'i need to check the code again' 'ummm, lemme search google for the error' 'shit, i need to reinstall the vstudio' 'ok, i think i need 'a' beer' Ive been lucky enough not to reach the last option in my programming problems ive had so far(though, i dont need programming problems to do that, really). googling a bit i found somebody adviced for a VisualStudio reinstallation. hmmm. too lazy for doing that (an...

Compile libjpeg instructions

http://www.nevrax.org/tikiwiki/tiki-index.php?page=libJPEG

working with DirectShow

The project I am working on has some code already written using DirectShow and I am working with Visual Studio 2005. DirectShow needs DirectX also. So you need both of them installed. It seems that DirectShow was moved from DirectX and was a separate package. Now it seems it is part of the Windows Platform SDK. Latest "DirectX Software Development Kit" location here Latest Platform SDK it is called "Microsoft Windows Server 2003 R2 Platform SDK" and get it from here From the webpage select the type of target platform you want the SDK for. It will download a small web installer from which you can select what SDK components you want to download an install. In this case choose DirectShow. The project uses a DirectShow filter and this one needs Strmbase.lib which is the static library from BaseClasses (located under: (SDK root)\Samples\C++\DirectShow\BaseClasses) The documentation says that there it should be an BaseClasses.dsw file but its not. I did a search on google...

CreateDialog, CreateWindow or any other window\dialog creation function fails and GetLastError is 0

... then check if you have called InitCommonControls() at very start of your application if you use any common controls (list view, tree view ,etc)

ASSERT, ASSERT and ASSERT. Also, always ASSERT !

Putting assertions in your code was always VERY important. I constantly find its importance in my coding. It is very important to always assert your variables\conditions along with the testing it ( 'if' statement ). Also, another common use is in an algorithm. it is important to assert different conditions which might define a state at a moment, a state which I know it should be false\true (when i mean a state i mean a condition composed of multiple variables, variables which might not be in the same scope but I expect some specific values). i am having situations where while coding, i come accross some asserts which make me say "wtf, how did that happen ?" and then i discover that the algo has some issues which were not obvious due to complexity (or my fatigue).

mozilla build from source code

1. get the mozilla source code -------------------------------------------------- Get source code from https://developer.mozilla.org/en/Download_Mozilla_Source_Code Copy the firefox-1.5-source.tar.tar in e:\ Create a directory called e:\mozilla Extract the firefox-1.5.source.tar.tar into mozilla directory 2. install cygwin -------------------------------------------------- run http://www.cygwin.com/setup.exe choose the first type of install (the first form with the 3 radio-buttons) and choose "DOS" style line endings as you read the http://developer.mozilla.org/en/docs/Windows_Build_Prerequisites you see that you need to make sure that the cygwin packages include these: ash -- UNIX-like command line interpreter shell (Base category) coreutils -- GNU core utilities (includes fileutils, sh-utils, and textutils) (Base category) diffutils -- file comparison utility (Base category) findutils (Base category) gawk -- pattern matching language (Base and Interpretors categories) grep ...

threading on GDI objects

When you dealing with multithreading in your code, every data you use in more than one thread it should be a subject to your attention in the way its used in those threads. Similary, using GDI objects in multithreading does not require any special code beside some (natural) common-sense 'not to do' things, which is, dont read and modify un-synchronized in the same time. Also, remember that they have thread affinity (the thread which created the object should be the one who deletes it). The following text is from the Raymond Chen wonderful blog: Window objects: thread which created the window its said to be the window 'owner'. Messages are dispatched to a window procedure only on the thread that owns it, and generally speaking, modifications to a window should be made only from the thread that owns it. Although the window manager permits any thread to access such things as window properties, styles, and other attributes such as the window procedure, and such accesses ar...

IHTMLFormElement::get_elements fails

Don't blow your head, you have this situation: you have a valid form element and you want to access its elements. IHTMLFormElement* pForm = ... one way to do it is by: HRESULT IHTMLFormElement::get_elements(IDispatch **p) and the code looks like the following: IDispatch * pDisp = NULL ; pForm -> get_elements (& pDisp ); IHTMLElementCollection * pFormColl = NULL ; HRESULT hr = pDisp -> QueryInterface ( IID_IHTMLElementsCollection , ( void **)& pFormColl ); but you get pFormColl having NULL value and hr = 0x80004002 which means The QueryInterface method did not recognize the requested interface. The interface is not supported. Google'ing it a bit you get plenty of similar behaviour. The workaround is to use HRESULT IHTMLElement::get_children(IDispatch **p) The hint is given by MSDN description for this method: To access all elements contained in a form, call QueryInterface on IHTMLFormElement and request an IHTMLElement interface. Use the IHTMLElement::chi...