Friday, December 22, 2006

Conversion macros\inline functions

sometimes you might need to convert (safely) between different Windows data types.
for example, consider this:

you have a dialog window procedure and you want to handle WM_CTLCOLOREDIT message to change the background color of the edit control.


BOOL CALLBACK DialogProc(
HWND hwndDlg,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
)
{
switch(uMsg)
{
case WM_CTLCOLOREDIT:
return (BOOL)(GetStockObject(DKGRAY_BRUSH));
}
}

I followed the instructions from MSDN for WM_CTLCOLOREDIT:
"If a dialog box procedure handles this message, it should cast the desired return value to a BOOL and return the value directly."

the problem is it works but it gives a warning: Compiler Warning (level 1) C4311.
"This warning detects 64-bit portability issues. For example, if code is compiled on a 64-bit platform, the value of a pointer (64 bits) will be truncated if it is assigned to an int (32 bits). This warning is only issued when /Wp64 is used. "

I am using VisualC++ 2003 compiler so it seems /wp64 is put by default.

Now, MS made some inline conversion functions which must be used when converting data types.
this if found in intsafe.h header.

additional information can be found in a blog of a MS security guy:

http://blogs.msdn.com/michael_howard/archive/2006/02/02/523392.aspx

additionally, look at this info:
"Rules for Using Pointers" in MSDN

There are also some other conversion macros in Basetsd.h header made for win64 programming too:
void * Handle64ToHandle( const void * POINTER_64 h )
void * POINTER_64 HandleToHandle64( const void *h )
long HandleToLong( const void *h )
unsigned long HandleToUlong( const void *h )
void * IntToPtr( const int i )
void * LongToHandle( const long h )
void * LongToPtr( const long l )
void * Ptr64ToPtr( const void * POINTER_64 p )
int PtrToInt( const void *p )
long PtrToLong( const void *p )
void * POINTER_64 PtrToPtr64( const void *p )
short PtrToShort( const void *p )
unsigned int PtrToUint( const void *p )
unsigned long PtrToUlong( const void *p )
unsigned short PtrToUshort( const void *p )
void * UIntToPtr( const unsigned int ui )
void * ULongToPtr( const unsigned long ul )

look in MSDN here for more info.

For the above code, the inline conversion function to use is:

case WM_CTLCOLOREDIT:
return HandleToLong(GetStockObject(DKGRAY_BRUSH));
or:


case WM_CTLCOLOREDIT:
return (BOOL)(INT_PTR)(GetStockObject(DKGRAY_BRUSH));

Thursday, December 21, 2006

Using photoshop to create & edit ICO files

To edit & create icons in Photoshop you need an ICO plugin: http://www.telegraphics.com.au/sw/info/icoformat.html

After installing the plugin file in the Photoshop directory (installing means just copy the ICOFormat.8bi file into the Photoshop plugin directory so it can load it at startup; mine is: C:\Program Files\Adobe\Adobe Photoshop CS\Plug-Ins\File Formats\ICOFormat.8bi)

to create an icon in Photoshop:
1. select your object which you want to appear in your icon with the Magic Wand tool (selection).
you might want to adjust the Tolerance factor.

2. go to Selection menu and choose save selection and name the layer Alpha (it can have any name).

3. go to Save menu and now you should have in the list the ICO format available.

Remeber 2 things:
The alpha channel you created must look like this: you should see the object silhouette in black and the background in white.
The icon cannot have in size more than 255x255 pixels (Windows file format limitation).

At the same location (http://www.telegraphics.com.au) there is a tool which can take different icons files and concatenates them into one ico file but i havent tried it.

Sunday, December 17, 2006

guest book

Although I created this blog just as a place where to keep my programming thoughts so I can review them anytime, since it's a public blog people might come in and want to say something, like Hello.

If you have any general feedback regading this blog, you are encouraged to reply it here.

what's this blog for

Hello,

I wanted a place where I can place programming things I encounter along my humble programming job I have.

It is meant for helping me to save different ideas I should remember but memory is not my strongest point.
In fact I dont like to remember things just beacause it is needed so. I prefer to write them down. Physical memory is valuable.

Thursday, December 14, 2006

CAtlRegExp Match access violation

if the string you want to match (not the regular expression string) contains characters with code >= 128 then CAtlRegExp::Match() method fails. Here is the fix suggested here: http://groups-beta.google.com/group/microsoft.public.vc.atl/msg/b525c84477676c4f Hi, I was running in the same problem with german umlauts. The algorithm seems to be buggy. To word around that, copy the file C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\atlmfc\include\atlrx.h to your project path and make the following changes and include it in your cpp: Line 637 New code:
unsigned char* usz = (unsigned char *) sz;
size_t u = (size_t) *usz;

instead of Old code:
size_t u = (size_t) *sz; and Line 1181 New code:
unsigned char uchStart = chStart;
unsigned char uchEnd = chEnd;
for (int i=uchStart; i<=uchEnd; i++)

instead of Old code:
for (int i=chStart; i<=chEnd; i++) With these changes everthing should work fine. Good luck! Michael