This repository has been archived by the owner on Jan 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmain.cpp
149 lines (131 loc) · 4.32 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* main.cpp
*
* Created on: 13.01.2015
* Author: Forsaken
*/
#include "main.h"
#include "dll_funcs.h"
#include "PrinterSettings.h"
#include <iostream>
#include <stdio.h>
#include <tchar.h>
#include "Log.h"
#include "logging/ConsoleTarget.h"
#include "logging/FileLogger.h"
#include <direct.h>
#include <Shlobj.h>
#include <sys/stat.h>
// 1 MB
#define MAX_LOG_FILE_SIZE 1048576
using namespace std;
static const TCHAR szWindowClass[] = TEXT("FixUp3DCLS");
static const TCHAR szTitle[] = TEXT("FixUp3D");
extern "C" BOOLEAN WINAPI DllMain(HINSTANCE hDllHandle, DWORD nReason, LPVOID Reserved) {
switch (nReason) {
case DLL_PROCESS_ATTACH:
{
// Initialize original winusb lib
if( !InitWinUsbWrapper() )
{
MessageBox(NULL, TEXT("Failed to load original winusb.dll"), TEXT("FixUp3D"), MB_OK);
return FALSE;
}
// Initialize settings window
HANDLE hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)initializeSettingsWindow, (LPVOID)hDllHandle, 0, NULL);
if (hThread == 0) {
MessageBox(NULL, TEXT("Failed to create window thread"), TEXT("FixUp3D"), MB_OK);
return FALSE;
}
// Initialize log targets
TCHAR sHomeDir[MAX_PATH];
TCHAR sFilename[MAX_PATH];
if(SUCCEEDED(SHGetFolderPath(NULL, CSIDL_PERSONAL|CSIDL_FLAG_CREATE, NULL, 0, sHomeDir))) {
_stprintf(sFilename, TEXT("%s\\UpUsbIntercept"), sHomeDir);
// Ensure directory exists
_mkdir(sFilename);
// Create log writer
_stprintf(sFilename, TEXT("%s\\UpUsbIntercept\\PrinterIntercept.log"), sHomeDir);
// Check log size
struct stat stat_buf;
int rc = stat(sFilename, &stat_buf);
// Add file target
if ((rc != 0) || (stat_buf.st_size > MAX_LOG_FILE_SIZE)) {
// File does not exists or is too big, overwrite
Log::addTarget("file_default", new Logging::FileLogger(sFilename, ios_base::out, LogLevel::DEBUG, LogSections::SECTION_DEFAULT));
} else {
// File exists and is below max size, append
Log::addTarget("file_default", new Logging::FileLogger(sFilename, ios_base::app | ios_base::out, LogLevel::DEBUG, LogSections::SECTION_DEFAULT));
}
// Raw usb log
_stprintf(sFilename, TEXT("%s\\UpUsbIntercept\\UsbRaw.log"), sHomeDir);
Log::addTarget("file_usb_raw", new Logging::FileLogger(sFilename, ios_base::out, LogLevel::DEBUG, LogSections::SECTION_USB_RAW));
}
// Generic console log
Log::addTarget("console", new Logging::ConsoleTarget(LogLevel::INFO, LogSections::SECTION_ANY));
}
break;
case DLL_PROCESS_DETACH:
{
Core::PrinterSettings *settings = Core::PrinterSettings::getInstance();
if (settings != NULL) {
settings->writeSettingsToConfig();
}
}
break;
default:
break;
}
return TRUE;
}
LRESULT CALLBACK PrinterDialogWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
return Core::PrinterSettings::getInstance()->handleWndMessage(hWnd, message, wParam, lParam);
}
int initializeSettingsWindow(HINSTANCE hDllHandle)
{
Core::PrinterSettings *settings = Core::PrinterSettings::getInstanceNew(hDllHandle);
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = PrinterDialogWndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hDllHandle;
wcex.hIcon = 0;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = 0;
if (!RegisterClassEx(&wcex)) {
MessageBox(NULL, TEXT("Call to RegisterClassEx failed"), TEXT("FixUp3D"), MB_OK);
return 1;
}
HWND hWnd = CreateWindowEx( WS_EX_TOPMOST,
szWindowClass, szTitle,
(WS_OVERLAPPED | WS_CAPTION | /*WS_SYSMENU |*/ WS_MINIMIZEBOX),
CW_USEDEFAULT, CW_USEDEFAULT,
476, 440,
NULL,
NULL,
hDllHandle,
NULL );
if (!hWnd) {
TCHAR error[64];
_stprintf( error, TEXT("Call to CreateWindow failed (Error %lu)"), GetLastError() );
MessageBox(NULL, error, TEXT("FixUp3D"), MB_OK);
return 2;
}
// Show window
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
settings->setHWnd(hWnd);
// Main message loop:
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
}