Showing posts with label win32. Show all posts
Showing posts with label win32. Show all posts

Thursday, September 20, 2007

GetOpenFileName() And Some Deep crashes..

I think most of all windows programmers are familiar with GetOpenFileName() API (in MFC CFileDialog class).This api popups a open file dialog letting users to select the file they want.
Okay , I am going into the matter.
Try opening notepad.
1. click open from menu , now you can see open file dialog. Navigate to Desktop
2  move the mouse pointer around some file till tool tip visible.
3. Now cancel the dialog (press cancel or ESC)
4. Try to repeat the same thing (make sure again you go to desktop) , you will be awaited with a crash. Notepad disappears.


Interestingly This happens only with Desktop. I don't know why . So you might ask me how it mess up with the GetOpenFileName. Yes it relates , try calling GetOpenFileName in win32 App (not MFC). You can see the same behavior , your application crashes..


This is Bug in windows XP ( Service pack??? , any oher Version ??? ) .. i don't about any other versions.
If you debug the application you can't see any stack trace . This also happens with the GetSaveFileName api also.
For calling GetOpenFileName you need initialize COM , by calling CoInitialize(0) in the first of the program and call CoUnitinialize in the end of the program. Then it should work. The interesting thing is that in MSDN there is no information about this.


If you call like this as show below , still you can expect the crash.
CoInitialize(0);
GetOpenFileName();
CoUnitialize()

I don't know the exact reason . But my guess is that the GetOpenFileName() / SaveFileName() creats 4 threads .Even you close the dialog these thread exists ,  and these threads might call some com calls , after your CoUnitialize() . That may cause some problems. The most important this is that , There is a chance to crash every application ,  that uses GetOpenOpenFile/GetSaveFile dialog (and not initializing COM) . Seriously... 

Monday, August 20, 2007

Quick way to disable global keys (like ALT + TAB )


Some times it needed to disable the functonality of the global keys like ALT + TAB ,CTRL + ESC . For example when playing some games , pressing ALT + TAB will not cause to switch the tasks. Personaly i don't like this ,but programmers do these ,otherwise they might needed to handle something more..


So if you want to do these , One way is to use Hooks , using SetWindowHookEx api. But there is much simpler way to do this , using hot keys. There is function named RegisterHotKey , this will define a system wide hot key. So the effect is same as Hook.


After defining a hotkey ,when the hotkey is pressed by the user WM_HOTKEY message will be posted to message queue of that window .


So if you want to disable ALT+TAB , install a hotkey using
                                 RegisterHotKey(hwnd,id,MOD_ALT,VK_TAB).


  Done!!!! . Very simple than hooks.you can also handle the WM_HOTKEY message to do your own tasks..


Monday, May 14, 2007

A Easy message filter to a Win32 application vc++.

IF you know c# or vc.Net you can see interfaces like IMessageFilter , which allows to filter a particular message from the application.

So this can be done in win32 with some lines of coding , no need for any interface :)).
I don't want to explain anything more than these code snippets.We know every win32 app has a message loop. it may be look like this

while(GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
   TranslateMessage(&msg);
   DispatchMessage(&msg);
}
}

