简体   繁体   English

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

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

I have DLL function which has two string parameters and two int parameters. 我有DLL函数,它具有两个字符串参数和两个int参数。 The function returns a string. 该函数返回一个字符串。 When I call the function directly from excel, it is OK, but when I call the function from VBA, to DLL function is pass string params , which is not corresponding with original (nonsense characters). 当我直接从excel调用该函数时,可以,但是当我从VBA调用该函数时,给DLL函数的是传递字符串参数 ,该参数与原始字符(废话字符) 不对应 And function return string , witch have every seconds char " " . 并且函数返回字符串每隔一秒钟就变成字符“”

My dll function looks like this: 我的dll函数如下所示:

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

Declare function in VBA: 在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

In excel I call the function like this: (And it is OK) 在excel中,我这样调用该函数:(并且可以)

=GetPattern(I5;C1;A1;B1)

And from VBA call function like this: (and it return only first char of string) 从VBA调用函数中这样:(并且它仅返回字符串的第一个char)

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

This could be an issue with VB converting strings to ANSI on C calls. VB在C调用中将字符串转换为ANSI时可能会出现问题。 You may need to explicitly pass a string pointer: 您可能需要显式传递一个字符串指针:

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