///////////////////////////////////////////////////////////////////////////////
// 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;
}
}