WIn32中CInternetSession运行异常(afxCurrentAppName 为空)

最近在做一个小任务,其中有一项功能是获取网页源码,VC中可以用 CInternetSession 来实现,以下为从MSDN摘下来的
[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. CInternetSession session;   
  2. CHttpFile* file = NULL;   
  3.   
  4. file = (CHttpFile *)session.OpenURL(_T("http://www.microsoft.com"));   
  5. if (NULL != file)  
  6. {  
  7. //Do something here with the web request  
  8.   
  9. //Clean up the file here to avoid a memory leak!!!!!!!  
  10. file->Close();   
  11. delete file;   
  12. }  
  13. session.Close();   

编译后运行显示错误:

Debug Assertion failed!
program:test.exe
file:afxwin1.inl
line:27

跟踪到afxwin1.inl文件的第27行是

{ ASSERT(afxCurrentAppName != NULL); return afxCurrentAppName; }

这里说明是AfxCurrentAppName为空造成的问题;

解决办法:
CWinApp app((LPCTSTR)argv[0]);
app.InitApplication();

AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0);

如果argv[0]基本常识普及,请参考以下:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. C/C++语言中的main函数,经常带有参数argc,argv,如下:   
  2.   
  3. int main(int argc, char** argv)  
  4.   
  5. int main(int argc, char* argv[])  
  6.   
  7. 这两个参数的作用是什么呢?argc 是指命令行输入参数的个数,argv存储了所有的命令行参数。假如你的程序是hello.exe,如果在命令行运行该程序,(首先应该在命令行下用 cd 命令进入到 hello.exe 文件所在目录) 运行命令为:   
  8.   
  9. hello.exe Shiqi Yu  
  10.   
  11. 那么,argc的值是 3,argv[0]是"hello.exe",argv[1]是"Shiqi",argv[2]是"Yu"。   
  12.   
  13. 下面的程序演示argc和argv的使用:   
  14.   
  15. #include <stdio.h>  
  16. int main(int argc, char ** argv)  
  17. {  
  18. int i;  
  19. for (i=0; i < argc; i++)  
  20. printf("Argument %d is %s.\n", i, argv[i]);  
  21. return 0;  
  22. }  
  23.   
  24. 假如上述代码编译为hello.exe,那么运行   
  25.   
  26. hello.exe a b c d e  
  27.   
  28. 将得到   
  29.   
  30. Argument 0 is hello.exe.  
  31. Argument 1 is a.  
  32. Argument 2 is b.  
  33. Argument 3 is c.  
  34. Argument 4 is d.  
  35. Argument 5 is e.  
  36.   
  37. 运行   
  38.   
  39. hello.exe lena.jpg  
  40.   
  41. 将得到   
  42.   
  43. Argument 0 is hello.exe.  
  44. Argument 1 is lena.jpg.  

分析:在win32程序中调用MFC中的类
[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include <stdio.h>  
  2. #include <afxinet.h>  
  3. #include <afx.h>  
  4. #include <afxwin.h>  
  5.   
  6. int main(int argc, char **argv)  
  7. {  
  8.     CWinApp app((LPCTSTR)argv[0]);  
  9.     app.InitApplication();  
  10.     AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0);  
  11.     CInternetSession session;   
  12.     CHttpFile* file = NULL;   
  13.   
  14.     file = (CHttpFile *)session.OpenURL(_T("http://www.microsoft.com"));   
  15.     if (NULL != file)  
  16.     {  
  17.         //Do something here with the web request  
  18.         //Clean up the file here to avoid a memory leak!!!!!!!  
  19.         file->Close();   
  20.         delete file;   
  21.     }  
  22.     session.Close();   
  23.     return 0;  
  24. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值