Showing posts with label WindowsAPIs. Show all posts
Showing posts with label WindowsAPIs. Show all posts

Sunday, November 1, 2009

[.NET] How to bring window upfront

The stuff on this post can be easily integrated with a previous post:
How to Check if App is Already Running.

In order to bring a window upfront you need to mess with win32 API calls - no easy way around (if you know any please give me a shout). Here's how to import the calls we'll be using:
//Win32 API calls to raise a given processs main window
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
private static extern bool IsIconic(IntPtr hWnd);

private const int SW_RESTORE = 9;
Here's a static method to bring a Window upfront in a given winApp (can be easily integrated with the previous post linked above):
static class Program
{

// all the rest of the stuff ... Main other methods etc.

public static void BringOnFront()
{
Process myProcess = Process.GetCurrentProcess();

string myAsseblyName = Assembly.GetExecutingAssembly().GetName().Name;

foreach (Process processId in Process.GetProcessesByName(myAsseblyName))
{
if (myProcess.Id != processId.Id)
{
IntPtr hWnd = processId.MainWindowHandle;

if (IsIconic(hWnd))
{
ShowWindowAsync(hWnd, SW_RESTORE);
}

SetForegroundWindow(hWnd);

break;
}
}
}


}
That'd be all - knock yourself out (tested on a number of XP and Vista machines).

Tuesday, March 18, 2008

[C++] How to retrieve Application Path

Hi There,
welcome back to "the Best Resource on the web for Application Path retrieval" (Jack Jones - Collective Development); after .NET Application Path and JAVA Application Path we present a quick reference about how to retrieve Application Path using C++ and raw WinAPIs.

There are two common ways to retrieve App Path in C++ in a windows environment:

1) GetCurrentDirectory - this is probably the most common way to do it but it has a Drawback: the current directory path is not always the directory from where your assembly is being executed; You can alway change your current directory with a call to SetCurrentDirectory Indeed. This function fills a buffer with the current directory path (without filename), and returns the path size (termination character excluded); in case of errors it returns 0, In case the buffer is too short it returns the buffer required size:

#define PATH_LENGTH 1023

char buffer[PATH_LENGTH];
CString AppPathNoFileName;
int rv = GetCurrentDirectory(PATH_LENGTH,buffer);

if((!rv) || (rv > PATH_LENGTH))
{
//something wrong!
}
else
{
AppPathNoFileName = buffer;
}


Ref: GetCurrentDirectory

2) GetModuleFileName - this is safer if you wanna be sure to get the directory from which the current assembly is being executed. With this method you get not only the path but the file name as well, so you might wanna parse the resulting string in order to cut the filename if you don't need it. If the buffer is too short the path gets trucated. The function returns the size of path (number of characters- excluding end of string character) or 0 if an error occurs:


HINSTANCE hInst = AfxGetInstanceHandle();
char _buffer[PATH_LENGTH];
CString AppPath, AppPathNoName;

if(!GetModuleFileName(hInst, _buffer, PATH_LENGTH))
{
//Troubles!
}
else
{
AppPath = _buffer;
AppPathNoName = AppPath.Left(ApplicationPath_.Find("\\[appname].[appext]", 0));
}


Ref: GetModuleFileName

That's all; Stay out of troubles.

Thursday, March 13, 2008

[C++] How to disable Alt+Tab (and other key combinations)

You might need at some stage to disable some key combinations. There are -as always- different ways to do it; the one that I find -arguably- the easiest is showed in the following snippet, which traps the ALT+TAB combo:


#define MY_HOTKEYID 100 //unique in your window
//...
//Lock ALT+TAB - might wanna do it in your form constructor
bool isMyKeyComboTrapped = RegisterHotKey(GetSafeHwnd(), MY_HOTKEYID, MOD_ALT, VK_TAB);
ASSERT(isMyKeyComboTrapped!= FALSE); // just in case
//...
//...
//Unlock ALT+TAB - might wanna do it in your form destructor
isMyKeyComboTrapped = UnregisterHotKey(GetSafeHwnd(), MY_HOTKEYID);
ASSERT(isMyKeyComboTrapped!= TRUE); // just in case


What you're doing here is basically register a key combo without providing any handler for the WM_HOTKEY message fired when the Alt+Tab combination is pressed. You can use this trick to lock other key combos; use this msdn link as reference: RegisterHotKey reference. This method can't be used to trap the infamous Ctrl+Alt+Del combination in order to disable Task Manager; for this you can use the method described in this other post: How to Disable Task Manager.

Have a nice Patrick's Day!

Wednesday, March 12, 2008

[C++] How to disable Windows Task Manager

Here's a kick-ass function snippet you can use to lock/unlock Task Manager.
It works setting the appropriate windows registry key. You might wanna do this when you're looking for a way to disable the Ctrl+Alt+Del combination; trapping this particular combination is not as straightforward as trapping the Alt+Tab one or similar, so you can't use the Register/UnregisterHotKey trick (that's gonna be another post).

You'll usually call this function within class contructor/destructor in order to make sure after your object is destroyed you enable back Task Manager (if that's what you want):


void LockTaskManager(bool Lock)
{
HKEY hkey;
DWORD dwDisposition;
DWORD dwType, dwSize;
DWORD value;

if (Lock)
value = 1;
else
value = 0;
if(RegCreateKeyEx(HKEY_CURRENT_USER,
TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\system"),
0,
NULL,
0,
KEY_SET_VALUE,
NULL,
&hkey,
&dwDisposition)== ERROR_SUCCESS)
{
dwType = REG_DWORD;
dwSize = sizeof(DWORD);
RegSetValueEx(hkey, TEXT("DisableTaskMgr"), 0, dwType, (PBYTE)&value, dwSize);
RegCloseKey(hkey);
}
}
RegCreateKeyEx will open the key if existing otherwise it'll create it. The KEY_SET_VALUE parameter on the RegCreateKeyEx is necessary, otherwise you wouldn't be able to set the DisableTaskMgr DWORD value on the reg. This is tested on Win2000 and WinXP SP2.

Enough registry butchering for today.