Home About Me Résumé My Programs Anime
Recommendations Alternative Computing Programming Links

Programming

Windows Tricks


Using DirectShow with MinGW

So you tried GCC 4.0, found out it beats the crap out of Microsoft's C++ Compiler, costs a lot less than Intel C++ Compiler, and you want to use it in Windows with DirectShow. Here's how:

First, you will need to install MinGW and MSYS (I tested MinGW 3.2.0-rc-3 and MSYS 1.0.10 on Windows 2000 SP4 and Windows XP SP1), and the DirectX 9.0 SDK. I used the DirectX 9.0b SDK Update Summer 2003, since it is the last version that comes with project files for the Base Classes in Visual C++ 6.0 format (DSW and DSP files).

Here are the necessary modifications:

Header Files

In dxtrans.h:

In strmif.h:

Base Classes

In ctlutil.h:

In refclock.h:

In streams.h:

In wxutil.h:

In ddmm.cpp:

In mtype.cpp:

In outputq.cpp:

In winutil.cpp:

In wxdebug.cpp

In wxutil.cpp:

After which you can compile the Base Classes as follows from cmd.exe (after adding MSYS's and MinGW's bin directory to the PATH and assuming MingGW is installed in C:\MinGW):

You can now compile and link DirectShow programs normally with these modified .h files and the .lib provided with the DirectX SDK! Note however that you will get a lot of warnings when compiling DirectX header files. To avoid seeing them, you can make use of the "-isystem" argument of GCC, or better yet, make use of the precompiled header files feature in GCC.

It should be possible to get the rest of the DirectX package (Direct3D, DirectSound, DirectInput, etc) working this way by fixing code to make GCC happy. However, be aware that the linker of MinGW might have trouble linking with Direct3D's static libraries (d3dx8.lib and d3dx9.lib).

OS/2 Tricks


Autodetecting installed CD-ROM drive letters

There is a way to autodetect the CD-ROM drive letter through undocumented ioctls, eventhough I heard they may have come from the DDK.
HFILE CDDevice;
ULONG action;
ULONG len;
struct
{
    USHORT CountCD;
    USHORT FirstCD;
} CDInfo;

len = sizeof(CDInfo);
if(!DosOpen("\\DEV\\CD-ROM2$", &CDDevice, &action, 0,
            FILE_NORMAL, OPEN_ACTION_OPEN_IF_EXISTS,
            OPEN_SHARE_DENYNONE | OPEN_ACCESS_READONLY, NULL))
{
   if(!DosDevIOCtl(CDDevice, 0x82, 0x60, NULL, 0, NULL, &CDInfo, len, &len))
   {
      for(i = 0; i < CDInfo.CountCD; i++)
      {
         char driveLetter[3] = { (char) ('A' + CDInfo.FirstCD + i), ':', 0};
         /* got one drive letter in driveLetter, use it */
      }
   }
   DosClose(CDDevice);
}

Making a program find its own directory

This's one is a bit tricky, but it's pretty obvious once you know how.
ULONG PathLength;
char *Path;

/* query max path length */

if (!DosQuerySysInfo(QSV_MAX_PATH_LENGTH, QSV_MAX_PATH_LENGTH, &PathLength, sizeof(ULONG)))
   Path = (char *) malloc(2*PathLength*sizeof(char)+1);

/* multiplied by 2 to include the filename length too */

if(Path)
{
   PPIB ppib;
   PPIT ppit;

   DosGetInfoBlocks(&ppit, &ppib);
   DosQueryModuleName(ppib->pib_hmte, 2*PathLength+1, Path);

   *(strrchr(Path,'\\')) = 0;

   Path = (char *)realloc(Path, (strlen(Path)+1)*sizeof(char));

   /* got the path in Path, use it */
}

Notebook dialogs

So, what would you like to do with Notebook dialogs?

WINHSWITCHFROMHAPP()

This function is hidden in PMSHAPI.DLL, and you can import it in any of your programs (in the DEF file using "IMPORTS PMSHAPI.WINHSWITCHFROMHAPP" for example). The C syntax is:
HSWITCH APIENTRY16 WINHSWITCHFROMHAPP(HAPP happ);
Doesn't seem useful? Guess what is the "open handle" of the VIEWITEM and VIEWFILE structure. You got it... You can then do whatever you are used to do with an HSWITCH.

And you can do some pretty amazing things with this and WinStartApp(). Makes me wonder what WinStartApp() really does, as DosStartSession() doesn't do half of what you can find with those.

Note however that for WPS windows, "open handle" is a window handle, so try to use it as a window handle, if it fails, use WINHSWITCHFROMHAPP().

Thanks to Uri J. Stern for this one, I needed it dearly.

Checking the class of a window handle

Ok, here's the trick:

(example if you want to know if hwnd is a scroll bar)

CHAR achClass[256]

WinQueryClassName(hwnd,sizeof(achClass),achClass);
if(WinFindAtom(WinQuerySystemAtomTable(),achClass)
   == LOUSHORT(WC_SCROLLBAR))
   /* this is a scroll bar */;
else
   /* this is not a scroll bar */;
Thanks to Larry Salomon, Jr. in EDM/2 and to Robert Mahoney for letting me know about it.
Masking File Names in Strings
fnmatch.zip 5 kB

This is a wildcard string matcher that I "ported" from EMX to VisualAge C++ (although source codes are included). I have replaced EMX functions with VAC++ equivalents, and I have stripped out DBCS support, since, euh, I didn't know how to support it in VAC++.

The advantage of this routine over Dos*() API filename wildcard matcher is that you don't need an actual file to match a wildcard, you only need two strings. This can be very useful in many situations.

Hooks and the Workplace Shell

If you plan on using hooks from the Workplace Shell, make sure you make one DLL for the WPS Class and another DLL for the hook. I learned the hard way that a hook from a WPS DLL just doesn't work right (God knows why).

Leaving Hooks loaded after program execution

You can leave a hook loaded any time after a program has finished executing. In theory, you would only need not to execute WinReleaseHook(), but it seems WinSetHook() executes asynchronously from the calling program. There must be a better way to know when the hook is loaded, but here's how I do it (this requires a shared memory segment or a single stack DLL):

In the Hook DLL, I have this code:

BOOL loaded = FALSE;

BOOL EXPENTRY WinHookProc(HAB hab,PQMSG pqmsg, USHORT usRemove)
{
   loaded = TRUE;
   ...
}

BOOL EXPENTRY WaitLoaded(void)
{
   while(!loaded) DosSleep(10);
   return TRUE;
}
In the hook loader, I call WaitLoaded() before returning from the main() function. Note that this only seems to work for VIO executables... :( An example of this is in numcomma.zip (31 kB).

Scroll Bars "style"

If you ever need to check if a scroll bar is horizontal or vertical, check with SBS_VERT first (or only) since SBS_HORZ is actually set to 0 in PMWIN.H, which will always make an if statement fail. ie.:
if(WinQueryWindowULong(hwndscroll,QWL_STYLE) & SBS_VERT)
   /* this is a vertical scroll bar */;
else
   /* this is an horizontal scroll bar */;
and NOT
if(WinQueryWindowULong(hwndscroll,QWL_STYLE) & SBS_HORZ)
   /* this is an horizontal scroll bar */;
else
   /* this is a vertical scroll bar */;


Last Modified: March 1, 2005 09:59:22 EST.