|
URLDownloadToCacheFile is what you want
- Roman -
|
|
|
|
|
i have used that but it is working only in intranet
|
|
|
|
|
I like the MFC WININET classes.
Here's how I generally write a simple download routine:
#include <afxinet.h>
#include <memory>
#include <stdexcept>
using namespace std;
typedef auto_ptr<CHttpConnection> apCHttpConnection;
typedef auto_ptr<CHttpFile> apCHttpFile;
typedef auto_ptr<CFtpConnection> apCFtpConnection;
typedef auto_ptr<CInternetFile> apCInternetFile;
void Download(CFile& Out,CString Agent,CString URL,CString Usr,CString Pwd,const char* PostData,HANDLE hTermRequestEvent)
{
CInternetSession Session(Agent,0);
int nTries=MAXNUMTRIES_HTTP_DOWNLOAD;
CString ErrorMsg;
_RetryDL:
try
{
CString Server,Object;
INTERNET_PORT Port;
DWORD ServiceType;
apCHttpConnection apConnection(0);
try
{
VERIFY(::AfxParseURL(URL,ServiceType,Server,Object,Port) && ServiceType==AFX_INET_SERVICE_HTTP);
apConnection=apCHttpConnection(Session.GetHttpConnection(Server,Port));
}
catch(CInternetException* pe)
{
DWORD err=pe->m_dwError;
pe->Delete();
if (err!=ERROR_SUCCESS)
ErrorMsg.ReleaseBuffer(::FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,NULL,err,MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),ErrorMsg.GetBuffer(1024),1024,NULL)?-1 );
apConnection=apCHttpConnection(0);
}
if (!apConnection.get())
throw runtime_error(LPCSTR("Can't connect online: "+(ErrorMsg.IsEmpty()?"check your connection.":ErrorMsg)));
apCHttpFile apFile(apConnection->OpenRequest(PostData?CHttpConnection::HTTP_VERB_POST:CHttpConnection::HTTP_VERB_GET,Object,0,1,0,0,INTERNET_FLAG_RELOAD|INTERNET_FLAG_DONT_CACHE));
if (!apFile.get())
throw runtime_error("Unable to create HTTP request. (is memory low?)");
CString Headers="";
if (PostData)
Headers="Content-Type: application/x-www-form-urlencoded";
_ReSendRequest:
if (!Usr.IsEmpty())
VERIFY(apFile->SetOption(INTERNET_OPTION_USERNAME,(void*)(LPCSTR)Usr,Usr.GetLength()));
if (!Pwd.IsEmpty())
VERIFY(apFile->SetOption(INTERNET_OPTION_PASSWORD,(void*)(LPCSTR)Pwd,Pwd.GetLength()));
if (hTermRequestEvent && WaitForSingleObject(hTermRequestEvent,0)==WAIT_OBJECT_0)
return;
if (!apFile->SendRequest(Headers,Headers.GetLength(),(LPVOID)PostData,PostData?strlen(PostData) ))
throw runtime_error("Unable to send HTTP request. (is connection ok?)");
DWORD Status=0;
if (!apFile->QueryInfoStatusCode(Status))
{
DWORD err=::GetLastError();
if (err!=ERROR_SUCCESS)
ErrorMsg.ReleaseBuffer(::FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,NULL,err,MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),ErrorMsg.GetBuffer(1024),1024,NULL)?-1 );
throw runtime_error(ErrorMsg.IsEmpty()?"Can't query status.":LPCSTR(ErrorMsg));
}
switch (Status)
{
case HTTP_STATUS_PROXY_AUTH_REQ:
if (apFile->ErrorDlg(0,ERROR_INTERNET_INCORRECT_PASSWORD,FLAGS_ERROR_UI_FILTER_FOR_ERRORS|FLAGS_ERROR_UI_FLAGS_GENERATE_DATA|FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS)!=ERROR_INTERNET_FORCE_RETRY)
throw runtime_error("Can't connect through proxy server.");
goto _ReSendRequest;
case HTTP_STATUS_OK:
break;
case HTTP_STATUS_NOT_MODIFIED:
case HTTP_STATUS_DENIED:
default:
{
CString S;
S.Format("HTTP STATUS CODE=%u",Status);
throw runtime_error(LPCSTR(S));
}
}
DWORD ContentLength=0,D=sizeof(DWORD);
apFile->QueryInfo(HTTP_QUERY_CONTENT_LENGTH|HTTP_QUERY_FLAG_NUMBER,&ContentLength,&D);
BYTE Buf[1024];
unsigned Bytes;
Out.SeekToBegin();
while (Bytes=apFile->Read(Buf,1024))
{
if (hTermRequestEvent && WaitForSingleObject(hTermRequestEvent,0)==WAIT_OBJECT_0)
return;
Out.Write(Buf,Bytes);
// Update Progress Bar or what have you
}
}
catch(CException* e)
{
if (--nTries>0)
{
e->Delete();
goto _RetryDL;
}
throw;
}
catch(...)
{
if (--nTries>0)
goto _RetryDL;
throw;
}
}
|
|
|
|
|
|
the app code is below .i could find any problem, why it isn't work.
BOOL AppFileOperator(LPCSTR lpszSrc, LPCSTR lpszDest, int op)
{
SHFILEOPSTRUCT stFileOP;
string strsrc = lpszSrc;
string strDesc = lpszDest;
stFileOP.pFrom = strsrc.c_str();
stFileOP.pTo = strDesc.c_str();
stFileOP.wFunc = op;
stFileOP.fFlags = FOF_SILENT | FOF_NOCONFIRMATION;
printf("Start to file operator. \n src file : %s\n, desc file is %s\n",
strsrc.c_str(), strDesc.c_str());
return SHFileOperation(&stFileOP) == 0 ? TRUE : FALSE;
}
|
|
|
|
|
If you have a look in the documentation for SHFILEOPSTRUCT, you'll see that pFrom and pTo both need to be double null terminated strings, which the std::string::c_str() operator won't give you.
A couple of minor points too - you should really initialise the entire SHFILEOPSTRUCT structure, and your code isn't UNICODE safe (but I'm assuming that's not an issue for you).
So, one possible version of this code would be (N.B. code is untested):
BOOL AppFileOperator(LPCSTR lpszSrc, LPCSTR lpszDest, int op)
{
SHFILEOPSTRUCT stFileOP={0};
char* pFrom = new char[strlen(lpszSrc)+2];
char* pTo = new char[strlen(lpszDest)+2];
strncpy(pFrom, lpszSrc, strlen(lpszSrc)+2);
strncpy(pTo, lpszDest, strlen(lpszDest)+2);
stFileOP.pFrom = pFrom;
stFileOP.pTo = pTo;
stFileOP.wFunc = op;
stFileOP.fFlags = FOF_SILENT | FOF_NOCONFIRMATION;
printf("Start to file operator.\n src file : %s,\n dest file is %s\n", lpszSrc, lpszDest);
BOOL bSuccess = (SHFileOperation(&stFileOP) == 0);
delete[] pFrom;
delete[] pTo;
return bSuccess;
}
"We are the knights who say Ni" (The Knights Who Say Ni)
|
|
|
|
|
thanks, a good man. ..... i'm a lazy boy
|
|
|
|
|
hi all,
i have to insert some voice data in existing wave file
for that
1.i opend the wave file by using mmioOpen in ReadWrite mode then
2.Read all the header to conform the wave file then
3.i tried to move the cursur position to particular portion in a wave file by using mmioSeek with SET_SEEK( it is failing ) then
4.i am getting the data by using wave functions then
5.i am using mmiowrite function to write the collected data in a particular portion of existing wave file
can any one help me why the mmioseek function fails
and is there good procedure to do this
thanks in advance
anju
|
|
|
|
|
hi All
I want to display text in a window (with scroll bars). I need to trap the user mouse clicks inside the control (line number where mouse was clicked ...like that).
how can i do it...
thanx
|
|
|
|
|
easy.
inherited from CEdit (if u use) to i.e. MyEdit
change member var from CEdit to MyEdit (even u use data exchange)
handle all messages in MyEdit.
includeh10
|
|
|
|
|
hi All
I want to display text in a window (with scroll bars). I need to trap the user mouse clicks inside the control (line number where mouse was clicked ...like that).
how can i do it...
thanx
|
|
|
|
|
handle WM_LBUTTONDOWN (use the class wizard to set up the function for you). this will tell you when a click happens, and where it happened.
-c
To explain Donald Knuth's relevance to computing is like explaining Paul's relevance to the Catholic Church. He isn't God, he isn't the Son of God, but he was sent by God to explain God to the masses. /. #3848917
|
|
|
|
|
thanx chris
but can i handle this event in Edit Control?
harish
|
|
|
|
|
Can i use CSocket in thread? If so how?
i wana to use many instance of CSocket in thread in my program to send email.
i try the MSDN way of Attach() and then Detach(),but that method do not work.
help me and thank you!
|
|
|
|
|
|
to Nishant S
thanks for your advice!but i really want to know how to use CSocket with threads!;)
|
|
|
|
|
yes, you can, no problem.
Just don't use the same instance in multiple thread contexts. A CSocket or CAsyncSocket instance can only used in one thread context... compile in debug version and you will get asserts if you brake this rule.
|
|
|
|
|
Hello, the codegurus around the world.;)
Technically possible to use the thread in CSocket program, but
it might be conflicted to the process of GUI stuff.
CAsynSocket class is more flexiable than CSocket, and CAsynSocket
can deal with the socket process asyncriouly. This means that CAsynSocket
can deal with the mutiple sockets without thread.
At the same time, the timer process can check some socket statuses more
easily without thread.
Please, don't send me your email about your questions directly.
Have a nice day!
Sonork - 100.10571:vcdeveloper
-Masaaki Onishi-
|
|
|
|
|
I need a user interface for my program which is simple yet useful. The program is an editing program where there are many parameters each of which the user needs to edit. I've made a a page that descrives 3 ideas that I've come up with so far. Sorry about the many spelling mistakes. Also keep in mind that the average user will probably be 12-18 years of age so it needs to be simple. The site is [here]. Thanks.
-Raffi
|
|
|
|
|
I have a few questions?
I am a little lost on the concept of 1 and Yes, as in "It would display 1 in stead of Yes". Could you explain what you are talking about? Maybe I am dense and everyone else understands it.
Will there be multiple names (manufacturers?) that will be edited? If so, I would use a list control set up in report mode...or a grid control that is setup to edit individual cells (like a spreadsheet). It gets tedious to input lots of data for multiple names (manufacturers) using a dialog box and edit controls.
You are allowing the user to specify any arbitrary category they desire. Is this what you want? Or are there a fixed number of specific categories for the user to choose from? If so, consider using a combobox.
In general, you want to make it as easy as possible to enter the data. Tabbing between fields in a list control or grid control is easier than popping up a dialog each time new entry is to be made...In other words, pop it up once and let the user enter multiple entries.
An example of what I am talking about is a checkbook program like Quicken.
Gary Kirkham
A working Program is one that has only unobserved bugs
|
|
|
|
|
For the 1 and Yes thing, when it reads it from a file, it reads it as a 1 or a 0, which means yes or no. If it was the edit box ui then it would display 1 or 0, if it was one of the other 2 ui's it would display yes or no.
-Raffi
|
|
|
|
|
For a state that is either 1 or 0
eg True or False wouldn't a check box be the best type of control to use.
And a combo dropdown box be best for the other fields ?
Regardz
Colin J Davies
Sonork ID 100.9197:Colin
I am sick of fighting with Martin, I think I will ignore his posts from here on in, and spend the time working on articles instead.
Christian Graus
|
|
|
|
|
I'm looking for sample code on implementing Listview printing using SDK style programming.
|
|
|
|
|
I tried:
CTrain1View * pView = (CTrain1View *)GetActiveView();
pView->m_listdisplay.ShowWindow(SW_SHOW);
in a mainframe handler, where m_listdisplay is a listcontrol in the view,. Compiles fine but crashes when run....memory could not be read.
CAn you please tell me what I'm doing wrong?
Thanks very much,
ns
|
|
|
|
|
In what function are u doing so? is the control already constructed, or is it destructed after ure doing this?!!?!
Check it out!
Papa
Murex Co.
while (TRUE)
Papa.WillLove ( Bebe ) ;
|
|
|
|
|