This repository has been archived on 2022-06-27. You can view files and clone it, but cannot push or open issues or pull requests.
Xash3DArchive/launch/rundll.h

145 lines
3.7 KiB
C
Raw Normal View History

2007-06-21 22:00:00 +02:00
//=======================================================================
// Copyright (C) XashXT Group 2007
2007-11-10 22:00:00 +01:00
// rundll.h - used for find engine library
2007-06-21 22:00:00 +02:00
// getting direct path or from environment
//=======================================================================
#ifndef GETLIB_H
#define GETLIB_H
#define NDEBUG
#include <sys\types.h>
#include <windows.h>
#include <winreg.h>
#include <stdio.h>
2007-09-13 22:00:00 +02:00
#pragma comment(lib, "msvcrt")
2007-06-21 22:00:00 +02:00
#pragma comment(linker,"/MERGE:.rdata=.text")
#pragma comment(linker,"/FILEALIGN:512 /SECTION:.text, EWRX /IGNORE:4078")
2007-12-11 22:00:00 +01:00
#define Run( prog ) int main(int argc, char **argv)\
{ return CreateMain32()( #prog, TRUE ); }
2007-10-05 22:00:00 +02:00
#define Run32( prog ) int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)\
2007-12-11 22:00:00 +01:00
{ return CreateMain32()( #prog, FALSE ); }
2007-06-21 22:00:00 +02:00
2007-11-10 22:00:00 +01:00
// engine entry point format
2008-07-30 22:00:00 +02:00
typedef int (*winmain_t)( char *hostname, int console );
2007-06-21 22:00:00 +02:00
char szSearch[ 5 ][ 1024 ];
char szFsPath[ 4096 ];
HINSTANCE hmain;
2007-09-13 22:00:00 +02:00
void GetError( const char *errorstring )
{
HINSTANCE user32_dll = LoadLibrary( "user32.dll" );
static int (*pMessageBoxA)(HWND hWnd, LPCSTR lpText, LPCSTR lpCaption, UINT uType);
if(!errorstring) exit( 0 );
if( user32_dll ) pMessageBoxA = (void *)GetProcAddress( user32_dll, "MessageBoxA" );
if(pMessageBoxA) pMessageBoxA( 0, errorstring, "Error", MB_OK );
if( user32_dll ) FreeLibrary( user32_dll ); // no need anymore...
exit( 1 );
}
2007-06-21 22:00:00 +02:00
BOOL GetBin( void )
{
//grab direct path?
return TRUE;
}
BOOL GetEnv( void )
{
char *pEnvPath = getenv("Xash3D");
if(!pEnvPath) return FALSE;
strcpy(szFsPath, pEnvPath );
return TRUE;
}
BOOL GetReg( void )
{
2007-09-13 22:00:00 +02:00
HINSTANCE advapi32_dll = LoadLibrary( "advapi32.dll" );
static long(_stdcall *pRegOpenKeyEx)(HKEY,LPCSTR,DWORD,REGSAM,PHKEY);
static long(_stdcall *pRegQueryValueEx)(HKEY,LPCSTR,LPDWORD,LPDWORD,LPBYTE,LPDWORD);
static long(_stdcall *pRegCloseKey)(HKEY);
2007-10-24 22:00:00 +02:00
DWORD dwBufLen = 4096; // max env length
HKEY hKey;
long lRet;
2007-11-08 22:00:00 +01:00
BOOL result = FALSE;
2007-06-21 22:00:00 +02:00
2007-09-13 22:00:00 +02:00
if(advapi32_dll)
{
pRegCloseKey = (void *)GetProcAddress( advapi32_dll, "RegCloseKey" );
pRegOpenKeyEx = (void *)GetProcAddress( advapi32_dll, "RegOpenKeyExA" );
pRegQueryValueEx = (void *)GetProcAddress( advapi32_dll, "RegQueryValueExA" );
}
else goto failure;
if(pRegCloseKey && pRegOpenKeyEx && pRegQueryValueEx)
{
lRet = pRegOpenKeyEx( HKEY_LOCAL_MACHINE, "System\\CurrentControlSet\\Control\\Session Manager\\Environment", 0, KEY_READ, &hKey);
if(lRet != ERROR_SUCCESS) goto failure;
lRet = pRegQueryValueEx( hKey, "Xash3D", NULL, NULL, (byte *)szFsPath, &dwBufLen);
if(lRet != ERROR_SUCCESS) goto failure;
pRegCloseKey( hKey );
2007-10-24 22:00:00 +02:00
result = TRUE;
2007-09-13 22:00:00 +02:00
}
else goto failure;
failure:
if( advapi32_dll ) FreeLibrary( advapi32_dll ); //don't forget freeing
2007-10-24 22:00:00 +02:00
return result;
2007-06-21 22:00:00 +02:00
}
void GetLibrary ( void )
{
int i, count = 0;
if(GetEnv()) //get environment
{
2007-09-14 22:00:00 +02:00
sprintf( szSearch[count], "%s\\bin\\launch.dll", szFsPath );
2007-06-21 22:00:00 +02:00
count++;
}
if(GetReg()) //get registry
{
2007-09-14 22:00:00 +02:00
sprintf( szSearch[count], "%s\\bin\\launch.dll", szFsPath );
2007-06-21 22:00:00 +02:00
count++;
}
if(GetBin()) //get direct
{
2007-09-14 22:00:00 +02:00
strcpy( szSearch[count], "bin\\launch.dll" );
2007-06-21 22:00:00 +02:00
count++;
}
if(GetBin()) //get direct
{
2007-09-14 22:00:00 +02:00
strcpy( szSearch[count], "launch.dll" );
2007-06-21 22:00:00 +02:00
count++;
}
for(i = 0; i < count; i++) { if(hmain = LoadLibrary( szSearch[i])) break; }
}
winmain_t CreateMain32( void )
{
2007-09-13 22:00:00 +02:00
winmain_t main;
2007-06-21 22:00:00 +02:00
GetLibrary();
if (hmain)
{
main = (winmain_t)GetProcAddress( hmain, "CreateAPI" );
2007-11-14 22:00:00 +01:00
if(!main) GetError("launch.dll is corrupt" );
2007-06-21 22:00:00 +02:00
return main;
}
2007-09-14 22:00:00 +02:00
GetError("Unable to load the launch.dll" );
2007-06-21 22:00:00 +02:00
return 0; //make compiller happy
}
#endif//GETLIB_H