我正在创建一个 COM dll,可以从 PHP 中使用它来读取我已经知道大小的内存映射文件,虽然读取文件没有问题,但我无法将它作为 BSTR 正确返回。当我使用 dll 时,它只返回空字符之前的字符(在这种情况下为 3 个字符),我知道文件可以包含多个空字符,这就是为什么我在 MultiByteToWideChar 函数中指定了大小,但它仍然不起作用.
STDMETHODIMP CMemReaderImpl::ReadFile(BSTR* filepath, BSTR* Ofile)
{
if (*filepath == nullptr) {
*Ofile = _com_util::ConvertStringToBSTR("err");
}
std::wstring wpath(*filepath, SysStringLen(*filepath));
LPCWSTR lpath = wpath.c_str();
HANDLE hFileMap;
PCHAR lpBuffer = NULL;
hFileMap = OpenFileMapping(
FILE_MAP_ALL_ACCESS,
FALSE,
lpath
);
if (hFileMap == NULL) {
char* err = "ERROR";
*Ofile = _com_util::ConvertStringToBSTR(err);
}
lpBuffer = (PCHAR)MapViewOfFile(
hFileMap,
FILE_MAP_ALL_ACCESS,
0,
0,
BUFF_SIZE
);
if (lpBuffer == NULL) {
char* err = "ERROR";
*Ofile = _com_util::ConvertStringToBSTR(err);
}
//where the magic happens
int wslen = MultiByteToWideChar(CP_ACP, 0, lpBuffer, 1000, 0, 0);
BSTR bstr = SysAllocStringLen(0, wslen);
MultiByteToWideChar(CP_ACP, 0, lpBuffer, 1000, bstr, wslen);
*Ofile = bstr;
UnmapViewOfFile(lpBuffer);
CloseHandle(hFileMap);
return S_OK;
}
我真的希望将整个文件作为 BSTR* 返回,以便它可以由另一个 php 程序操作,但到目前为止似乎没有任何效果。