DLL函数不能在VBA环境中工作,但在Excel VBA中工作

我有以下function,包含在我写的(c + +),我在Excel中debugging的DLL,工作得很好:

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; } 

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

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

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

如果我从其他环境做同样的事情,并使用相同的string函数,我得到一个**ERROR** ,可悲的是没有别的,因为我不能debugging(这是一个专有的应用程序)。

可能是什么问题呢?

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

编辑:我设法链接Visual Studio的应用程序,所以我可以检查什么是导入和什么是错的。 FileName看起来是正确的导入,(我也使用VARIANT方法,看看是否是这个问题,这是不是),但我收到这样一个错误:

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

错误是:

NSI2000.exe中的0x75F0C54F处未处理的exception:Microsoft C ++exception:内存位置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 */ 

编辑:我的VS版本是2013年,platformtoolset是“Visual Studio 2013 – Windows XP(v120_xp)”。 我的编译器版本是:“版本18.00.21005.1 for x64”

原来我input的string出错了。 我不明白是什么,因为当我debugging程序,他们看起来很好。 我通过导入一个string的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); } 

把所有的BSTR正确地转换成wstring s后,我可以wregex的使用wregex