Application Programming Hints


This contains hints on application programming, especially pertaining to dealing with drivers. I'll continue to add them as time goes on. If you know something that may help people, please email me.

Last updated Nov 21, 2000


Finding the executable name from a process ID

If you have a process ID, there are methods to find the module name. But, it depends on what OS you are running. If you are running Windows 95, 98 or NT 5, you can use the TOOLHELP32 calls. If you are using NT 4.0, you can use the PSAPI calls. There is an example of the PSAPI calls in the November 1996 edition of Windows Systems Journal. It's in Matt Pietrek's Under the Hood section.

For an example of TOOLHELP32, go to the September 1995 edition of Microsoft Systems Journal. It is in the th32demo example by Matt Pietrek.


Discovering what operating system a program is running from

Many times it is important to know if a program is running under NT or Windows 95/98. There is a routine in kernel32 dll which only exists under 95/98. This is an easy way to find out that information.

typedef HANDLE (WINAPI * OVH)( HANDLE ring3 );

 BOOL IsWin9xRunning(void)
 {
 OVH lpfn = (OVH)GetProcAddress( GetModuleHandle("kernel32"),"OpenVxDHandle");
 
 if (lpfn == NULL) 
    {
    //NT is running
    return FALSE;
    }

 //95 or 98 is running
 return TRUE;
 }

How to send keyboard presses to an application

There are two methods to doing this. The first is sending a keyboard event through a windows hook (WH_JOURNALPLAYBACK). This method works well in NT and Windows 95. The example that everyone seems to use is Sendkeys by Robert Mashlan. (If that link didn't work, try this one.) There are a few problems trying to use this code with Windows 95/98 or NT 4.0 or later. First, LibMain and WEP should be changed to DllMain. Next, all code that will be seen by multiple processes need to be shared. (See my Dll hint page for examples.) Last, but not least, change the hook procedure. HC_GETNEXT may be called many times, expecting the same value, until HC_SKIP is called. Sometimes the event is queued but not sent, so the same character needs to be requeued. HC_SKIP should increment any array count and check for the end conditions. If all these changes are made, the example should work as expected.

But, there have been indications that this method does not work with DirectInput. So, there is another method that is usable for Windows 9x. Under the Windows 95 DDK, under keyb\samples\Vkxd\ there is an example of an easy keyboard emulation VxD.


Go to driver annotation page