KLOG.DLL C run-time usage
Previous  Top  Next

The following C program is a simple example:
·It loads KLOG.DLL at run-time. It assumes it is available in the same directory or in the system root directory  
·It gets all functions exported by KLOG.DLL,  
·It uses the functions to set parameters, and  
·Finally, it sends a message to host 'sysloghost' port 2110, and reports any errors.  

/*
  Send a syslog message using KLOG.DLL. This is the same as the command
  klog -h sysloghost -u 2110 -r myproc -p 122 –m "Local event occurred"
*/

#include <stdio.h>
#include <windows.h>

//DLL functions
FARPROC KLogInitialise;
FARPROC KLogSetPortByService;
FARPROC KLogSetPortByNum;
FARPROC KLogSetHostAddress;
FARPROC KLogSetMessagePriority;
FARPROC KLogSetMessageLevelNum;
FARPROC KLogSetMessageFacilityNum;
FARPROC KLogSetMessageLevelName;
FARPROC KLogSetMessageFacilityName;
FARPROC KLogSetProcessName;
FARPROC KLogUseTCP;
FARPROC KLogUseSilentErrors;
FARPROC KLogSetMessage;
FARPROC KLogSendMessage;
FARPROC KLogReturnError;
FARPROC KLogGetUsageHelp;
FARPROC KLogGetUsageExample;
FARPROC KLogGetSoftwareInfo;
FARPROC KLogGetPriorityNames;

void getAllFunctions(HMODULE);

int main()
{
  int  UdpPort = 2110;
  char Loghost[] = "sysloghost";
  int  Priority = 122;
  char Processname = "myproc";
  char Msg[] = "Local event occurred";

  int  Kret = 0;      // Returned error code
  char Kerror[100];   // Returned error message
  HMODULE KLib;       // DLL handle

  // Load library, hopefully in current directory.
  if ((KLib = LoadLibrary("klog.dll")) == NULL)
  {
    fprintf(stderr, "Cannot find klog.dll.\nCannot send messages.\n");
    return 1;
  }

  // Get all DLL functions
  getAllFunctions(KLib);

  // Send the message
  KLogInitialise();                         // Do defaults
  KLogSetPortByNum(UdpPort);                // Set destination port
  KLogSetHostAddress(Loghost);              // Set host
  KLogSetMessagePriority(Priority);         // Priority
  KLogSetProcessName(1, Processname);       // RFC process name
  KLogSetMessage(Msg);                      // Set message text

  // Send the message. If FALSE, then check Kret for a return code.
  // Use the DLL to get an error string. If silent errors is enabled,
  // no string is returned in Kerror.
  if (KLogSendMessage(&Kret) == FALSE)
  {
     KLogReturnError(Kerror, sizeof(Kerror));

     if (Kerror[0] != '\0')
        fprintf(stderr, "Error %d: %s\n", Kret, Kerror);

     return 1;
  }

  return 0;
}

/*
  Get all functions from the DLL.
*/

void getAllFunctions(HMODULE KLib)
{
  KLogInitialise = GetProcAddress(KLib, "KLogInitialise");
  KLogSetPortByService = GetProcAddress(KLib, "KLogSetPortByService");
  KLogSetPortByNum = GetProcAddress(KLib, "KLogSetPortByNum");
  KLogSetHostAddress = GetProcAddress(KLib, "KLogSetHostAddress");
  KLogSetMessagePriority = GetProcAddress(KLib, "KLogSetMessagePriority");
  KLogSetMessageLevelNum = GetProcAddress(KLib, "KLogSetMessageLevelNum");
  KLogSetMessageFacilityNum = GetProcAddress(KLib, "KLogSetMessageFacilityNum");
  KLogSetMessageLevelName = GetProcAddress(KLib, "KLogSetMessageLevelName");
  KLogSetMessageFacilityName = GetProcAddress(KLib, "KLogSetMessageFacilityName");
  KLogSetProcessName = GetProcAddress(KLib, "KLogSetProcessName");
  KLogUseTCP = GetProcAddress(KLib, "KLogUseTCP");
  KLogUseSilentErrors = GetProcAddress(KLib, "KLogUseSilentErrors");
  KLogSetMessage = GetProcAddress(KLib, "KLogSetMessage");
  KLogSendMessage = GetProcAddress(KLib, "KLogSendMessage");
  KLogReturnError = GetProcAddress(KLib, "KLogReturnError");
  KLogGetUsageHelp = GetProcAddress(KLib, "KLogGetUsageHelp");
  KLogGetUsageExample = GetProcAddress(KLib, "KLogGetUsageExample");
  KLogGetSoftwareInfo = GetProcAddress(KLib, "KLogGetSoftwareInfo");
  KLogGetPriorityNames = GetProcAddress(KLib, "KLogGetPriorityNames");
}