I've written in a C++ dll the following function to return to VBA a substring containing a certain pattern, given an input string, which contains that substring:
VARIANT _stdcall ReturnT(VARIANT FileName)
{
wstring str(FileName.bstrVal, SysStringLen(FileName.bstrVal));
wregex ToFind(L"_[tT]\\d{2,3}(_|.)");
wsmatch TStr;
regex_search(str, TStr, ToFind);
wstring TVal = TStr.str(0).erase(0, 2);
TVal.erase(TVal.end() - 1);
assert(!TVal.empty());
BSTR bs = SysAllocStringLen(TVal.data(), TVal.size());
VARIANT TValue;
TValue.vt = VT_BSTR;
TValue.bstrVal = bs;
return TValue;
}
This works flawlessly, but I suspect I am doing something more complicated than it really should be.
Notice that I'm using VARIANT
to avoid interop problems ( BSTR
as input would return kanjis, and in output would return a string with spaces between the characters)