一个VC++编程疑惑的问题
本帖最后由 zyyujq 于 2022-8-18 11:16 编辑运行环境:WINDOWS
涉及工具:VS2017、VB6
编程语言:VC++、VB6
以下为主题内容:详解VC++动态链接库中的结构成员规则与调用约定,完整原文在下面链接:
https://blog.csdn.net/zyyujq/article/details/126314376
https://blog.csdn.net/zyyujq/article/details/126400139
实例使用VB6编译的程序,调用VC2017编译的DLL动态链接库:VB6里设置结构体和调用函数:Type LogicParam
Dim eBool as Boolean
Dim CMBool as Boolean
Dim NBool as Boolean
Dim EBool as Boolean
End Type
Public Declare Sub ParamInitializn Lib "Test.dll" (ByVal X As Integer, ByVal LP As LogicParam)'参数初始化
Private Sub Command1_Click()
Dim BType as LogicParam
BType.eBool = True
BType.CMBool = False
BType.NBool = True
BType.EBool = False
call ParamInitializn(2022,BType)
End Sub
//Test.h //测试头文件
struct LogicParam
{
bool eBool;
bool CMBool;
bool NBool;
bool EBool;
};
void ParamInitializn(int X, struct LogicParam PP);
//Test.cpp //测试源文件
void ParamInitializn(int X, struct LogicParam PP)
{
char buf;//定义字符串数组
const char *showFormat = " X = %d\n eBool = %d\n CBool = %d\n NBool = %d\n EBool = %d\";//定义显示格式
sprintf_s(buf, showFormat , X, PP.eBool , PP.CMBool, PP.NBool, PP.EBool);//将变量按格式赋予 buf
MessageBoxA(0, buf, "测试DLL调用参数", 0);//对话框显示
}
//Test.def
LIBRARYEXPORTS
;此处可以是显式导出
ParamInitializn @1
DLL 动态链接库无法正确获得 LogicParam 结构体值,无论怎么设置结构成员对齐值,还是使用 *LogicParam 指针参数 。
当使用如下结构体,使逻辑变量参杂在其它变量中,使结构体按其它变量字节对齐,DLL 动态链接库可以正确获取 LogicParam 结构体值。
struct LogicParam
{
int A;
bool eBool;
int B;
bool CMBool;
int C;
bool NBool;
int D;
bool EBool;
};
哈哈,完全看不懂戴拿,让大佬来看看 吧 VB默认的一般是4字节对齐
VC中设置为4字节对齐试一下
感谢分享 也有可能系统不兼容VS版本的某些函数
页:
[1]