So we can add filter , which will filter all WM_LBUTTONDOWN message from this application by a mere checking, like
while (GetMessage(&msg, NULL, 0, 0))
{
 if (msg.message != WM_LBUTTONDOWN && !TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
 {
   TranslateMessage(&msg);
   DispatchMessage(&msg);
  }
}

Thats all.. 

Monday, May 7, 2007

Easy way to Disable IME window in an Win32 Application

In this world of Unicode still we often do non Unicode applications. But there is a feature in windows operating system it allows to enter Unicode characters or characters other than basic ascii character set to our application.So What happens is when user press enter key these unicode characters will turn to "????" .Because our application is non Unicode .

So there is a easy way to disappear this IME window when our application comes to front. Microsoft provides api like

1 ImmDisableIme
2 ImmAssociateContext

So if you want to make disappear the ime window for a particular window in you application , you can use ImmAssociateContext.

like ImmAssociateContext(YourHwnd,0) , If don't wan to use the ime feature at all use ImmDisableIme().

The ImmDisableIme will cause to disable IME for all windows in you application .One important thing you should call this function , before any window is created.
Like ImmDisableIme( GetCurrentThreadID()). For an mfc application the best place to call is from CYourApp::InitInstance() , where CYourApp derived from CWinApp.



Thursday, January 4, 2007

Some GDI Informations

Yesterday i just read some pages from the book

Windows Graphics Programming Win32 GDI and DirectDraw®

Feng Yuan
The Feng Yuan is working in mircosoft.  In this space i am going to share some knowledge i got from this book.  So Tell me what a GDI handle is ? like HPEN or HBRUSH or HGDIOBJ ? . Somebody may tell it is a pointer . me too thought like that till yesterday

 Actually Take HGIOBJ as a 32 bit number (it can be pointer or anything).  We all know about the functions GetStockObject(). it is returning a predefined handle. If we call this GetStockObject(WHITE_BRUSH)  from 2 process it will return the same handle(32 bit Number). So what does it means ? it is not residing in user address space. it is in Kernel address space. May be initialized during system start-up.

 The book says that the lower 12 bit of the GDi handle is a index to table. May be that tables stores all other information.The 24th bit position says whether it is a stock object or not. The GDI handle also store information about the type of the handle.


The following function Gives the handle type.
inline unsigned GetObjType(HGDIOBJ hGDIObj)
{
  return (((unsigned) hGDIObj) >> 16) & 0x7F;
}

 The return value can be one of the following.
typedef enum
{
  gdi_objtypeb_dc          = 0x01,
  gdi_objtypeb_region      = 0x04,
  gdi_objtypeb_bitmap      = 0x05,
  gdi_objtypeb_palette     = 0x08,
  gdi_objtypeb_font        = 0x0a,
  gdi_objtypeb_brush       = 0x10,
  gdi_objtypeb_enhmetafile = 0x21,
  gdi_objtypeb_pen         = 0x30,
  gdi_objtypeb_extpen      = 0x50
};

Fortunately there is an undocumented function in GDI32.dll named GdiQueryTable() . It will return the address of the Table. using that address we can access all handles created by our program. 




Facts of a GDI Handle

Yesterday i just read some pages from the book

Windows Graphics Programming Win32 GDI and DirectDraw®

Feng Yuan
The Feng Yuan is working in mircosoft.  In this space i am going to share some knowledge i got from this book. So Tell me what a GDI handle is ? like HPEN or HBRUSH or HGDIOBJ ? . Somebody may tell it is a pointer . me too thought like that till yesterday , Now its changed.

Actually Take HGIOBJ as a 32 bit number (it can be pointer or anything).  We all know about the functions GetStockObject(). it is returning a predefined handle. If we call this GetStockObject(WHITE_BRUSH)  from 2 process it will return the same handle(32 bit Number). So what does it means ? it is not residing in user address space. it is in Kernel address space. May be initialized during system startup.  The book says that the lower 12 bit of the GDi handle is a index to table. May be that tables stores all other information. The 24th bit position says whether it is a stock object or not. The GDI handle also store information about the type of the handle.
The following function Gives the handle type.

inline unsigned GetObjType(HGDIOBJ hGDIObj)
{
  return (((unsigned) hGDIObj) >> 16) & 0x7F;
}


The return value can be one of the following.

typedef enum
{
  gdi_objtypeb_dc          = 0x01,
  gdi_objtypeb_region      = 0x04,
  gdi_objtypeb_bitmap      = 0x05,
  gdi_objtypeb_palette     = 0x08,
  gdi_objtypeb_font        = 0x0a,
  gdi_objtypeb_brush       = 0x10,
  gdi_objtypeb_enhmetafile = 0x21,
  gdi_objtypeb_pen         = 0x30,
  gdi_objtypeb_extpen      = 0x50
};

Fortunatly there is an undocumented function in GDI32.dll named GdiQueryTable() . It will return the address of the Table. using that address we can access all handles created by our program.