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

153 lines
3.9 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")
2009-09-04 22:00:00 +02:00
#define Run( prog ) int main( int argc, char **argv )\
2007-12-11 22:00:00 +01:00
{ return CreateMain32()( #prog, TRUE ); }
2009-09-04 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 );
2009-09-04 22:00:00 +02:00
char szSearch[5][1024];
char szFsPath[4096];
2007-06-21 22:00:00 +02:00
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 )
{
2009-09-04 22:00:00 +02:00
// grab direct path?
2007-06-21 22:00:00 +02:00
return TRUE;
}
BOOL GetEnv( void )
{
char *pEnvPath = getenv("Xash3D");
2009-09-04 22:00:00 +02:00
if( !pEnvPath ) return FALSE;
2007-06-21 22:00:00 +02:00
2009-09-04 22:00:00 +02:00
strcpy( szFsPath, pEnvPath );
2007-06-21 22:00:00 +02:00
return TRUE;
}
BOOL GetReg( void )
{
2007-09-13 22:00:00 +02:00
HINSTANCE advapi32_dll = LoadLibrary( "advapi32.dll" );
2009-09-04 22:00:00 +02:00
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
2009-09-04 22:00:00 +02:00
if( advapi32_dll )
2007-09-13 22:00:00 +02:00
{
pRegCloseKey = (void *)GetProcAddress( advapi32_dll, "RegCloseKey" );
pRegOpenKeyEx = (void *)GetProcAddress( advapi32_dll, "RegOpenKeyExA" );
pRegQueryValueEx = (void *)GetProcAddress( advapi32_dll, "RegQueryValueExA" );
}
else goto failure;
2009-09-04 22:00:00 +02:00
if( pRegCloseKey && pRegOpenKeyEx && pRegQueryValueEx )
2007-09-13 22:00:00 +02:00
{
2009-09-04 22:00:00 +02:00
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;
2007-09-13 22:00:00 +02:00
pRegCloseKey( hKey );
2007-10-24 22:00:00 +02:00
result = TRUE;
2007-09-13 22:00:00 +02:00
}
else goto failure;
failure:
2009-09-04 22:00:00 +02:00
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
}
2009-09-04 22:00:00 +02:00
void GetLibrary( void )
2007-06-21 22:00:00 +02:00
{
2009-09-04 22:00:00 +02:00
int i, count = 0;
if( GetBin( ))
2007-06-21 22:00:00 +02:00
{
2009-09-04 22:00:00 +02:00
// assume run directory as XashDirectory
strcpy( szSearch[count], "bin\\launch.dll" );
2007-06-21 22:00:00 +02:00
count++;
}
2009-09-04 22:00:00 +02:00
if( GetBin( ))
2007-06-21 22:00:00 +02:00
{
2009-09-04 22:00:00 +02:00
// assume run directory as XashDirectory (S.T.A.L.K.E.R. style)
strcpy( szSearch[count], "launch.dll" );
2007-06-21 22:00:00 +02:00
count++;
2009-09-04 22:00:00 +02:00
}
if( GetEnv( ))
2007-06-21 22:00:00 +02:00
{
2009-09-04 22:00:00 +02:00
// get environment variable (e.g. compilers)
sprintf( szSearch[count], "%s\\bin\\launch.dll", szFsPath );
2007-06-21 22:00:00 +02:00
count++;
}
2009-09-04 22:00:00 +02:00
if( GetReg( ))
2007-06-21 22:00:00 +02:00
{
2009-09-04 22:00:00 +02:00
// get environment variable direct from registry (paranoid)
sprintf( szSearch[count], "%s\\bin\\launch.dll", szFsPath );
2007-06-21 22:00:00 +02:00
count++;
2009-09-04 22:00:00 +02:00
}
for( i = 0; i < count; i++ )
{
if( hmain = LoadLibrary( szSearch[i] ))
break; // done
}
2007-06-21 22:00:00 +02:00
}
winmain_t CreateMain32( void )
{
2007-09-13 22:00:00 +02:00
winmain_t main;
2007-06-21 22:00:00 +02:00
GetLibrary();
2009-09-04 22:00:00 +02:00
if( hmain )
2007-06-21 22:00:00 +02:00
{
main = (winmain_t)GetProcAddress( hmain, "CreateAPI" );
2009-09-04 22:00:00 +02:00
if( !main ) GetError( "launch.dll is corrupt" );
2007-06-21 22:00:00 +02:00
return main;
}
2009-09-04 22:00:00 +02:00
GetError( "Unable to load the launch.dll" );
// make compiller happy
return 0;
2007-06-21 22:00:00 +02:00
}
#endif//GETLIB_H