FS2_Open
Open source remastering of the Freespace 2 engine
externalcode.h
Go to the documentation of this file.
1 #ifndef EXTERNALCODE_H_INCLUDED_
2 #define EXTERNALCODE_H_INCLUDED_
3 
4 #include "globalincs/pstypes.h"
5 
6 /* We must have windows.h anywhere SCP_ExternalCode is used under windows */
7 #ifdef _WIN32
8 #include <windows.h>
9 #endif
10 
11 /* This class loads external libraries for FSO use.
12  * Different platforms have different ways of doing this, so use your ifdefs!
13  */
15 {
16 public:
18 #ifdef _WIN32
19  : m_dll( NULL )
20 #endif
21  {
22  }
23 
24  virtual ~SCP_ExternalCode( )
25  {
26 #ifdef _WIN32
27  if ( m_dll )
28  ::FreeLibrary( m_dll );
29 #endif
30  }
31 
32 protected:
33  BOOL LoadExternal( const char* externlib )
34  {
35  if ( !externlib )
36  return FALSE;
37 
38 #ifdef _WIN32
39  m_dll = ::LoadLibrary( externlib );
40 
41  if ( m_dll )
42  return TRUE;
43 #endif
44 
45  return FALSE;
46  }
47 
48  void* LoadFunction( const char* functionname )
49  {
50 #ifdef _WIN32
51  if ( m_dll != NULL && functionname != NULL )
52  return ::GetProcAddress( m_dll, functionname );
53 #endif
54  return NULL;
55  }
56 private:
57 #ifdef _WIN32
58  HMODULE m_dll;
59 #endif
60 };
61 
62 /* These are available if you're compiling an external DLL
63  * So far we have trackir, and speech is on the way
64  */
65 #ifdef _WIN32
66 # define SCP_EXT_CALLCONV __cdecl
67 # ifdef SCPDLL_EXTERNAL_LIB
68 # define SCPDLL_EXTERNAL __declspec( dllexport )
69 # else
70 # define SCPDLL_EXTERNAL __declspec( dllimport )
71 # endif
72 /* Must be in a CPP file in your DLL code
73  * If you want to write your own, you shouldn't.
74  */
75 # define SCPDLL_DLLMAIN( ) \
76  SCP_EXTERN_C BOOL APIENTRY DllMain( HANDLE, DWORD, LPVOID ) { return TRUE; }
77 #else
78 # define SCP_EXT_CALLCONV
79 # define SCPDLL_EXTERNAL
80 # define SCPDLL_DLLMAIN( )
81 #endif
82 
83 #ifdef __cplusplus
84 # define SCP_EXTERN_C extern "C"
85 #else
86 # define SCP_EXTERN_C
87 #endif
88 
89 /* Version information */
90 typedef struct _SCPDLL_Version
91 {
92  int major;
93  int minor;
94  int patch;
96 
97 typedef int ( SCP_EXT_CALLCONV *SCPDLL_PFVERSION )( SCPDLL_Version* );
98 
99 /* Must be in a CPP file in your DLL code */
100 #define SCPDLL_VERSION_FUNCTION( Major, Minor, Patch ) \
101  SCP_EXTERN_C int SCPDLL_EXTERNAL SCPDLL_GetVersion( SCPDLL_Version* v ) { \
102  if ( !v ) return -1;\
103  v->major = Major; v->minor = Minor; v->patch = Patch;\
104  return 0; }
105 
106 #endif /* EXTERNALCODE_H_INCLUDED_ */
struct _SCPDLL_Version SCPDLL_Version
#define TRUE
Definition: pstypes.h:399
typedef int(SCP_EXT_CALLCONV *SCPDLL_PFVERSION)(SCPDLL_Version *)
BOOL LoadExternal(const char *externlib)
Definition: externalcode.h:33
void * LoadFunction(const char *functionname)
Definition: externalcode.h:48
int BOOL
Definition: config.h:80
virtual ~SCP_ExternalCode()
Definition: externalcode.h:24
#define FALSE
Definition: pstypes.h:400
#define SCP_EXT_CALLCONV
Definition: externalcode.h:78