大家帮我看一下这段有关文件操作和内存分配代码问题出在什么地方?执行后会提示"存取未命名档案遭拒绝"错误.void myfunction(LPCTSTR strFileName)
{
CFile file;
HGLOBAL hGlobal=NULL; if(!file.Open(strFileName,CFile::modeRead||CFile::typeBinary))
return NULL;
DWORD dwSize=file.GetLength();
hGlobal=::GlobalAlloc(GHND,dwSize);
ASSERT(hGlobal!=NULL);
LPVOID pData=::GlobalLock(hGlobal);
ASSERT(pData!=NULL);
//read file and store in the global memorry;
DWORD dwReadSize=file.ReadHuge(pData,dwSize);
ASSERT(dwReadSize==dwSize);
file.Close();
::GlobalUnlock(hGlobal);
.......
.....
}

解决方案 »

  1.   

    CString Namel("d:\\src.txt");
        CFile MyFilel(Namel,CFile::modeRead);
    DWORD dwSize=MyFilel.GetLength();
    void far *p=::GlobalAlloc(0,dwSize);
    if(p==NULL)
    AfxMessageBox("Alloc memory error");
    MyFilel.ReadHuge(p,dwSize);
    MyFilel.Close();
      

  2.   

    if(!file.Open(strFileName,CFile::modeRead||CFile::typeBinary))应该为
    if(!file.Open(strFileName,CFile::modeRead|CFile::typeBinary))
    return NULL;
      

  3.   

    to Phourm:love u for ever!