-->

Access violation using FtpFindFirstFile unicode ve

2019-08-22 08:24发布

问题:

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.

回答1:

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.



回答2:

I'd recommend changing compiler setting Character Set from Unicode to Multi-Byte Character Set. The same thing was happening to me.