pointersdllmingwlabwindows

Compiler specific pointers?


I am having problems to use pointers from a NI LabWindows Application in functions from a dynamically linked DLL.

When calling specific DLL functions i use pointers to structs. The problem is, that the pointers given by the LabWindows application are pointing to a memory location 1 byte AHEAD of what the DLL expects them to point to.

So my nasty solution up to now is the following:

int MyFunction(MyStruct* struct) {
    char *ptr = (char*) struct;
    ptr--;
    struct = (MyStruct*) ptr;

    // do stuff

    ptr = (char*) struct;
    ptr++;
    struct = (MyStruct*) ptr;

    return 0;
}

My questions are: Why ??? And is there a more sophisticated solution to that?

I would expect that a as basic concept as a pointer would not differ from compiler to compiler, but maybe the one LabWindows uses is just too old.

Edit: The solution was to declare the struct the correct way for both compilers and specifiy the padding and alignment. So the correct structure definition to work with both compilers is:

#pragma pack(2)
typedef struct MyStruct{...};

Solution

  • Okay, i solved my problem! The reason for all the odd behavior was the memory managment. The alignment and padding was different! so after declaring my struct as packed

    typedef struct __attribute__(packed) MyStruct{ ...};
    

    the problem was not solved, so i tried

    typedef struct __attribute__(packed,align(1)) MyStruct{...};
    

    also wasnt the solution, therefore i added #pragma pack(1) for the LLVM CLang Compilter

    #pragma pack(1)
    typedef struct __attribute__(packed,align(1)) MyStruct{...};
    

    I dont know the reason why, but the LLVM Compiler was not going to follow the pack(1) directive, BUT the final solution was to change it all to two-byte-alignment and removed everything except the pragma:

    #pragma pack(2)
    typedef struct MyStruct{...};
    

    And now everything works like a charm :-)