brainlilo/BrainLILO.cpp

162 lines
3.4 KiB
C++
Raw Normal View History

2020-09-28 03:19:37 +09:00
#define BRAINLILO_API extern "C" __declspec(dllexport)
#include "BrainLILO.h"
#include "BrainLILODrv.h"
#include <string.h>
2022-01-24 01:52:45 +09:00
static bool g_initialized = false;
static HANDLE g_helperHandle = NULL;
2020-09-28 03:19:37 +09:00
static HINSTANCE g_hInstance;
2022-01-24 01:52:45 +09:00
static void initialize()
{
if (g_initialized)
2020-09-28 03:19:37 +09:00
return;
2022-01-24 01:52:45 +09:00
g_initialized = true;
2020-09-28 03:19:37 +09:00
}
2022-01-24 01:52:45 +09:00
static void getThisDllDirectoryPath(LPWSTR buffer)
{
// retrive the path of the application.
GetModuleFileName(g_hInstance, buffer, 512);
const size_t notFound = (size_t)-1;
size_t i = 0;
size_t j = notFound;
while (buffer[i])
{
if (buffer[i] == L'/' || buffer[i] == L'\\')
{
j = i;
2020-09-28 03:19:37 +09:00
}
i++;
}
2022-01-24 01:52:45 +09:00
if (j == notFound)
2020-09-28 03:19:37 +09:00
return;
2022-01-24 01:52:45 +09:00
buffer[j] = 0;
2020-09-28 03:19:37 +09:00
}
2022-01-24 01:52:45 +09:00
static bool isDriverLoaded()
{
2020-09-28 03:19:37 +09:00
HANDLE handle;
2022-01-24 01:52:45 +09:00
handle =
CreateFile(L"LIN0:", GENERIC_READ | GENERIC_WRITE | FILE_WRITE_ATTRIBUTES, 0, NULL, OPEN_EXISTING, 0, NULL);
if (handle == INVALID_HANDLE_VALUE)
handle = NULL;
if (handle)
{
2020-09-28 03:19:37 +09:00
CloseHandle(handle);
return true;
}
return false;
}
2022-01-24 01:52:45 +09:00
static void loadDriverIfNeeded()
{
if (isDriverLoaded())
2020-09-28 03:19:37 +09:00
return;
2022-01-24 01:52:45 +09:00
OutputDebugString(L"BrainLILO: installing LILODriver...");
DWORD err;
HANDLE driver = NULL;
2020-09-28 03:19:37 +09:00
wchar_t driverPath[1024];
2022-01-24 01:52:45 +09:00
getThisDllDirectoryPath(driverPath);
TCHAR newDriverPath[128];
getThisDllDirectoryPath(driverPath);
wcscat(driverPath, L"\\BrainLILODrv.dll");
2022-01-24 01:52:45 +09:00
wcscpy(newDriverPath, L"\\Windows\\BrainLILODrv.dll");
2020-09-28 03:19:37 +09:00
{
wchar_t buf[1024];
2022-01-24 01:52:45 +09:00
swprintf(buf, L"BrainLILO: copying \"%ls\" to \"%ls\"", driverPath, newDriverPath);
2020-09-28 03:19:37 +09:00
OutputDebugString(buf);
}
2022-01-24 01:52:45 +09:00
if (!CopyFile(driverPath, newDriverPath, FALSE))
{
if (GetFileAttributes(newDriverPath) == (DWORD)-1)
{
OutputDebugString(L"BrainLILO: failed to copy");
return;
}
}
OutputDebugString(L"BrainLILO: registering BrainLILODriver...");
try
{
driver = RegisterDevice(L"LIN", 0, L"\\Windows\\BrainLILODrv.dll", 0);
if (driver == INVALID_HANDLE_VALUE)
driver = NULL;
if (!driver)
{
driver = RegisterDevice(L"LIN", 0, L"BrainLILODrv.dll", 0);
if (driver == INVALID_HANDLE_VALUE)
driver = NULL;
}
err = GetLastError();
}
catch (DWORD e)
{
err = e;
}
if (!driver && (err != 0x964))
{
OutputDebugString(L"BrainLILO: failed to install...");
return;
}
2020-09-28 03:19:37 +09:00
}
2022-01-24 01:52:45 +09:00
static void openDriver()
{
if (g_helperHandle)
2020-09-28 03:19:37 +09:00
return;
2022-01-24 01:52:45 +09:00
g_helperHandle =
CreateFile(L"LIN0:", GENERIC_READ | GENERIC_WRITE | FILE_WRITE_ATTRIBUTES, 0, NULL, OPEN_EXISTING, 0, NULL);
if (g_helperHandle == INVALID_HANDLE_VALUE)
g_helperHandle = NULL;
2020-09-28 03:19:37 +09:00
}
2022-01-24 01:52:45 +09:00
extern "C" BOOL APIENTRY DllMain(HINSTANCE hInstance, DWORD ul_reason_for_call, LPVOID lpReserved)
2020-09-28 03:19:37 +09:00
{
switch (ul_reason_for_call)
2022-01-24 01:52:45 +09:00
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
g_hInstance = hInstance;
initialize();
loadDriverIfNeeded();
openDriver();
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
2020-09-28 03:19:37 +09:00
}
return TRUE;
}
2022-01-24 01:52:45 +09:00
BRAINLILO_API BOOL RKDoSoftReset()
{
initialize();
2020-09-28 03:19:37 +09:00
loadDriverIfNeeded();
openDriver();
2022-01-24 01:52:45 +09:00
return DeviceIoControl(g_helperHandle, IOCTL_LIN_DO_LINUX, NULL, 0, NULL, 0, NULL, NULL);
2020-09-28 03:19:37 +09:00
}