Calling a VB6 DLL function with a complex User Defined Type (UDT) from C#

佐手、 提交于 2019-11-29 10:01:22

What does the struct look like? It's been a while since I did any serious VB6 development, but one of the things I remember tripping me up sometimes when calling between languages was VB6's insistence on dword-aligning all of it's structures. So for example if you have some byte values mixed in the middle, it will insert padding so all the values align on an even 4 byte boundary. Consider the following:

Type MyType
    A As Long
    B As Byte
    C As Long
End Type

In memory, there will be 3 bytes of unused space between B and C. Of course if C# is not performing the same padding, it can throw your values off and cause all sorts of chaos.

With some compilers (such as C) it is possible to set a compiler switch to use this type of alignment. I don't know if C# has anything similar. If not, the solution is to insert some dummy fields of the appropriate size in to your struct on the C# size.

Here's an article which provides more information about how VB6 aligns UDTs: http://www.developerfusion.com/article/3367/copymemory-and-arrays-proper-use/4/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!