今天开发遇到一个问题。
URLDownloadToFile 下载的URL文件只能通过ShellExecute打开一次,
再次下载并改名(不改名也不行,改名是为了说明不是下载问题)调用ShellExecute会返回错误如下:
SE_ERR_OOM |
There was not enough memory to complete the operation |
只要在一个进程中调用就会出现这个问题,其实与内存无关。
主要代码如下:
static int s_iIndex = 0;
#define BUFSIZE 512#include <urlmon.h>
#pragma comment(lib, "urlmon.lib")#include <Wininet.h>
#pragma comment(lib, "Wininet.lib")char* OpenUrlFileEx( char* _cstrUrl )
{DWORD dwRetVal = 0;
UINT uRetVal = 0;char lpPathBuffer[BUFSIZE] = {0};
DWORD dwBufSize=BUFSIZE;
// 获取临时文件路径
dwRetVal = GetTempPath(dwBufSize, // length of the buffer
lpPathBuffer); // buffer for path
if (dwRetVal > dwBufSize || (dwRetVal == 0))
{
printf ("dfb GetTempPath failed with error %d.\n", GetLastError());
return NULL;
}
char *cFileName = strrchr(_cstrUrl, '\\');if (cFileName == NULL)
{
cFileName = strrchr(_cstrUrl, '/');
cFileName = cFileName+1;
}
if (cFileName != NULL)
{char cBuf[BUFSIZE] = {0};
sprintf(cBuf,"%sServerTmp%d_%s",lpPathBuffer,s_iIndex++,cFileName);
if (S_OK == URLDownloadToFile(NULL, _cstrUrl, cBuf, NULL,NULL) )
{
return cBuf;
}
else
{
//失败
printf("dfb URLDownloadToFile error %d. \n",
GetLastError());
}
}
else
{
printf("dfb URLDownloadToFile error can't find file.\n");
}
return "";
}
void CtestdoDlg::OnBnClickedButton1()
{
char *pFile = OpenUrlFileEx("http://10.3.241.71:8088/cloud/111.doc");HINSTANCE hInstance = NULL;
hInstance = ShellExecute(NULL, "open", pFile, NULL, NULL, SW_SHOW);
if((int)hInstance > 31)
{
CloseHandle(hInstance);}
else
{
TRACE("ERROR %d",hInstance);
}
}
最后解决办法是将下载与打开放在两个进程中解决了这个问题。