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

    << AR System API C


    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


     /* --- 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,, 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";


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






    /* --- 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)




    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)




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

    #ifndef _WIN32


    #include "memchk.h"




    #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);



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

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


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


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

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





    void *win_malloc(size_t 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)





    char *win_strdup(const char *ptr)


      int len = strlen(ptr);

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

      strcpy(ret, ptr);