Hi

I need to find out if a specific cmd window is opened on the pc, and I need to hide it
I looking for that cmd window by its name (cause i know it )

Ive used the EnumWindows with a callback function:

BOOL vFound=((::EnumWindows(mFindWindowOnlyByCustomTitle,0)));

and this is the CALLBACK function code

BOOL CALLBACK mFindWindowOnlyByCustomTitle(HWND hwnd, LPARAM lParam)
{
    CFileException ex;
    TCHAR buf[256] = {'\0'};
    
	::ZeroMemory(buf, 256);
    ::SendMessage(hwnd, WM_GETTEXT, 256, (LPARAM)buf);

    if (strlen(buf) > 0)
	{
		CFile vSourceFile;
		vSourceFile.Open(buf, CFile::modeRead | CFile::shareDenyWrite, &ex);

		if (strstr(buf,aName)!=NULL)
		{
			HWND hWnd = FindWindow(NULL, buf);
			if(hWnd){
				return FALSE;
			}
		}
	}
    return TRUE; // process more windows
}

the problem is that after the ::SendMessage(hwnd, WM_GETTEXT, 256, (LPARAM)buf); line
buf got a partial string of the window title (it cuts first characters) for example instead of "Continue" it hold "inue" and etc....

what could be the problem?

Thanx ahead?

>>::ZeroMemory(buf, 256);
Since you declared buf as TCHAR you need to use the sizeof operator, like this: ::ZeroMemory(buf, sizeof(buf)); >>strlen(buf)
Can't use strlen() in TCHAR because it won't work when compiled for UNICODE. Use the macro _tsclen() instead, which will get correctly converted to either strlen() or wsclen(), depending on UNICODE setting.

Note: If you don't want to compile for UNICODE then don't use TCHAR.

Thanx but it does not solved my problem

the problem is in the

::SendMessage(hwnd, WM_GETTEXT, 256, (LPARAM)buf);

line. After this line i receive the partial string inside the buf:

+ buf   0x0012fcb4 "inue"   char [256]

and i did fixed into ::ZeroMemory(buf, sizeof(buf)); which did not change anything....

Any other ideas?

Thx ahead.

Edited 3 Years Ago by Reverend Jim: Fixed formatting

the problem is that after the ::SendMessage(hwnd, WM_GETTEXT, 256, (LPARAM)buf); line
buf got a partial string of the window title (it cuts first characters) for example instead of "Continue" it hold "inue" and etc....

what could be the problem?

You seem to be experiencing what MS has (vaguely) documented as follows:

Windows NT/2000/XP:ANSI applications may have the string in the buffer reduced in size (to a minimum of half that of the wParam value) due to conversion from ANSI to Unicode.

Try coding the mFindWindowOnlyByCustomTitle() in Unicode to avoid the internal Ansi to Unicode conversion.

try this to see if it works. Its ok on my computer

#include <windows.h>
#include <iostream>

BOOL CALLBACK mFindWindowOnlyByCustomTitle(HWND hwnd, LPARAM lParam)
{
    char buf[256] = {'\0'};
    
    ::SendMessage(hwnd, WM_GETTEXT, 256, (LPARAM)buf);
    std::cout << "\"" << buf << "\"\n";
    return TRUE; // process more windows
}
int main()
{
BOOL vFound=((::EnumWindows(mFindWindowOnlyByCustomTitle,0)));
}

I just found out that the error of incomplete procccess name does works for the first time, BUT after i execute the following code before calling the EnumWindows for the second (and the problematic) time

void Utils::mExecuteScript(CString vScript)
{

    SHELLEXECUTEINFO vInfo;
    
    ::ZeroMemory(&vInfo, sizeof(vInfo));

    vInfo.cbSize = sizeof(vInfo);
	vInfo.lpFile = ((char *)vScript.GetBuffer());

	vInfo.fMask  = SEE_MASK_NOCLOSEPROCESS;
    
	ShellExecuteEx(&vInfo);

	WaitForSingleObject(vInfo.hProcess, 0);

}

which execute some batch file, The EnumWindows starts to cut fhe first letters of the enumerated windows name...

What have i done wrong in this function?

thanx ahead...

A couple possibilities:
1) copy the string in vScript into a character buffer then use that buffer for lpFile pointer.

2) explicitly specify the verb to be used. According to MSCN the default verb may, or may not, be "open", but is that what you want?

  • For systems prior to Windows 2000, the default verb is used if it is valid and available in the registry. If not, the "open" verb is used.
  • For Windows 2000 and later systems, the default verb is used if available. If not, the "open" verb is used. If neither verb is available, the system uses the first verb listed in the registry.
This article has been dead for over six months. Start a new discussion instead.