000023791 - RKM 1.5.Client: Can you load kmclient.dll dynamically at run time instead of at load time

Document created by RSA Customer Support Employee on Jun 16, 2016Last modified by RSA Customer Support Employee on Apr 21, 2017
Version 2Show Document
  • View in full screen mode

Article Content

Article Number000023791
Applies ToKey Manager Client 1.5.x
Microsoft Windows
IssueRKM 1.5.Client: Can you load kmclient.dll dynamically at run time instead of at load time?

Is it possible to load kmclient.dll dynamically at run time (not dynamically at load time)? 

e.g. using "HINSTANCE hDll = LoadLibrary("kmclient.dll")"?

ResolutionYes, you can load kmclient.dll dynamically at run time instead of at load time.

1. Load-Time Dynamic Linking (http://msdn2.microsoft.com/en-us/library/ms684184(vs.80).aspx) is when you link with an import library (in our case it is Key Management Client\lib\win32\dll\kmclient.lib) that contains the entry points into the DLL (kmclient.dll) and put that information in your program.

2. Run-Time Dynamic Linking (http://msdn2.microsoft.com/en-us/library/ms686944(VS.80).aspx) means that you call LoadLibrary() to load the DLL and it is not necessary to link with an import library.

To confirm that you can use run-time dynamic linking for kmclient.dll, use Microsoft Visual Studio to create a Win32 application and modify the standard .cpp file that you get by adding sample code from the Web page above, plus code to call KMSInit(). Stepping through the program shows that the DLL can be loaded and the KMSInit function can be located.  For example:

#include "stdafx.h"
#include "resource.h"

//ADDING
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "KMSWrapper.h"     /* Header file for C API */
#include "KMSError.h"

#ifndef TRUE
#define TRUE 1
#endif

#ifndef FALSE
#define FALSE 0
#endif


#define SAMPLE_KMS_BUFSIZE 1024

 

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst;        // current instance
TCHAR szTitle[MAX_LOADSTRING];        // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];        // The title bar text

// Foward declarations of functions included in this code module:
ATOM    MyRegisterClass(HINSTANCE hInstance);
BOOL    InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
  // TODO: Place code here.
 MSG msg;
 HACCEL hAccelTable;

 // Initialize global strings
 LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
 LoadString(hInstance, IDC_JOE, szWindowClass, MAX_LOADSTRING);
 MyRegisterClass(hInstance);


// ADDING code from c_encrypt.c
  int           status = 0;
  void          *kmhandle = NULL;
  unsigned char encryptedData[SAMPLE_KMS_BUFSIZE] = { 0 };
  unsigned char decryptedData[SAMPLE_KMS_BUFSIZE] = { 0 };
  unsigned int  encryptedDataLen = 0;
  unsigned int  decryptedDataLen = 0;
  FILE          *fp = NULL;
  char          *configFile = NULL;
  char          *keyClass = NULL;
  char          *outFile = NULL;

// ADDING code from http://msdn2.microsoft.com/en-us/library/ms686944(VS.80).aspx
  /* A string is used for the input here, but it can be any arbitrary data. */
  char          inputData[] = "The quick brown fox jumps over the lazy dog.";
  int           inputDataLen = strlen(inputData);

//  typedef int (*MYPROC)(LPTSTR);
// Make a typedef that matches the signature for each function call
//  void    *KMSInit(char *configFile, char *password);
  typedef void * (*MY_KMS_INIT)(char *, char *);

  HINSTANCE hinstLib;
 
//    MYPROC ProcAdd;
  MY_KMS_INIT ProcAdd;

    BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;

    // Get a handle to the DLL module.
//    hinstLib = LoadLibrary(TEXT("myputs"));
    hinstLib = LoadLibrary(TEXT("kmclient.dll"));
 
    // If the handle is valid, try to get the function address.

    if (hinstLib != NULL)
    {
//        ProcAdd = (MYPROC) GetProcAddress(hinstLib, TEXT("myPuts"));
      ProcAdd = (MY_KMS_INIT) GetProcAddress(hinstLib, TEXT("KMSInit"));
 
        // If the function address is valid, call the function.
 
        if (NULL != ProcAdd)
        {
            fRunTimeLinkSuccess = TRUE;
//             (ProcAdd) (TEXT("Message via DLL function\n"));
  if ((kmhandle = (ProcAdd) (configFile, NULL)) == NULL) {
//    printf("Failed to initialize KM Client\n");
//    exit(-1);
  }

        }
 
        // Free the DLL module.
 
        fFreeResult = FreeLibrary(hinstLib);
    }

  // Perform application initialization:
 if (!InitInstance (hInstance, nCmdShow))
 {
  return FALSE;
 }

 hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_JOE);

 // Main message loop:
 while (GetMessage(&msg, NULL, 0, 0))
 {
  if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
  {
   TranslateMessage(&msg);
   DispatchMessage(&msg);
  }
 }

 return msg.wParam;
}

/*** Leave rest of file unchanged ***/

Legacy Article IDa35442

Attachments

    Outcomes