How to dynamically load MSVCRT71.DLL - Remedy AR C-API memory problem

<< AR System API C

Description

When using another compiler that BMC used when compiling a C-API program in Windows, you will sometimes get different memory allocation library by default.

 

This can result in memory allocation problem, especially if you want to allocate some memory yourself and free it with the FreeAR-functions, or vice versa.

 

The solution is to load the same DLL as used by the ARAPI.

 

The code below shows you an example.

 

Just copy my memchk.h/memchk.c code, and call the win_load_dll()-function first thing in your main()-function.

 

Code highlighting using tohtml.com

 

 /* --- main.c --- */

/* Description: This sample program shows how to dynamically load MSVCRT71.DLL */

/*              before you call the AR API 7.6.04 */

/* Author: Misi Mladoniczky, RRR AB, http://rrr.se, 2012-03-15 */

 

#include <stdlib.h>

#include <string.h>

#include <stdio.h>

 

#include <ar.h>

#include <arextern.h>

#include <arfree.h>

 

#include "memchk.h"

 

int main(int argc, char* argv[])

{

#ifdef _WIN32

  const char *dll = "MSVCR71.DLL";

  if(!win_load_dll(dll))

    printf("Error, could not load %s\n", dll);

#endif

  ARInitialization()

  ...

}

 

/* --- memchk.h --- */

#ifndef _WIN32

 

#define m_malloc(size) malloc(size)

#define m_calloc(nitems, size) calloc(nitems, size)

#define m_realloc(ptr, size) realloc(ptr, size)

#define m_free(ptr) free(ptr)

#define m_strdup(ptr) strdup(ptr)

 

#else

 

void *win_malloc(size_t);

void *win_calloc(size_t, size_t);

void *win_realloc(void *, size_t);

void win_free(void *);

char *win_strdup(const char *);

int win_load_dll(const char *);

 

#define m_malloc(size) win_malloc(size)

#define m_calloc(nitems, size) win_calloc(nitems, size)

#define m_realloc(ptr, size) win_realloc(ptr, size)

#define m_free(ptr) win_free(ptr)

#define m_strdup(ptr) win_strdup(ptr)

 

#endif

 

/* --- memchk.c --- */

#ifndef _WIN32

 

#include "memchk.h"

 

#else

 

#include <stdlib.h>

#include <windows.h>

#include "memchk.h"

 

void *(*malloc_func)(size_t);

void *(*calloc_func)(size_t, size_t);

void *(*realloc_func)(void *, size_t);

void (*free_func)(void *);

 

int win_load_dll(const char *dll)

{

  HMODULE handle = LoadLibrary(dll);

  if(!handle)

    return(0);

  malloc_func = (void *(*)(size_t)) GetProcAddress(handle, "malloc");

  calloc_func =  (void *(*)(size_t, size_t)) GetProcAddress(handle,

"calloc");

  realloc_func =  (void *(*)(void *, size_t)) GetProcAddress(handle,

"realloc");

  free_func =  (void (*)(void *)) GetProcAddress(handle, "free");

  if(!malloc_func || !calloc_func || !realloc_func || !free_func)

    return(0);

  return(1);

}

 

void *win_malloc(size_t size)

{

  return(malloc_func(size));

}

 

void *win_calloc(size_t num, size_t size)

{

  return(calloc_func(num, size));

}

 

void *win_realloc(void *ptr, size_t size)

{

  return(realloc_func(ptr, size));

}

 

void win_free(void *ptr)

{

  free_func(ptr);

}

 

char *win_strdup(const char *ptr)

{

  int len = strlen(ptr);

  char *ret = (char *) win_malloc((len+1) * sizeof(char));

  strcpy(ret, ptr);

  return(ret);

}

 

#endif