TCUtils.cpp

From TotalcmdWiki
Jump to: navigation, search
#include "TCUtils.h"
#include <strsafe.h>

void TCUtils::sendUserCommand (const char* userCommand, HWND sourceWindow)
{
	HWND targetWindow = FindWindow ("TTOTAL_CMD", NULL);
	if (targetWindow)
	{
		COPYDATASTRUCT copyStruct;
		ZeroMemory (&copyStruct, sizeof (COPYDATASTRUCT));
		copyStruct.dwData = 'E' + 256 * 'M';
		copyStruct.cbData = strlen (userCommand) +1;
		copyStruct.lpData = (PVOID)userCommand;
		SendMessage (targetWindow, WM_COPYDATA, (WPARAM)sourceWindow, (LPARAM)&copyStruct);
	}
}

void TCUtils::sendChangeDirectory (const char* firstPath, const char* secondPath, const char* flags, HWND sourceWindow)
{	
	HWND targetWindow = FindWindow ("TTOTAL_CMD", NULL);
       if (!targetWindow)
	{
		return;
	}
	COPYDATASTRUCT copyStruct;
	ZeroMemory (&copyStruct, sizeof (COPYDATASTRUCT));		
	copyStruct.dwData = 'C' + 256 * 'D';
	char commandLine [MAX_PATH] = {0};		
	// One path is mandatory.
	if (!firstPath && !secondPath)
	{
		return;
	}
	// First or second path or both can be entered separated by carriage return.
	if (firstPath)
	{
		StringCchCopy (commandLine, MAX_PATH, firstPath);
	}
	StringCchCat (commandLine, MAX_PATH, "\r");
	if (secondPath)
	{
		StringCchCat (commandLine, MAX_PATH, secondPath);
	}		
	if (flags)
	{
		StringCchCat (commandLine + strlen(commandLine) +1, MAX_PATH, flags);
	}
	// Calculate command line length.
	copyStruct.cbData = strlen (commandLine) +1;
	if (flags)
	{
		copyStruct.cbData += strlen(flags) +1;
	}		
	copyStruct.lpData = (PVOID)commandLine;
	SendMessage (targetWindow, WM_COPYDATA, (WPARAM)sourceWindow, (LPARAM)&copyStruct);
}