[英]How to pass a char parameter to a C DLL from a VB6 application?
I am trying to call a C function in my VB6 application. 我试图在我的VB6应用程序中调用C函数。 The problem is that that function takes simple
char
as a parameter. 问题是该函数将简单的
char
作为参数。 Not char
array pointer ( char *
) but regular char
. 不是
char
数组指针( char *
)而是常规char
。
This is how it is declared: 这是它的声明方式:
#define FM_API extern "C" int _stdcall
FM_API NVF_SetGroupFormat(char *aMarker, char aDecSeparator, int aDigits)
It returns -1 if execution was successful and 0 if not. 如果执行成功则返回-1,否则返回0。
At the very beginning I did what I always do - just declared it and that is it. 在一开始我做了我一直做的事情 - 只是宣布它就是这样。
This was my declaration: 这是我的声明:
Declare Function SetGroupFormat Lib "C:\Libs\FM_API.dll" Alias "NVF_SetGroupFormat" (ByVal lMarker As String, ByVal lDecSeparator As String, ByVal lDigits As Integer) As Integer
But it always returned 0 (fail/false). 但它总是返回0(失败/错误)。
I thought that is something wrong with parameters I passed. 我认为我传递的参数有问题。 I called dll author for advice and told him how I pass my parameters.
我打电话给dll作者寻求建议并告诉他我如何传递我的参数。 This is a call of that function:
这是该函数的调用:
Dim lSt As Integer
lSt = SetGroupFormat("=;\n", ".", 0)
Author told me that this call is correct but he told me that my declaration is "probably" wrong, because I declared lDecSeparator As String, but it should be char. 作者告诉我,这个调用是正确的,但他告诉我,我的声明“可能”错了,因为我声明lDecSeparator为String,但它应该是char。
Main problem is that char does not exist in VB6. 主要问题是VB6中不存在char。
How do I declare and pass char to ac based Dll from a VB6 code? 如何从VB6代码声明并将char传递给基于交流的Dll?
In VB6 you can use fixed-length strings for this purpose: 在VB6中,您可以使用固定长度的字符串来实现此目的:
Declare Function SetGroupFormat Lib "C:\Libs\FM_API.dll" Alias "NVF_SetGroupFormat" ( _
ByVal lMarker As String, _
ByVal lDecSeparator As String * 1, _
ByVal lDigits As Integer _
) As Integer
Admittedly it's been a while since I used VB6, and my knowledge is a bit rusty; 不可否认,自从我使用VB6以来已经有一段时间了,而且我的知识有点生疏; if the above syntax isn't supported, then the following will definitely work:
如果不支持上述语法,那么以下内容肯定会有效:
Type SingleChar
Value As String * 1
End Type
Declare Function SetGroupFormat Lib "C:\Libs\FM_API.dll" Alias "NVF_SetGroupFormat" ( _
ByVal lMarker As String, _
ByVal lDecSeparator As SingleChar, _
ByVal lDigits As Integer _
) As Integer
Alternatively you can also use the data type Byte
to represent a single 8-bit value, equivalent to char
in the external API. 或者,您也可以使用数据类型
Byte
来表示单个8位值,相当于外部API中的char
。 If the call then fails, this is due to something else, unrelated to the parameter type. 如果调用失败,则这是由于与参数类型无关的其他因素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.