在异步模式下使用FtpFindFirstFile unicode版本访问冲突 [英] Access violation using FtpFindFirstFile unicode version in async mode

查看:263
本文介绍了在异步模式下使用FtpFindFirstFile unicode版本访问冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有非常简单的小代码示例,它使用异步WinInet:

  #includestdafx.h
#includeWinInet.h
#pragma comment(lib,wininet.lib)

DWORD LatestResult = 0;
HANDLE MayContinue = 0;

VOID CALLBACK
CallBack(
__in HINTERNET hInternet,
__in DWORD_PTR dwContext,
__in DWORD dwInternetStatus,
__in_bcount(dwStatusInformationLength)LPVOID lpvStatusInformation ,
__in DWORD dwStatusInformationLength

{
if(dwInternetStatus == INTERNET_STATUS_REQUEST_COMPLETE)
{
LatestResult =((LPINTERNET_ASYNC_RESULT)lpvStatusInformation) - > dwResult ;
SetEvent(MayContinue);
}
}

int APIENTRY _tWinMain(_In_HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPTSTR lpCmdLine,
_In_ int nCmdShow)
{
MayContinue = :: CreateEvent(NULL,FALSE,FALSE,NULL);
HINTERNET Session = InternetOpen(NULL,INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,INTERNET_FLAG_ASYNC);
INTERNET_STATUS_CALLBACK CallbackPointer = InternetSetStatusCallback(Session,(INTERNET_STATUS_CALLBACK)CallBack);

MayContinue = :: CreateEvent(NULL,FALSE,FALSE,NULL);
InternetConnect(Session,_T(ftp.secureftp-test.com),INTERNET_INVALID_PORT_NUMBER,_T(test),_T(test),INTERNET_SERVICE_FTP,0,1);

WaitForSingleObject(MayContinue,INFINITE);
HINTERNET Internet =(HINTERNET)LatestResult;

WIN32_FIND_DATA * FindData = new WIN32_FIND_DATA;
FtpFindFirstFileW(Internet,_T(*。*),FindData,0,1);
WaitForSingleObject(MayContinue,INFINITE);
delete FindData;
return 0;
}

执行后得到的结果:



WinInetTest.exe中0xBAADF00D处的未处理异常:
0xC0000005:访问冲突执行位置0xBAADF00D。

在最终的WaitForSingleObject和callstack之间发生的事让人困惑。



但如果我改变

  WIN32_FIND_DATA * FindData = new WIN32_FIND_DATA; 
FtpFindFirstFileW(Internet,_T(*。*),FindData,0,1);

  WIN32_FIND_DATAA * FindData = new WIN32_FIND_DATAA; 
FtpFindFirstFileA(Internet,*。*,FindData,0,1);

它执行和工作原理。
所以我的问题是 - 我真的不正确的事情或它只是失败在WinInet端?



我使用Visual Studio 2012 btw在Windows 7上测试。

解决方案

我也有困难FtpFindFirstFileW。当我把我的项目从MBCS转换为Unicode FtpFindFirstFileW导致一个访问冲突,似乎是一个指针解除引用0xbaadf00d在调用后的某个地方,可能是在准备异步结果。我通过在MBCS和Unicode构建中使用FtpFindFirstFileA,InternetFindNextFileA和WIN32_FIND_DATAA结构来解决这个问题。然后我将输出cFileName字段转换为TCHAR字符串。


Let's say I have very simple little code sample which uses async WinInet:

#include "stdafx.h"
#include "WinInet.h"
#pragma comment(lib, "wininet.lib")

DWORD LatestResult = 0;
HANDLE MayContinue = 0;

VOID CALLBACK
  CallBack(
  __in HINTERNET hInternet,
  __in DWORD_PTR dwContext,
  __in DWORD dwInternetStatus,
  __in_bcount(dwStatusInformationLength) LPVOID lpvStatusInformation,
  __in DWORD dwStatusInformationLength
  )
{
  if (dwInternetStatus == INTERNET_STATUS_REQUEST_COMPLETE)
  {
    LatestResult = ((LPINTERNET_ASYNC_RESULT)lpvStatusInformation)->dwResult;
    SetEvent (MayContinue);
  }
}

int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
                       _In_opt_ HINSTANCE hPrevInstance,
                       _In_ LPTSTR    lpCmdLine,
                       _In_ int       nCmdShow)
{
  MayContinue = ::CreateEvent (NULL, FALSE, FALSE, NULL);
  HINTERNET Session = InternetOpen (NULL, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, INTERNET_FLAG_ASYNC);
  INTERNET_STATUS_CALLBACK CallbackPointer = InternetSetStatusCallback (Session, (INTERNET_STATUS_CALLBACK) CallBack);

  MayContinue = ::CreateEvent (NULL, FALSE, FALSE, NULL);
  InternetConnect (Session, _T ("ftp.secureftp-test.com"), INTERNET_INVALID_PORT_NUMBER, _T ("test"), _T ("test"), INTERNET_SERVICE_FTP, 0, 1);

  WaitForSingleObject (MayContinue, INFINITE);
  HINTERNET Internet = (HINTERNET) LatestResult;

  WIN32_FIND_DATA *FindData = new WIN32_FIND_DATA;
  FtpFindFirstFileW (Internet, _T ("*.*"), FindData, 0, 1);
  WaitForSingleObject (MayContinue, INFINITE);
  delete FindData;
  return 0;
}

What I've got after execution:

Unhandled exception at 0xBAADF00D in WinInetTest.exe: 
0xC0000005: Access violation     executing location 0xBAADF00D.

It happens somewhere around final WaitForSingleObject and callstack is rather confusing.

But if I change

WIN32_FIND_DATA *FindData = new WIN32_FIND_DATA;
FtpFindFirstFileW (Internet, _T ("*.*"), FindData, 0, 1);

to

WIN32_FIND_DATAA *FindData = new WIN32_FIND_DATAA;
FtpFindFirstFileA (Internet, "*.*", FindData, 0, 1);

It executes and works as it should. So my question is - am I really not doing something correctly or it just failure on WinInet side?

I'm testing it on Windows 7 using Visual Studio 2012 btw.

解决方案

I also had difficulty with FtpFindFirstFileW. When I converted my project from MBCS to Unicode FtpFindFirstFileW resulted in an access violation that appeared to be a pointer dereference of 0xbaadf00d somewhere after the call, possibly when the async result was being prepared. I worked around this by using FtpFindFirstFileA, InternetFindNextFileA and the WIN32_FIND_DATAA struct in both MBCS and Unicode builds. I then convert the output cFileName field to a TCHAR string.

这篇关于在异步模式下使用FtpFindFirstFile unicode版本访问冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