Home > Software engineering >  How to read memory zone in C
How to read memory zone in C

Time:08-26

I'm working on a project and I have a list of functions to use. I use the provided DLL. I have the function's prototypes in the file "list.h". This file is also provided to me

list.h

typedef unsigned short (__stdcall * ReadConfig)
                (void * * pDataOut, 
                 size_t * pSizeDataOut);

I wrote this main.c

int main (int nbArg, char** listeArg)
{

    // Initialization
    unsigned short outputFunction;
    void * pointerMemoryZone = NULL;
    size_t sizeMemoryZone = NULL;

    // Load DLL
    HMODULE dllLoadOuput= LoadLibrary("dllFile");

    // Alias Creation
    typedef unsigned short(*A_ReadConfig) (void * *, size_t *);

    // Get Pointer on Function
    A_ReadConfig ptrReadConfiguration = (A_ReadConfig)GetProcAddress(dllLoadOuput, "ReadConfig");

    // Launch of the function
    outputFunction = ptrReadConfiguration(&pointerMemoryZone, &sizeMemoryZone);

    // Display
    printf("\n\nSize Read Config : %ld\n\n", sizeMemoryZone);

    // Unload DLL
    FreeLibrary(dllLoadOuput);

    return 0;
}

This program works and I get the size of the memory area fine.

But my program, my variables are they correctly declared and used...?

And how can I read the data contained in the memory area...?

Below is a diagram provided in the documentation : enter image description here

CodePudding user response:

Presumming outputFunction indicates success, the pointer pointerMemoryZone should contain sizeMemoryZone bytes of data.

How you access the data depends on the format (e.g. text/json/xml string).

CodePudding user response:

I answer to my question. I find a solution.

I have to read datas byte by byte.

So I create a caracter pointer on "pointerMemoryZone" (wich contains adress of datas) by forcing the pointer type to character (because 1 byte) And I make a loop in which I loop through the addresses one by one (along the length of the data)

Below the code for the loop

//...
//...
//...

// Launch of the function
outputFunction = ptrReadConfiguration(&pointerMemoryZone, &sizeMemoryZone);

// Creation of Pointer
char *pointerOnPointerMemoryZone = NULL;


// Begin Loop
for (int i = 0; i < sizeMemoryZone; i  )
{
    // Association Pointer to Adress ( 0,  1,  2, etc..)
    pointerOnPointerMemoryZone = (char*)pointerMemoryZone   i;

    printf("\n%d\t\t%d\t\t%c",i, *pointerOnPointerMemoryZone, *pointerOnPointerMemoryZone);
}
// End Loop

//...
//...
//...
  • Related