Alignment of struct members during DLL compilation in VC ++

Source: Internet
Author: User

Alignment of struct members during DLL compilation in VC ++

Now we have a requirement that we use VC ++ to compile a DLL called GT2MQ. dll, which outputs some functions for a language called GRAPHTALK (GT). In the implementation of these functions, the DLL function (adapter) of the middleware product provided by another vendor is called. dll ).

GT has requirements for calling DLL functions. The most important thing is to specify that the structure members are aligned by 1 byte during compilation, otherwise, the GT operation is invalid when calling the DLL function.

When a method in GT2MQ calls a method of adapter. dll, You need to input a struct pointer as a parameter. The definition of this struct is as follows:
/* Message structure */
Typedef struct BusMessage
{
Char messageId [50];
Char correlId [50];
Char appMessageId [30];
Char version [30];
Char bodyType [20];
Char bodyCategory [20];
Char timeStampCreated [30];
Char timeStampExpired [30];
Char srcLogicalId [30];
Char dstLogicalId [30];
Char authenticationId [30];
Char commandMode [30];
Char txnScope [30];
Char * standardBody;
Char * body;
Char priority [20];
Char persistence [10];
Char expiry [10];
Char traceLevel [10];
Char publish [10];
Char backup [10];
Char messageName [2, 384];
Char encoding [30];
Char msgCharset [30];

Long msglen;/* message sending length */
Long rcvlen;/* memory required to receive messages */
 
} BusMessage, * ptrBusMessage;

When our DLL function creates this struct, clears it, and fills in the required fields, it uses the structure pointer as a parameter to call the adapter. a function in the dll. However, this method fails to be called in any case. the returned error indicates that the msglen field is not filled. but that field is clearly filled with values.

Because adapter. dll has no source code, so we have to debug it through the assembly code to find this struct in our dll and in adapter. when the dll addresses different fields, our DLL addresses msglen with msg + 932, while adapter. dll addressing the same field is msg + 936!

After manual calculation, it is determined that the Offset Value of the msglen member should be 932. However, if the same struct definition is placed in a new win32 project, view the assembly code, it is found that the address is 936.

Later, I went further and found that the problem lies in the addressing of the following two fields: standardBody and msglen. The two fields moved two bytes backward at the addressing, resulting in the offset of a total of four bytes, this causes an error when the members access data.

We suspect that our GT2MQ. the structure member alignment option in the dll project is set to "1 byte alignment" (/Zp1), but no matter how much we set this parameter, this problem will not be eliminated: our DLL never adds two byte offsets for those two fields. what's more, our project requires GT2MQ. the/Zp1 option must be specified for the dll, And the/Zp1 option cannot meet the requirements even if it is solved.

Our current solution is to add a field before standardBody and msglen In the struct: char Reserved1 [2] and char Reserved2 [2] are used to force our DLL to add a value for later member addressing. however, it is not appropriate to do this. We have to modify the header file provided by the vendor, but the same header file won't be faulty in other projects.

Please kindly advise if you are looking for a better solution or if you know how to adjust the project settings!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.