2015-04-17 109 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