Uvazhaemie Gospoda!
Pomogite razobrat'sya s problemoi:
Ya sozdau s pomosh'u ATL elementarnij COMponent s dvumya funkciyami:
[id(1), helpstring("method ReadFromFile")] HRESULT ReadFromFile([in] BSTR fName, [out, retval] BSTR *txtRet);
[id(2), helpstring("method Save2File")] HRESULT Save2File([in] BSTR fName, [in] BSTR txt2save);
V funkcii ReadFromFile ya prosto chitau text iz faila i vozvrashau ego.
I poluchau — error : Memory couldn't be 'written'
hotya tot zhe kod v obichnoi programme (ne v COM) rabotaet bezukoriznenno, edinstvennoe otlichie preobrazovanie iz char* v BSTR.
Esli uvazhaemie specialisti smogut vzglyanut' na privedennii nizhe code i ukazat' mne na oshibku budu priznatelen :-).
STDMETHODIMP CFIO::ReadFromFile(BSTR fName, BSTR *txtRet)
{
// TODO: Add your implementation code here
USES_CONVERSION;
_bstr_t bstrFName(fName,FALSE);
HANDLE hFile;
DWORD dwFileSize;
DWORD dwBytesRead;
BOOL bResult;
char *pBuf;
hFile = CreateFile(bstrFName,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
CloseHandle(hFile);
return E_FAIL;
}
dwFileSize = GetFileSize(hFile,NULL);
dwBytesRead = 0;
if (dwFileSize == INVALID_FILE_SIZE)
{
CloseHandle(hFile);
return E_FAIL;
}
pBuf = new char[dwFileSize];
bResult = ReadFile(hFile,pBuf,dwFileSize,&dwBytesRead,NULL);
if (bResult==0)
{
CloseHandle(hFile);
return E_FAIL;
}
*txtRet = ::SysAllocString(A2OLE(pBuf));
// delete pBuf;
CloseHandle(hFile);
return S_OK;
}
P.S.
proshu prosheniya za translit, poka u menya net drugoi vozmozhnosti beglo pisat' po russki
Alexander N. Treyner