Click here to Skip to main content
15,885,244 members
Articles / Desktop Programming / WTL

Screen Event Recorder DLL/Application

Rate me:
Please Sign up or sign in to vote.
4.91/5 (34 votes)
9 May 2003MIT3 min read 213.7K   4.4K   105  
Screen Event Recorder (DLL) shows how to create a DLL/Application (one that can be used with RunDll32.exe).
///////////////////////////////////////////////////////////////////////////////
//	File:		Playfile.cpp
//	Version:	1.5
//  Implementation file dialog and PlayFile (RunDll32.exe) extension
//
//	Author:		Ernest Laurentin
//	E-mail:		elaurentin@netzero.net
//
//  Permission to use, copy, modify, distribute and sell this software
//  and its documentation for any purpose is hereby granted without fee,
//  provided that the above copyright notice appear in all copies and
//  that both that copyright notice and this permission notice appear
//  in supporting documentation.
//
//	This file is provided "as is" with no expressed or implied warranty.
//	The author accepts no liability for any damage/loss of business that
//	this software may cause.
//
//	Version history
//  Ver 1.0 - 04/24/2003 - Initial Release
//  Ver 1.5 - 05/04/2003 - Bug fixes, Add 'CPlayFileDlg' ATL-class
///////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Resource.h"
#include <tchar.h>   // for _tcscpy, _tcsncmp, _tcslen
#include <stdio.h>	 // for sscanf
#include "PlayFileDlg.h"
#include "MacRcrdImport.h"

///////////////////////////////////////////////////////////////////////////////
// L O C A L S
int PlayFileInt(HWND hwnd, HINSTANCE hinst, LPCTSTR lpCmdLine, int nCmdShow);
void GetPlayfileArgs(LPCTSTR lpCmdLine, LPTSTR szFileBuffer, bool& bPlay);


///////////////////////////////////////////////////////////////////////////////
// PlayFileA
// DESCRIPTION
//  This function is called from 'rundll32'
//  rundll32 DllName,PlayFile [Arguments]
//
// PARAMETERS:
//	HWND hwnd		  :	handle to owner window
//	HINSTANCE hinst	  : instance handle (DLL instance)
//  LPSTR	lpCmdLine : command-line
//	int		nCmdShow  : show state
///////////////////////////////////////////////////////////////////////////////
#pragma comment(linker, "/EXPORT:PlayFileA@16=PlayFileA")
void CALLBACK PlayFileA(HWND hwnd, HINSTANCE hinst, LPCSTR lpCmdLine, int nCmdShow)
{
	TCHAR szCommandLine[MAX_PATH] = { 0 };

#ifdef _UNICODE
	MultiByteToWideChar(CP_ACP, 0, lpCmdLine, -1, szCommandLine, MAX_PATH);
#else
	_tcsncpy(szCommandLine, lpCmdLine, MAX_PATH);
#endif

	PlayFileInt( hwnd, hinst, szCommandLine, nCmdShow);
}


///////////////////////////////////////////////////////////////////////////////
// PlayFileW
// DESCRIPTION
//  This function is called from 'rundll32'
//  rundll32 DllName,PlayFile [Arguments]
//
// PARAMETERS:
//	HWND hwnd		  :	handle to owner window
//	HINSTANCE hinst	  : instance handle (DLL instance)
//  LPSTR	lpCmdLine : command-line
//	int		nCmdShow  : show state
///////////////////////////////////////////////////////////////////////////////
#pragma comment(linker, "/EXPORT:PlayFileW@16=PlayFileW")
void CALLBACK PlayFileW(HWND hwnd, HINSTANCE hinst, LPCWSTR lpCmdLine, int nCmdShow)
{
	TCHAR szCommandLine[MAX_PATH] = { 0 };

#ifndef _UNICODE
	WideCharToMultiByte(CP_ACP, 0, lpCmdLine, -1, szCommandLine, MAX_PATH, NULL, NULL);
#else
	_tcsncpy(szCommandLine, lpCmdLine, MAX_PATH);
#endif

	PlayFileInt( hwnd, hinst, szCommandLine, nCmdShow);
}


///////////////////////////////////////////////////////////////////////////////
// PlayFileInt
int PlayFileInt(HWND hwnd, HINSTANCE hinst, LPCTSTR lpCmdLine, int nCmdShow)
{
	// get command-line arguments
	TCHAR szFileName[MAX_PATH] = { 0 };
	bool bPlay = false;
	GetPlayfileArgs(lpCmdLine, (LPTSTR) szFileName, bPlay);

	TCHAR szClass[32] = { 0 };
	GetClassName(hwnd, szClass, 32);
	// must be run by 'RunDll32.exe'
	//if (_tcsicmp(szClass, _T("RunDLL")) != 0)
	//	return 0;

	// show dialog
	CPlayFileDlg dlg;
	dlg.SetFileName( szFileName );
	dlg.SetAutoPlay( bPlay );
	int nResult = dlg.DoModal(hwnd);
	return nResult;
}

///////////////////////////////////////////////////////////////////////////////
// GetPlayfileArgs
void GetPlayfileArgs(LPCTSTR lpCmdLine, LPTSTR szFileBuffer, bool& bPlay)
{
	LPCTSTR lpszCommand = lpCmdLine;
	int nToParse = _tcslen( lpszCommand );
	while( nToParse > 0 )
	{
		if (lpszCommand[0] == TCHAR('-') || lpszCommand[0]==TCHAR('/'))
		{
			int nSkip = 1;

			if (_tcsnicmp((lpszCommand+1), _T("FILE:"), 5) == 0)
			{
				_stscanf((lpszCommand+6), _T("%s"), szFileBuffer);
				nSkip = _tcslen( szFileBuffer ) + 6; // 6 = '/FILE:'
			}
			else if (_tcsnicmp((lpszCommand+1), _T("PLAY"), 4) == 0)
			{
				bPlay = true;
				nSkip = 5; // 5 = '/PLAY'
			}

			// Skip...till next argument
			if (nToParse > 0)
			{
				lpszCommand = lpszCommand + nSkip;
				nToParse -= nSkip;
				while( (nToParse>0) && (lpszCommand[0] != TCHAR('-')) && (lpszCommand[0] != TCHAR('/')))
				{
					lpszCommand++;
					nToParse--;
				}
			}
		}
		else
			nToParse = 0;
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Software Developer (Senior)
United States United States
Ernest is a multi-discipline software engineer.
Skilled at software design and development for all Windows platforms.
-
MCSD (C#, .NET)
Interests: User Interface, GDI/GDI+, Scripting, Android, iOS, Windows Mobile.
Programming Skills: C/C++, C#, Java (Android), VB and ASP.NET.

I hope you will enjoy my contributions.

Comments and Discussions