質問

Visual Basic 6からA C DLLにシンプルなユーザー定義タイプ(UDT)を渡しています。それを除いて、それは正常に動作します ダブル データ型。これは0として表示されます。

C DLL:

#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <stdio.h>

typedef struct _UserDefinedType
{
    signed int      Integer;
    unsigned char   Byte;
    float           Float;
    double          Double;
} UserDefinedType;

int __stdcall Initialize ( void );
int __stdcall SetUDT ( UserDefinedType * UDT );

BOOL WINAPI DllMain ( HINSTANCE Instance, DWORD Reason, LPVOID Reserved )
{
    return TRUE;
}

int __stdcall Initialize ( void )
{
    return 1;
}

int __stdcall SetUDT ( UserDefinedType * UDT )
{
    UDT->Byte = 255;
    UDT->Double = 25;
    UDT->Float = 12345.12;
    UDT->Integer = 1;

    return 1;
}

Visual Basic 6コード:

Option Explicit

Private Type UserDefinedType
    lonInteger As Long
    bytByte As Byte
    sinFloat As Single
    dblDouble As Double
End Type

Private Declare Function Initialize Lib "C:\VBCDLL.dll" () As Long
Private Declare Function SetUDT Lib "C:\VBCDLL.dll" (ByRef UDT As UserDefinedType) As Long

Private Sub Form_Load()

    Dim lonReturn As Long, UDT As UserDefinedType

    lonReturn = SetUDT(UDT)

    Debug.Print "VBCDLL.SetUDT() = " & CStr(lonReturn)

    With UDT
        Debug.Print , "Integer:", CStr(.lonInteger)
        Debug.Print , "Byte:", CStr(.bytByte)
        Debug.Print , "Float:", CStr(.sinFloat)
        Debug.Print , "Double:", CStr(.dblDouble)
    End With

End Sub

Visual Basicからの出力:

VBCDLL.SetUDT() = 1
              Integer:      1
              Byte:         255
              Float:        12345.12
              Double:       0

ご覧のとおり、ダブルは 0, 、それがそうあるべきであるとき 25.

役に立ちましたか?

解決

VB6のUDTSは、4の倍数のアドレスにダブルを調整します。これは、cでそれに対処する方法を次に示します。

#pragma pack(push,4)
typedef struct _UserDefinedType 
{ 
    signed int      Integer; 
    unsigned char   Byte; 
    float           Float; 
    double          Double; 
} UserDefinedType;
#pragma pack(pop)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top