【发布时间】:2010-08-23 05:41:32
【问题描述】:
我将一个简单的用户定义类型 (UDT) 从 Visual Basic 6 传递到一个 C DLL。它工作正常,除了 double 数据类型显示为 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。
【问题讨论】:
-
您可以尝试在第一个
Float和Double之间插入另一个Float,看看会发生什么。也许 C 和 Visual Basic 不同意如何填充double。
标签: c dll vb6 double user-defined-types