c++ - DLL 函数在 VBA 环境中不起作用,但在 Excel VBA 中起作用

标签 c++ excel string vba

我在我编写的 (c++) 的 DLL 中包含以下函数,我在 Excel 中对其进行了调试,并且运行良好:

float _stdcall ReturnT(LPCSTR FileName)
{

// Extracts the generic language string from the (importing BSTR  
// would import kanji or whatever) and converts it into a wstring
wstring str = CA2T(FileName);

// Sets the string to find as _t or _T followed by 2 or 3 digits and a subsequent _ or .
wregex ToFind(L"_[tT]\\d{2,3}(_|.)");
wsmatch TStr;

regex_search(str, TStr, ToFind);    // Now the wsmatch variable contains all the info about the matching
wstring T = TStr.str(0).erase(0, 2);    // Removes the first 2 characters
T.erase(T.end() - 1);               // Removes the last character

// Checks if T is 3 digits or not (2 digits) and eventually add a "."
wstring TVal = L"";
if (T.size() == 3)
{
    TVal += T.substr(0, 2) + L"." + T.substr(2, 3);
}
else if (T.size() == 2)
{
    TVal += T;
}

// Converts T string to a float
const float TValue = (float) _wtof(TVal.c_str());

return TValue;
}

例如,如果 FileNamefoo_T024.lol,此函数会正确返回一个 float(在 C++ 中,或 Single code> 在 VBA 中),值为 2.4 。

我从 VBA(从 Excel 和其他环境)调用该函数为:

Private Declare Function ReturnT Lib "[myDLLname]" (ByVal FileName As String) As Single

如果我在其他环境中执行相同的操作并在同一个字符串上使用该函数,我会得到一个 **ERROR** 并且遗憾的是没有别的,因为我无法调试(是这个专有应用程序)。

可能是什么问题?

编辑:我发现这个其他环境实际上是 SAX , 这与 VBA 基本相同。

编辑:我设法将 Visual Studio 与应用程序链接起来,因此我可以检查导入的内容和错误的内容。 FileName 看起来正确导入,(我还使用了 VARIANT 输入方法来查看这是否是问题所在,但事实并非如此)但我在这一行收到错误:

wregex ToFind(L"_[tT]\\d{2,3}(\\_|\\.)");

错误是:

Unhandled exception at 0x75F0C54F in NSI2000.exe: Microsoft C++ exception: std::regex_error at memory location 0x0018E294.

此时它在 xthrow.cpp 处停止:

#if _HAS_EXCEPTIONS

#include <regex>

_STD_BEGIN
_CRTIMP2_PURE _NO_RETURN(__CLRCALL_PURE_OR_CDECL _Xregex_error(regex_constants::error_type _Code))
    {   // report a regex_error
    _THROW_NCEE(regex_error, _Code);
    }                                     <--- Code stops here
_STD_END
 #endif /* _HAS_EXCEPTIONS */

编辑:我的 VS 版本是 2013,平台工具集是“Visual Studio 2013 - Windows XP (v120_xp)”。我的编译器版本是:“版本 18.00.21005.1 for x64”

最佳答案

原来我导入的字符串有问题。我不明白是什么,因为当我调试程序时,它们看起来很好。我通过导入字符串的 SAFEARRAY 来解决(这需要我更改整个函数和 VBA 代码),其 BSTR 值可以按如下方式访问:

int _stdcall FilenameSort(LPSAFEARRAY* StringArray)
{
    // Fills a vector with the wstring values
    char** StrPtr = 0;
    long LowerBound = 0;  SafeArrayGetLBound(*StringArray, 1, &LowerBound);
    long UpperBound = 0;  SafeArrayGetUBound(*StringArray, 1, &UpperBound);
    const long Dimension = UpperBound - LowerBound;
    SafeArrayAccessData(*StringArray, reinterpret_cast<void**>(&StrPtr));

    BSTR element;
    vector<wstring> wstrArr;

    for (long i = 0; i <= Dimension; ++i)
    {
        SafeArrayGetElement(*StringArray, &i, &element);
        wstring ws(element, SysStringLen(element));
        wstrArr.push_back(ws);
    }

正确转换 wstring 中的所有 BSTR 后,我可以毫无问题地使用 wregex

关于c++ - DLL 函数在 VBA 环境中不起作用,但在 Excel VBA 中起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43891008/

相关文章:

java - Android:构建字符串引用

PHP-将空字符串转换为null

c++ - 如何以更少的开销管理字符串切片?

c++ - 为什么在这个函数中返回时使用std::move

c++ - 对链表打印函数的 undefined reference

c++ - 为什么我的程序在使用 makefile 编译时变慢了?

java - 如何使用 .properties 文件中的条目创建 HashMap

excel - 查找值在哪个(多个)列表中

vba - VB Clear Scripting.Dictionary 对象

Python(字符串): Using a string saved in a DataFrame cell as a pandas Formula