繁体   English   中英

Excel VBA-从Excel而不是从VBA传递字符串参数

[英]Excel VBA - string param pass from excel but not from VBA

我有DLL函数,它具有两个字符串参数和两个int参数。 该函数返回一个字符串。 当我直接从excel调用该函数时,可以,但是当我从VBA调用该函数时,给DLL函数的是传递字符串参数 ,该参数与原始字符(废话字符) 不对应 并且函数返回字符串每隔一秒钟就变成字符“”

我的dll函数如下所示:

BSTR __stdcall getPattern(int sex, int pad, BSTR * a, BSTR * b){
    ...
}

在VBA中声明功能:

Declare Function GetPattern _
Lib "myPathToFunction" Alias "GetPattern" (ByVal poh As Integer, ByVal pad As Integer, ByRef a As String, ByRef b As String) As String

在excel中,我这样调用该函数:(并且可以)

=GetPattern(I5;C1;A1;B1)

从VBA调用函数中这样:(并且它仅返回字符串的第一个char)

result = GetPattern(Range("I5").Value, Range("C1").Value, Cells(i, 1).Value, Cells(i, 2).Value)

VB在C调用中将字符串转换为ANSI时可能会出现问题。 您可能需要显式传递一个字符串指针:

Dim param_a As String
Dim param_b As String

param_a = Cells(i, 1).Value
param_b = Cells(i, 2).Value
result = GetPattern(Range("I5").Value, Range("C1").Value, _
                    StrPtr(param_a), StrPtr(param_b))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM