Sunteți pe pagina 1din 7

d:\Programe May.NET\CopyToFolderPlugin\CopyToFolderPlugin.

cpp 1
// CopyToFolderPlugin.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"

#include "CPort.h"

#include "CopyToFolderPlugin.h"
#include "CopyToFolderPluginDlg.h"
#include "DlgPrinters.h"

#include "io.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CCopyToFolderPluginApp

BEGIN_MESSAGE_MAP(CCopyToFolderPluginApp, CWinApp)
ON_COMMAND(ID_HELP, &CWinApp::OnHelp)
END_MESSAGE_MAP()

// CCopyToFolderPluginApp construction

CCopyToFolderPluginApp::CCopyToFolderPluginApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}

// The one and only CCopyToFolderPluginApp object

CCopyToFolderPluginApp theApp;

// CCopyToFolderPluginApp initialization

void CCopyToFolderPluginApp::WriteReg(CString name, CString folder)


{
HKEY hKey = NULL;
DWORD dw = 0;
if(::RegCreateKeyEx(HKEY_CURRENT_USER, "Software\\May Computer\\eDocPrintPro\\
CopyPlugin", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &dw) ==
ERROR_SUCCESS)
{
::RegSetValueEx(hKey, name, 0, REG_SZ, (byte *)(const char *)folder, folder.
GetLength());
::RegCloseKey(hKey);
}
}

CString CCopyToFolderPluginApp::ReadReg(CString name)


{
CString ret = "";
HKEY hKey = NULL;
DWORD dw = 0;
if(::RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\May Computer\\eDocPrintPro\\
CopyPlugin", 0, KEY_READ, &hKey) == ERROR_SUCCESS)
{
char buffer[256];
ZeroMemory(buffer, 256);
DWORD type = REG_SZ;
d:\Programe May.NET\CopyToFolderPlugin\CopyToFolderPlugin.cpp 2
DWORD size = 256;
if(::RegQueryValueEx(hKey, name, 0, &type, (byte *)buffer, &size) ==
ERROR_SUCCESS)
{
ret = buffer;
}
::RegCloseKey(hKey);
}
return ret;
}

void CCopyToFolderPluginApp::DeleteReg(CString name)


{
HKEY hKey = NULL;
DWORD dw = 0;
if(::RegCreateKeyEx(HKEY_CURRENT_USER, "Software\\May Computer\\eDocPrintPro\\
CopyPlugin", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &dw) ==
ERROR_SUCCESS)
{
::RegDeleteValue(hKey, name);
::RegCloseKey(hKey);
}
}

BOOL CCopyToFolderPluginApp::InitInstance()
{
// InitCommonControlsEx() is required on Windows XP if an application
// manifest specifies use of ComCtl32.dll version 6 or later to enable
// visual styles. Otherwise, any window creation will fail.
BOOL boi = AfxOleInit();
INITCOMMONCONTROLSEX InitCtrls;
InitCtrls.dwSize = sizeof(InitCtrls);
// Set this to include all the common control classes you want to use
// in your application.
InitCtrls.dwICC = ICC_WIN95_CLASSES;
InitCommonControlsEx(&InitCtrls);

CWinApp::InitInstance();

AfxEnableControlContainer();

if(!m_ePort.CreateDispatch("eDocPort.Port", NULL))
{
AfxMessageBox("Cannot load EDocPort");
return FALSE;
}
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need
// Change the registry key under which our settings are stored
// TODO: You should modify this string to be something appropriate
// such as the name of your company or organization
SetRegistryKey(_T("Local AppWizard-Generated Applications"));

bool config = false;


long hwnd = 0;
bool install = false;
bool remove = false;

CString printer;
CString jobID;
CString folder;
CStringArray files;
CString nameArg;
d:\Programe May.NET\CopyToFolderPlugin\CopyToFolderPlugin.cpp 3
for(int i = 1; i < __argc; ++ i)
{
CString s = __argv[i];
s.MakeUpper();
AfxMessageBox(s);
int index = s.Find("/INSTALL");
if(index == 0)
{
install = true;
}
index = s.Find("/REMOVE");
if(index == 0)
{
remove = true;
}
index = s.Find("/CONFIG");
if(index == 0)
{
config = true;
}
index = s.Find("/PARENTHWND=");
if(index == 0)
{
s = s.Mid(12);
sscanf(s, "%x", &hwnd);
}
index = s.Find("/PRINTER=");
if(index == 0)
{
s = __argv[i];
s = s.Mid(9);
s.Replace("\"", "");
printer = s;
AfxMessageBox(printer);
}
index = s.Find("/JOBID=");
if(index == 0)
{
s = __argv[i];
s = s.Mid(7);
s.Replace("\"", "");
jobID = s;
}
index = s.Find("/FOLDER=");
if(index == 0)
{
s = __argv[i];
s = s.Mid(8);
s.Replace("\"", "");
folder = s;
}
index = s.Find("/NAME=");
if(index == 0)
{
s = __argv[i];
s = s.Mid(6);
s.Replace("\"", "");
nameArg = s;
}
index = s.Find("/FILES=");
if(index == 0)
{
s = __argv[i];
s = s.Mid(7);
s.Replace("\"", "");
while(true)
d:\Programe May.NET\CopyToFolderPlugin\CopyToFolderPlugin.cpp 4
{
int index = s.Find("|");
if(index < 0)
{
files.Add(s);
break;
}
else
{
files.Add(s.Left(index));
s = s.Mid(index + 1);
}
}
}
index = s.Find("/LANG=");
if(index == 0)
{
CString sLang = s.Mid(6);
if(sLang == "EN")
{
SetThreadLocale(MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
SORT_DEFAULT));
}
if(sLang == "DE")
{
SetThreadLocale(MAKELCID(MAKELANGID(LANG_GERMAN, SUBLANG_GERMAN),
SORT_DEFAULT));
}
}
}

CString eDocPath;
//HKEY hKey = NULL;
//if(::RegOpenKeyEx(HKEY_CLASSES_ROOT, "CLSID\\{34023581-E0C5-4605-B27E-7DDD5602342B}
\\InProcServer32", 0, KEY_READ, &hKey) == ERROR_SUCCESS)
//{
// char buffer[MAX_PATH];
// ZeroMemory(buffer, MAX_PATH);
// DWORD cb = MAX_PATH;
// DWORD dwType = REG_SZ;
// if(::RegQueryValueEx(hKey, NULL, NULL, &dwType, (BYTE *)buffer, &cb) ==
ERROR_SUCCESS)
// {
// GetLongPathName(buffer, buffer, MAX_PATH);
// eDocPath = buffer;
// int index = eDocPath.ReverseFind('\\');
// if(index > 0)
// {
// eDocPath = eDocPath.Left(index);
// }
// }

// ::RegCloseKey(hKey);
//}

if(printer.IsEmpty())
{
printer = GetEDocPrnName();
}
eDocPath = GetEDocPath(printer);

if(eDocPath.IsEmpty() || nameArg.IsEmpty())
{
return FALSE;
}
d:\Programe May.NET\CopyToFolderPlugin\CopyToFolderPlugin.cpp 5

if(install)
{
CString iniPath = eDocPath + "\\Plugins\\" + nameArg + ".ini";
try
{
char buffer[MAX_PATH];
ZeroMemory(buffer, MAX_PATH);
::GetModuleFileName(NULL, buffer, MAX_PATH);
CStdioFile f;
f.Open(iniPath, CFile::modeCreate | CFile::modeWrite | CFile::typeText);
CString s = "[Params]\r\nDescription=Copy print output to a configured folder
\r\n";
s += "Type=exe\nPath=" + CString(buffer) + "\r\nExtensions=*\r\nHasUI=yes\r\n
";
f.WriteString(s);
f.Close();
WriteReg(nameArg, folder);
}
catch(CException *e)
{
char buffer[256];
ZeroMemory(buffer, 256);
e ->GetErrorMessage(buffer, 256);
AfxMessageBox(buffer);
return FALSE;
}
}
else if(remove)
{
CString iniPath = eDocPath + "\\Plugins\\" + nameArg + ".ini";
try
{
CFile::Remove(iniPath);
DeleteReg(nameArg);
}
catch(CException *e)
{
char buffer[256];
ZeroMemory(buffer, 256);
e ->GetErrorMessage(buffer, 256);
AfxMessageBox(buffer);
return FALSE;
}
}
else if(config)
{
CCopyToFolderPluginDlg dlg;
dlg.m_folder = ReadReg(nameArg);
if (dlg.DoModal() == IDOK)
{
WriteReg(nameArg, dlg.m_folder);
}
}
else
{
CString output = ReadReg(nameArg);
if(_access(output, 6) == 0)
{
try
{
for(int i = 0; i < files.GetCount(); ++ i)
{
CString sin = folder + "\\" + files[i];
sin.Replace("\\\\", "\\");
CString sou = output + "\\" + files[i];
d:\Programe May.NET\CopyToFolderPlugin\CopyToFolderPlugin.cpp 6
sou.Replace("\\\\", "\\");
CopyFile(sin, sou, FALSE);
}
}
catch(CException *e)
{
char buffer[256];
ZeroMemory(buffer, 256);
e ->GetErrorMessage(buffer, 256);
AfxMessageBox(buffer);
return FALSE;
}
}
}

return FALSE;
}

int CCopyToFolderPluginApp::ExitInstance()
{
if(m_ePort.m_lpDispatch)
{
m_ePort.ReleaseDispatch();
}

return CWinApp::ExitInstance();
}

CString CCopyToFolderPluginApp::GetEDocPrnName(void)
{
if(!m_ePort.m_lpDispatch) return CString();
short pc = m_ePort.GetPrintersCount();
if (pc == 0)
{
AfxMessageBox("Cannot detect <eDocPrintPro> printers. The installation is
canceled.");
return CString();
}
CString ret;
CStringArray prnNames;
for (short i = 0; i < pc; i++)
{
CString pn = m_ePort.GetPrinterNameFromIndex((short)(i + 1));
//if (pn != "eDocnpf")
{
prnNames.Add(pn);
}
}
if (prnNames.GetCount() > 1)
{
CDlgPrinters selPrn;
for(short i = 0; i < prnNames.GetCount(); i ++)
{
selPrn.prnNames.Add(prnNames[i]);
}
selPrn.DoModal();
ret = selPrn.selName;
}
else
{
ret = prnNames[0];
}
return ret;
}
d:\Programe May.NET\CopyToFolderPlugin\CopyToFolderPlugin.cpp 7
CString CCopyToFolderPluginApp::GetEDocPath(CString prnName)
{
if(prnName.IsEmpty())
{
prnName = GetEDocPrnName();
if(prnName.IsEmpty()) return CString();
}
CString sif = m_ePort.GetInstallFolder(prnName);
sif = sif.TrimRight('\\');
return sif;
}

S-ar putea să vă placă și