STDMETHODIMP FileStore::WriteData(BSTR fileName, BSTR data)
{
CComBSTR filePath = store_path;
filePath.Append(fileName);
HANDLE hFile = ::CreateFile(filePath, GENERIC_WRITE | GENERIC_READ, FILE_SHARE_WRITE , NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
DWORD dwBytesWritten = 0;
DWORD dwFileSize;
TCHAR* pszUnicodeBuff;
dwFileSize = wcslen(data);
pszUnicodeBuff = (TCHAR*)LocalAlloc(LPTR, (dwFileSize +1) * sizeof(TCHAR));
_tcsncpy(pszUnicodeBuff, data, dwFileSize);
::WriteFile( hFile, (LPCVOID)pszUnicodeBuff, ( dwFileSize +1) * sizeof(TCHAR), &dwBytesWritten, NULL);
::CloseHandle(hFile);
return S_OK;
}
//-------------------------------------------------
STDMETHODIMP FileStore::ReadData(BSTR fileName, BSTR* data)
{
DWORD dwBytesWritten = 0;
DWORD dwFileSize;
CHAR* pszFileBuff = 0;
TCHAR* pszUnicodeBuff;
CComBSTR filePath = store_path;
filePath.Append(fileName);
HANDLE hFile = CreateFile(filePath, GENERIC_READ | GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
dwFileSize = GetFileSize( hFile, NULL);
pszFileBuff = (char*)LocalAlloc( LPTR, dwFileSize );
ReadFile(hFile,(LPVOID)pszFileBuff, dwFileSize, &dwBytesWritten, NULL);
pszUnicodeBuff = (TCHAR*)LocalAlloc( LPTR, sizeof(TCHAR)*( dwFileSize + 1 ));
mbstowcs(pszUnicodeBuff, (const char *)pszFileBuff, (size_t)strlen(pszFileBuff) );
CloseHandle(hFile);
return SysReAllocString(data, pszUnicodeBuff);
}
|