Test Loader for Windows HeroScript Extension Plugins

From HEWIKI
Jump to: navigation, search

He server.png Documentation on this page is intended for customers of HeroEngine managing their own server(s).

The following source code can be compiled to create a command line utility to test HeroScript Extension Interface Plugins on Windows platforms.

#include <windows.h>
 
#include <stdio.h>
#include <stdlib.h>
 
extern "C" {
  typedef void* (__cdecl getInterface)(int version);
  typedef int   (__cdecl releaseInterface)(void *);
}
 
void ErrorExit(LPSTR lpszFunction)
{
  // Retrieve the system error message for the last-error code
  LPVOID lpMsgBuf;
  DWORD dw = GetLastError();
 
  FormatMessageA(
    FORMAT_MESSAGE_ALLOCATE_BUFFER |
    FORMAT_MESSAGE_FROM_SYSTEM |
    FORMAT_MESSAGE_IGNORE_INSERTS,
    NULL,
    dw,
    MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
    (LPSTR) &lpMsgBuf,
    0, NULL );
 
  // Display the error message and exit the process
 
  fprintf(stderr, "%s failed with error %d: %s", lpszFunction, dw, lpMsgBuf);
 
  LocalFree(lpMsgBuf);
  ExitProcess(dw);
}
 
int main(int argc, char *argv[]) {
  HMODULE handle = NULL;
  void * interfaceHandle = 0;
 
  char *libname = "HSEIDemo.dll";
  char *getSymbolName = "getHSLInterface";
  char *releaseSymbolName = "releaseHSLInterface";
 
  if (argc > 1)
    libname = argv[1];
  else
    fprintf(stdout, "Pass a library name on the command line to test a library with a different name then %s.\n", libname);
 
  fprintf(stdout, "Testing HSEI plugin dynamic load library %s.\n", libname);
 
  // Open the library
  fprintf(stdout, "Opening the library %s with LoadLibraryA(\"%s\")\n", libname, libname);
  HMODULE module = LoadLibraryA(libname);
  if (!module) {
    ErrorExit("Opening the library");
  }
 
  // Find the getInterface function in the library
  fprintf(stdout, "Finding function %s in library with GetProcAddress(handle, \"%s\")\n", getSymbolName, getSymbolName);
  getInterface * getFunc = (getInterface *)GetProcAddress(module, getSymbolName);
  if (!getFunc) {
    ErrorExit("Finding the function");
  }
 
  // Find the releaseInterface function in the library
  fprintf(stdout, "Finding function %s in library with dlsym(handle, \"%s\")\n", releaseSymbolName, releaseSymbolName);
  releaseInterface * releaseFunc = (releaseInterface *)GetProcAddress(module, releaseSymbolName);
  if (!releaseFunc) {
    ErrorExit("Finding the function");
  }
 
  // Call the symbol to determine if the library can retrieve the interface
  fprintf(stdout, "Calling function %s\n", getSymbolName);
  interfaceHandle = getFunc(2);
  if ( 0 == interfaceHandle ) {
    fprintf(stderr, "Failed to get the HSEI interface with version 2\n");
    exit(1);
  }
 
  // We just wanted to get the interface, go ahead and release it before exiting
  fprintf(stdout, "Calling function %s\n", releaseSymbolName);
  if ( 1 != releaseFunc(interfaceHandle) ) {
    fprintf(stderr, "Failed to release the HSEI interface\n");
    exit(1);
  }
 
  fprintf(stdout, "All tests successful\n");
 
  FreeLibrary(handle);
 
  return 0;
}

Step-by-step instructions

  1. Open up NOTEPAD or other text editor.
  2. Select the entire C program above.
  3. Copy it onto the clipboard.
  4. Paste the program from the clipboard into the text editor.
  5. Save the file as "testLoader.cpp".
  6. Bring up a Visual Studio command prompt.
  7. Navigate to the directory containing "testLoader.cpp".
  8. Compile the program using the command-line compiler, cl:
C:\>cl testLoader.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.30729.01 for 80x86
Copyright (C) Microsoft Corporation.  All rights reserved.

testLoader.cpp
Microsoft (R) Incremental Linker Version 9.00.30729.01
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:testLoader.exe
testLoader.obj

C:\>
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox