DLL函数无法在VBA环境中工作,但在Excel VBA中工作

Nol*_*884 9 c++ string excel vba

我在Excel中编写的DLL(c ++)中包含以下函数,我在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;
}
Run Code Online (Sandbox Code Playgroud)

FileName例如foo_T024.lol,如果此函数正确返回值为2.4 float(在C++中或Single在VBA中).

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

Private Declare Function ReturnT Lib "[myDLLname]" (ByVal FileName As String) As Single
Run Code Online (Sandbox Code Playgroud)

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

可能是什么问题呢?

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

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

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

错误是:

NSI2000.exe中0x75F0C54F处的未处理异常:Microsoft C++异常:内存位置0x0018E294处的std :: regex_error.

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 */
Run Code Online (Sandbox Code Playgroud)

编辑:我的VS版本是2013年,platformtoolset是"Visual Studio 2013 - Windows XP(v120_xp)".我的编译器版本是:"版本18.00.21005.1 for x64"

Nol*_*884 2

事实证明我导入的字符串有问题。我不明白什么,因为当我调试程序时,它们看起来很好。我通过导入一个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);
    }
Run Code Online (Sandbox Code Playgroud)

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