|
 Hi Randor,
I am getting http response code = 407 while using proxy. I know it is for proxy authentication. I wrote the following code. Can you tell me where is my mistake.
std::string responseData("");
DWORD statusCode;
CInternetSession iSession = CInternetSession(L"My Application",1,INTERNET_OPEN_TYPE_PROXY, L"http://proxy.mycompany.com:8080");
CHttpConnection *httpConnection = NULL;
CHttpFile *httpFile = NULL;
char userName[] = "myUsername", password[] = "myPassword";
try
{
iSession.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, (int)1000000000);
iSession.SetOption(INTERNET_OPTION_CONTROL_SEND_TIMEOUT, (int)1000000000);
iSession.SetOption(INTERNET_OPTION_CONTROL_RECEIVE_TIMEOUT, (int)1000000000);
iSession.SetOption(INTERNET_OPTION_DATA_SEND_TIMEOUT, (int)1000000000);
iSession.SetOption(INTERNET_OPTION_DATA_RECEIVE_TIMEOUT, (int)1000000000);
iSession.SetOption(INTERNET_OPTION_RECEIVE_TIMEOUT, (int)1000000000);
iSession.SetOption(INTERNET_OPTION_SEND_TIMEOUT, (int)1000000000);
iSession.SetOption(INTERNET_OPTION_PROXY_USERNAME, userName, strlen(userName) + 1);
iSession.SetOption(INTERNET_OPTION_PROXY_PASSWORD, password, strlen(password) + 1);
httpConnection = iSession.GetHttpConnection(CString("subdomain.maindomain.com"), (INTERNET_PORT) 443);
httpFile = httpConnection->OpenRequest(L"POST", CString("/somepage/"), 0, 1, 0, 0, INTERNET_FLAG_SECURE);
httpConnection->SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, (int)1000000000);
CString strHeader;
int contentLength =0;
strHeader = "Content-Type: application/x-amf";
httpFile->AddRequestHeaders(strHeader);
strHeader.Format(L"Content-Length: %d", contentLength);
httpFile->AddRequestHeaders(strHeader);
BOOL x = httpFile->SendRequest(NULL, 0, NULL, contentLength);
httpFile->QueryInfoStatusCode(statusCode);
if(statusCode != 200)
{
responseData = "";
}
char fileData[1024];
int len;
while((len = httpFile->Read(fileData, 1024)) > 0)
responseData.append(fileData, len);
}
catch(CException *ex)
{
WCHAR str[500];
DWORD errorCode = GetLastError();
ex->GetErrorMessage(str, GetLastError());
}
I am using AMF so I specified 443 port while my proxy is using 8080 port.
|
|
|
|
|
I want to log some information before the application crashes. I find the function SetUnhandledExceptionFilter() in MSDN, which enables an application to supersede the top-level exception handler of each thread and process. I'm not sure whether it can catch all exception in a MFC application or not.
I also found some articles about SetUnhandledExceptionFilter in VS2005. It seems that we need to disable the SetUnhandledExceptionFilter function called by CRT. The code is as follows:
void DisableSetUnhandledExceptionFilter()
{
void *addr = (void*)GetProcAddress(LoadLibrary(_T("kernel32.dll")),
"SetUnhandledExceptionFilter");
if (addr)
{
unsigned char code[16];
int size = 0;
code[size++] = 0x33;
code[size++] = 0xC0;
code[size++] = 0xC2;
code[size++] = 0x04;
code[size++] = 0x00;
DWORD dwOldFlag, dwTempFlag;
VirtualProtect(addr, size, PAGE_READWRITE, &dwOldFlag);
WriteProcessMemory(GetCurrentProcess(), addr, code, size, NULL);
VirtualProtect(addr, size, dwOldFlag, &dwTempFlag);
}
}
I don't know what's the meaning of "0x33, 0xC0, 0xC2, 0x04, 0x00". Who does understand the code?
In addition, when I debug the code to catch exception, a exception dialog will ask me "Abort, Retry or Ignore". If I select "Abort" or "Ignore', my defined exception funciton will not call. Instead I want it will be called in any case.
So is there some solution for my requirement? Thanks!
|
|
|
|
|
jtasph wrote: I don't know what's the meaning of "0x33, 0xC0, 0xC2, 0x04, 0x00". Who does understand the code?
An assembly programmer might recognize those bytes as:
__asm
{
xor eax,eax
ret 0x4
}
But unfortunately its hard to find one these days. They are like the dinosaurs and are nearly extinct.
Best Wishes,
-David Delaune
|
|
|
|
|
Thanks Randor for your explanation. Do you know what's the meaning of these assembly codes and how the codes to disable the SetUnhandledExceptionFilter function called by CRT? Do these assembly codes depend on platform? Thanks!
|
|
|
|
|
jtasph wrote: Do you know what's the meaning of these assembly codes and how the codes to disable the SetUnhandledExceptionFilter function called by CRT?
Yes, it overwrites the instructions in memory with new instructions. This causes SetUnhandledExceptionFilter to immediately return.
jtasph wrote: Do these assembly codes depend on platform?
Yes, it is 32 bit assembly instructions.
Best Wishes,
-David Delaune
|
|
|
|
|
Randor wrote: They are like the dinosaurs and are nearly extinct.
I thought the dinosaurs *are* extinct.
“Follow your bliss.” – Joseph Campbell
|
|
|
|
|
Some of us dinosaurs have survived. We are like the Coelacanth[^].
Best Wishes,
-David Delaune
|
|
|
|
|
Rajesh R Subramanian wrote: I thought the dinosaurs *are* extinct.
You're actually wrong. There's one of them, for instance, they call Norton-Antivirusex that grabs all the system resources making more damages the viruses could ever do. There is the Eclipsosaurus too, the name comes because it eclipses all your machine power.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
CPallini wrote: Norton
Excuse me? We aren't talking about pigs, are we?
“Follow your bliss.” – Joseph Campbell
|
|
|
|
|
Yes pigs on the wings! Or maybe the great pig in the sky...
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
jtasph wrote: I don't know what's the meaning of "0x33, 0xC0, 0xC2, 0x04, 0x00".
As David said, it is hex code for "return 0;" . So after you do that WriteProcessMemory (), the SetUnhandledExceptionFilter () will not work any maore it will simply reurn 0. Not only for CRT calls for you also( I am not sure when CRT calls this function. If it has called the SetUnhandledExceptionFilter() before you disable it, the CRT exception handler will work ).
However I suspect the necessity of supressing the CRT exception trough the above method. I guess the you can use the _set_abort_behavior () to control the exception handler of CRT.
More over there is AddVectoredExceptionHandler () function which will be executed before the exception handler specified in SetUnhandledExceptionFilter ().
|
|
|
|
|
Naveen wrote: However I suspect the necessity of supressing the CRT exception trough the above method. I guess the you can use the _set_abort_behavior() to control the exception handler of CRT.
I found out some introdution about exception handling mechanism in VS2005.
There're 3 kinds of checks in CRT code that pass around and invoke Dr.Watson directly.
1.One is when we have detected a buffer overrun (/GS failure). For security reasons in this case, we directly call the UnhandledExceptionFilter.
2.Another is when we detect an invalid parameter before a bad situation occurs.
3.A third are other abnormal terminations (purecall, unexpected, etc). These generally have their own handlers that you can set (_set_purecall, _set_terminate, _set_unexpected). These exist only in cases where we consider it safe to do so.
_set_abort_behavior, signal(SIGABRT, ...), and _set_invalid_parameter_handler(...) functions allow to intercept in case 2 and case 3. But for case 1, we can set api hook.
Is it right?
|
|
|
|
|
jtasph wrote: functions allow to intercept in case 2 and case 3. But for case 1, we can set api hook.
Hmm..Seems there is no way to control the first one. Did you check the AddVectoredExceptionHandler()? With that i guess there is no need to block any other SEH filters. Here [^]is an excellent article about vectored exception handing written by Matt Pietrek.
|
|
|
|
|
jtasph wrote: In addition, when I debug the code to catch exception, a exception dialog will ask me "Abort, Retry or Ignore". If I select "Abort" or "Ignore', my defined exception funciton will not call.
Add the following code to your project and tell me if your exception handler works:
SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
Best Wishes,
-David Delaune
|
|
|
|
|
I added "SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);" but it does not work.
|
|
|
|
|
Thanks for all answerers. Here is my test code:
LONG WINAPI MyUnFilterA (struct _EXCEPTION_POINTERS *lpExceptionInfo)
{
AfxMessageBox(_T("11"));
return EXCEPTION_CONTINUE_SEARCH;
}
1.
void CExceptionDlg::OnBnClickedOk()
{
SetUnhandledExceptionFilter(MyUnFilterA);
DisableSetUnhandledExceptionFilter();
*(int *) 0 = 0;
}
2.
void CExceptionDlg::OnBnClickedOk()
{
SetUnhandledExceptionFilter(MyUnFilterA);
DisableSetUnhandledExceptionFilter();
abort();
}
3.
void CExceptionDlg::OnBnClickedOk()
{
AddVectoredExceptionHandler(1, MyUnFilterA);
*(int *) 0 = 0;
}
4.
void CExceptionDlg::OnBnClickedOk()
{
AddVectoredExceptionHandler(1, MyUnFilterA);
abort();
}
When I pressed button OK, an excepiton will occur. Case 1 and case 3 are the same result. That is,
MyUnFilterA will be be called and a messagebox will pop up and show the text "11". And after the messagebox is closed, the application exits. Case 2 and case 2 are the same result. An error dialog will pop up first and ask me "Abort", "Retry" or "Ignore". If select "Retry", the message box "11" will show. Otherwise, the application will exit.
I think it's better pop up my defined message box first. What should I do?
modified on Thursday, October 22, 2009 5:32 AM
|
|
|
|
|
Hi, can someone help me out with CFile Read mode. I want to read from a txt file and place it in the format below. My code give me error. Can't read at all.
My code
CStdioFile f1;
CString f1_txt;
f1.Open("C:/abc", CFile::modeRead);
int len = f1.GetLength();
f1.Read(f1_txt, len);
_tscanf(f1_txt, "%lf %lf %lf\n", &a, &b, &c);
....
....
....
f1.Close();
f1.Flush();
Can someone help me out with this? Need to read in this "%lf %lf %lf\n", &a, &b, &c) format.
Thanks alot.
|
|
|
|
|
If you use Read function, then its better to use CFile.
Use ReadString to get hte benefit of CStdioFile.
CStdioFile FileData;
CString chLine;
if( !FileData.Open( csFilePath_i, CFile::modeRead ))
{
return;
}
FileData.SeekToBegin();
while (FileData.ReadString(chLine))
{
_stscanf(chLine, "%lf %lf %lf\n", &a, &b, &c);
}
Величие не Бога может быть недооценена.
|
|
|
|
|
Thanks for replied ARJ09.
I can't readstring. I can only use Read. If i will readstring, my output will be wrong cos my file consist of numbers and abit of letters.
|
|
|
|
|
That wont be a problem. If you use CStdioFile then it means you like to ReadString, it will help to read line by line of your file so easily as mentioned above.
But if you prefer to read chunk by chunk data then i prefer you to use Read itself.
Величие не Бога может быть недооценена.
modified on Thursday, October 22, 2009 2:07 AM
|
|
|
|
|
Nicholas Amh wrote: _tscanf(f1_txt, "%lf %lf %lf\n", &a, &b, &c);
I think yesterday you said its working...
Whats the error?
|
|
|
|
|
The error is the program will just stop and ask me to debug, send error online or not.
It work when i change the CString txt to char* txt = new char[len+1]
but my output result is wrong. I am read some number with letter from a txt file. The txt file is quite big.
|
|
|
|
|
Check the value in the f1_txt after reading. Is it same you wrote to the file?
|
|
|
|
|
|
See my response here[^], you are going round in circles.
|
|
|
|
|