2015-04-17 61 views
0

我有DLL函数,它有两个字符串参数和两个int参数。该函数返回一个字符串。当我直接从Excel中调用函数时,它是可以的,但是当我从VBA调用函数时,要传递给DLL函数字符串参数,这是不对应与原始(无意义字符)。并函数返回字符串,女巫有每秒char“”Excel的VBA - 字符串参数传递从Excel但不是从VBA

我的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中我调用该函数是这样的:(它是OK)

=GetPattern(I5;C1;A1;B1) 

而从VBA的调用函数是这样的:(只返回字符串的第一个字符)

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

回答

0

这可能是VB将字符串转换为ANSI C调用的问题。您可能需要明确地传递一个字符串指针:

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)) 
+0

这会得到相同的结果。 –

+0

偶然返回的字符串unicode? – Comintern