I am trying to write my own implementation of GetSystemDirectory, and my pointer comparison seems to be failing.

Code:
UINT xGetSystemDirectory(char *pszDest, UINT iLen)
{
	char szBuf[MAX_PATH], *p, *q;
	unsigned i;

	GetModuleFileName(GetModuleHandle("kernel32"), szBuf, sizeof szBuf - 1);

	q = strrchr(szBuf, '\\');

	for(i = 0, p = pszDest; i < iLen && p != q; *p++ = szBuf[i], i++);

	*p = '\0';
	
	return i;
}
this function produces: C:\windows\system32\kernel32.dll in pszDest. comparing p to q seems to be failing. the loop should be stopping at the last backslash, the result should be: C:\windows\system32

any help is greatly appreciated. thanks in advance.