2010-08-23 80 views
2

我将一个简单的用户定义类型(UDT)从Visual Basic 6传递给一个C DLL。能正常工作,除了数据类型,它表现为0。从Visual Basic 6调用C DLL:双重数据类型不工作

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

你可以尝试插入另一'Float'第一'Float'和'Double'之间并看看会发生什么。也许C和Visual Basic不同意如何去填充'double'。 – 2010-08-23 06:03:30

回答

3

VB6的UDT的那个是4的倍数。下面介绍如何用在C抗衡地址对齐双打:

#pragma pack(push,4) 
typedef struct _UserDefinedType 
{ 
    signed int  Integer; 
    unsigned char Byte; 
    float   Float; 
    double   Double; 
} UserDefinedType; 
#pragma pack(pop) 
+1

+1我也建议看看[Microsoft的建议](http://vb.mvps.org/tips/vb5dll.asp)编写C++ DLL从VB调用。最初与VB5一起发布,但仍与VB6相关。例如,它提到VB6预计4字节打包。 – MarkJ 2010-08-23 08:15:37

+0

它没有说我在这里有任何答案。无论如何,感谢你们两个人(Windows程序员)的代码和MarkJ的链接。 :) – 2010-08-24 06:28:21