📄 loader.c
字号:
/*
This file is part of KCeasy (http://www.kceasy.com)
Copyright (C) 2002-2004 Markus Kern <mkern@kceasy.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
//---------------------------------------------------------------------------
#include <windows.h>
#pragma hdrstop
#include "loader.rc"
//---------------------------------------------------------------------------
#pragma argsused
#define USE_WATCHDOG
#define GIFT_DLL "gift.dll"
#define GIFT_MAIN "gift_main"
extern void __cdecl _setargv (void);
typedef int (*GiftMain) (int argc, char **argv);
#ifdef USE_WATCHDOG
DWORD WINAPI WatchdogThreadFunc (void *data);
#endif
WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
HINSTANCE hDll;
HANDLE hMutex;
#ifdef USE_WATCHDOG
HANDLE hKCeasyMutex;
HANDLE hWatchdogThread;
DWORD WatchdogThreadID;
#endif
GiftMain gift_main;
int gift_ret;
/* load gift dll */
hDll = LoadLibrary (GIFT_DLL);
if (hDll == NULL)
{
char ErrStr[4096] = "Couldn't load " GIFT_DLL ":\r\n";
FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM,
NULL, GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
ErrStr + strlen(ErrStr), 4096 - strlen(ErrStr), NULL);
MessageBox (0, ErrStr, "giFT Loader - Error", MB_OK | MB_ICONERROR);
return 0;
}
gift_main = (GiftMain)GetProcAddress (hDll, GIFT_MAIN);
if (hDll == NULL)
{
FreeLibrary (hDll);
MessageBox (0, "Couldn't find symbol \"" GIFT_MAIN "\" in " GIFT_DLL, "giFT Loader - Error", MB_OK | MB_ICONERROR);
return 0;
}
/* set up mutex */
hMutex = CreateMutex (NULL, FALSE, BRAND_LOADER_MUTEX_NAME);
if (hMutex == NULL)
{
FreeLibrary (hDll);
MessageBox (0, "Couldn't create named mutex \"" BRAND_LOADER_MUTEX_NAME "\"", "giFT Loader - Error", MB_OK | MB_ICONERROR);
return 0;
}
if (GetLastError () == ERROR_ALREADY_EXISTS)
{
FreeLibrary (hDll);
CloseHandle (hMutex);
MessageBox (0, "Another instance of giFT Loader is already running", "giFT Loader - Error", MB_OK | MB_ICONERROR);
return 0;
}
#ifdef USE_WATCHDOG
/* check that KCeasy is running */
hKCeasyMutex = OpenMutex(SYNCHRONIZE,FALSE,BRAND_GLOBAL_MUTEX_NAME);
if (hKCeasyMutex == NULL)
{
FreeLibrary (hDll);
CloseHandle (hMutex);
MessageBox (0, "Couldn't find KCeasy mutex \"" BRAND_GLOBAL_MUTEX_NAME "\"", "giFT Loader - Error", MB_OK | MB_ICONERROR);
return 0;
}
CloseHandle (hKCeasyMutex);
/* start watchdog thread */
hWatchdogThread = CreateThread (NULL, 0, WatchdogThreadFunc, NULL, 0, &WatchdogThreadID);
#endif
/* parse command line args and switch to giFT */
_setargv ();
gift_ret = gift_main (__argc, __argv);
/* terminate watchdog thread */
TerminateThread (hWatchdogThread, 0);
CloseHandle (hWatchdogThread);
/* unload dll and remove mutex */
FreeLibrary (hDll);
CloseHandle (hMutex);
ExitProcess (0);
}
//---------------------------------------------------------------------------
#ifdef USE_WATCHDOG
DWORD WINAPI WatchdogThreadFunc (void *data)
{
HANDLE hKCeasyMutex;
while (1)
{
/* sleep for a minute */
Sleep (1000*60);
/* check that KCeasy is running */
if ((hKCeasyMutex = OpenMutex(SYNCHRONIZE,FALSE,BRAND_GLOBAL_MUTEX_NAME)) == NULL)
{
/* wait for another 30 seconds */
Sleep (1000*30);
if ((hKCeasyMutex = OpenMutex(SYNCHRONIZE,FALSE,BRAND_GLOBAL_MUTEX_NAME)) == NULL)
{
#if 0
MSG msg;
PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE);
MessageBox (0, "Couldn't find KCeasy mutex \"" BRAND_GLOBAL_MUTEX_NAME "\"\n"
"Killing self now.", "giFT Loader - Error", MB_OK | MB_ICONERROR);
#endif
ExitProcess (0);
}
}
CloseHandle (hKCeasyMutex);
}
}
#endif /* USE_WATCHDOG */
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -