我编写了一个 SOAP Web 服务,我试图从 MFC 应用程序中使用它。MFC 应用程序不是使用 ActiveX 控件支持或自动化生成的。当我从 Vista 或 Win7 运行 MFC 应用程序时,访问 Web 服务没有问题。当我从 XP (SP3) 运行 MFC 应用程序时,Web 服务调用失败并显示0x800401F0
CO_E_NOTINITIALIZED
“尚未调用 CoInitialize”。调用CoInitialize(NULL)
修复了 XP 问题。
在这种情况下,为什么 XP 中的 MFC 应用程序的行为与 Vista/Win7 不同?
EDIT2:添加了自动生成的 Web 服务访问代码
net_webservicexwww.h
//
// sproxy.exe generated file
// do not modify this file
//
// Created: 08/15/2013@13:09:29
//
#pragma once
#if !defined(_WIN32_WINDOWS) && !defined(_WIN32_WINNT) && !defined(_WIN32_WCE)
#pragma message("warning: defining _WIN32_WINDOWS = 0x0410")
#define _WIN32_WINDOWS 0x0410
#endif
#include <atlsoap.h>
namespace USZip
{
template <typename TClient = CSoapSocketClientT<> >
class CUSZipT :
public TClient,
public CSoapRootHandler
{
protected:
const _soapmap ** GetFunctionMap();
const _soapmap ** GetHeaderMap();
void * GetHeaderValue();
const wchar_t * GetNamespaceUri();
const char * GetServiceName();
const char * GetNamespaceUriA();
HRESULT CallFunction(
void *pvParam,
const wchar_t *wszLocalName, int cchLocalName,
size_t nItem);
HRESULT GetClientReader(ISAXXMLReader **ppReader);
public:
HRESULT __stdcall QueryInterface(REFIID riid, void **ppv)
{
if (ppv == NULL)
{
return E_POINTER;
}
*ppv = NULL;
if (InlineIsEqualGUID(riid, IID_IUnknown) ||
InlineIsEqualGUID(riid, IID_ISAXContentHandler))
{
*ppv = static_cast<ISAXContentHandler *>(this);
return S_OK;
}
return E_NOINTERFACE;
}
ULONG __stdcall AddRef()
{
return 1;
}
ULONG __stdcall Release()
{
return 1;
}
CUSZipT(ISAXXMLReader *pReader = NULL)
:TClient(_T("http://www.webservicex.net/uszip.asmx"))
{
SetClient(true);
SetReader(pReader);
}
~CUSZipT()
{
Uninitialize();
}
void Uninitialize()
{
UninitializeSOAP();
}
HRESULT GetInfoByAreaCode(
BSTR USAreaCode,
BSTR* GetInfoByAreaCodeResult
);
HRESULT GetInfoByCity(
BSTR USCity,
BSTR* GetInfoByCityResult
);
HRESULT GetInfoByState(
BSTR USState,
BSTR* GetInfoByStateResult
);
HRESULT GetInfoByZIP(
BSTR USZip,
BSTR* GetInfoByZIPResult
);
};
typedef CUSZipT<> CUSZip;
struct __CUSZip_GetInfoByAreaCode_struct
{
BSTR USAreaCode;
BSTR GetInfoByAreaCodeResult;
};
extern __declspec(selectany) const _soapmapentry __CUSZip_GetInfoByAreaCode_entries[] =
{
{
0x529BE19C,
"USAreaCode",
L"USAreaCode",
sizeof("USAreaCode")-1,
SOAPTYPE_STRING,
SOAPFLAG_NONE | SOAPFLAG_IN | SOAPFLAG_PID | SOAPFLAG_DOCUMENT | SOAPFLAG_LITERAL | SOAPFLAG_NULLABLE,
offsetof(__CUSZip_GetInfoByAreaCode_struct, USAreaCode),
NULL,
NULL,
-1,
},
{
0xCB1A9C5A,
"GetInfoByAreaCodeResult",
L"GetInfoByAreaCodeResult",
sizeof("GetInfoByAreaCodeResult")-1,
SOAPTYPE_STRING,
SOAPFLAG_NONE | SOAPFLAG_OUT | SOAPFLAG_PID | SOAPFLAG_DOCUMENT | SOAPFLAG_LITERAL | SOAPFLAG_NULLABLE,
offsetof(__CUSZip_GetInfoByAreaCode_struct, GetInfoByAreaCodeResult),
NULL,
NULL,
-1,
},
{ 0x00000000 }
};
extern __declspec(selectany) const _soapmap __CUSZip_GetInfoByAreaCode_map =
{
0xFBDA3B2A,
"GetInfoByAreaCode",
L"GetInfoByAreaCodeResponse",
sizeof("GetInfoByAreaCode")-1,
sizeof("GetInfoByAreaCodeResponse")-1,
SOAPMAP_FUNC,
__CUSZip_GetInfoByAreaCode_entries,
sizeof(__CUSZip_GetInfoByAreaCode_struct),
1,
-1,
SOAPFLAG_NONE | SOAPFLAG_PID | SOAPFLAG_DOCUMENT | SOAPFLAG_LITERAL,
0x8833C2E7,
"http://www.webserviceX.NET",
L"http://www.webserviceX.NET",
sizeof("http://www.webserviceX.NET")-1
};
struct __CUSZip_GetInfoByCity_struct
{
BSTR USCity;
BSTR GetInfoByCityResult;
};
extern __declspec(selectany) const _soapmapentry __CUSZip_GetInfoByCity_entries[] =
{
{
0xCC4AFAC1,
"USCity",
L"USCity",
sizeof("USCity")-1,
SOAPTYPE_STRING,
SOAPFLAG_NONE | SOAPFLAG_IN | SOAPFLAG_PID | SOAPFLAG_DOCUMENT | SOAPFLAG_LITERAL | SOAPFLAG_NULLABLE,
offsetof(__CUSZip_GetInfoByCity_struct, USCity),
NULL,
NULL,
-1,
},
{
0xE3D245BF,
"GetInfoByCityResult",
L"GetInfoByCityResult",
sizeof("GetInfoByCityResult")-1,
SOAPTYPE_STRING,
SOAPFLAG_NONE | SOAPFLAG_OUT | SOAPFLAG_PID | SOAPFLAG_DOCUMENT | SOAPFLAG_LITERAL | SOAPFLAG_NULLABLE,
offsetof(__CUSZip_GetInfoByCity_struct, GetInfoByCityResult),
NULL,
NULL,
-1,
},
{ 0x00000000 }
};
extern __declspec(selectany) const _soapmap __CUSZip_GetInfoByCity_map =
{
0x2121D1CF,
"GetInfoByCity",
L"GetInfoByCityResponse",
sizeof("GetInfoByCity")-1,
sizeof("GetInfoByCityResponse")-1,
SOAPMAP_FUNC,
__CUSZip_GetInfoByCity_entries,
sizeof(__CUSZip_GetInfoByCity_struct),
1,
-1,
SOAPFLAG_NONE | SOAPFLAG_PID | SOAPFLAG_DOCUMENT | SOAPFLAG_LITERAL,
0x8833C2E7,
"http://www.webserviceX.NET",
L"http://www.webserviceX.NET",
sizeof("http://www.webserviceX.NET")-1
};
struct __CUSZip_GetInfoByState_struct
{
BSTR USState;
BSTR GetInfoByStateResult;
};
extern __declspec(selectany) const _soapmapentry __CUSZip_GetInfoByState_entries[] =
{
{
0x56D19209,
"USState",
L"USState",
sizeof("USState")-1,
SOAPTYPE_STRING,
SOAPFLAG_NONE | SOAPFLAG_IN | SOAPFLAG_PID | SOAPFLAG_DOCUMENT | SOAPFLAG_LITERAL | SOAPFLAG_NULLABLE,
offsetof(__CUSZip_GetInfoByState_struct, USState),
NULL,
NULL,
-1,
},
{
0x271736E7,
"GetInfoByStateResult",
L"GetInfoByStateResult",
sizeof("GetInfoByStateResult")-1,
SOAPTYPE_STRING,
SOAPFLAG_NONE | SOAPFLAG_OUT | SOAPFLAG_PID | SOAPFLAG_DOCUMENT | SOAPFLAG_LITERAL | SOAPFLAG_NULLABLE,
offsetof(__CUSZip_GetInfoByState_struct, GetInfoByStateResult),
NULL,
NULL,
-1,
},
{ 0x00000000 }
};
extern __declspec(selectany) const _soapmap __CUSZip_GetInfoByState_map =
{
0x4967ACF7,
"GetInfoByState",
L"GetInfoByStateResponse",
sizeof("GetInfoByState")-1,
sizeof("GetInfoByStateResponse")-1,
SOAPMAP_FUNC,
__CUSZip_GetInfoByState_entries,
sizeof(__CUSZip_GetInfoByState_struct),
1,
-1,
SOAPFLAG_NONE | SOAPFLAG_PID | SOAPFLAG_DOCUMENT | SOAPFLAG_LITERAL,
0x8833C2E7,
"http://www.webserviceX.NET",
L"http://www.webserviceX.NET",
sizeof("http://www.webserviceX.NET")-1
};
struct __CUSZip_GetInfoByZIP_struct
{
BSTR USZip;
BSTR GetInfoByZIPResult;
};
extern __declspec(selectany) const _soapmapentry __CUSZip_GetInfoByZIP_entries[] =
{
{
0x0631331B,
"USZip",
L"USZip",
sizeof("USZip")-1,
SOAPTYPE_STRING,
SOAPFLAG_NONE | SOAPFLAG_IN | SOAPFLAG_PID | SOAPFLAG_DOCUMENT | SOAPFLAG_LITERAL | SOAPFLAG_NULLABLE,
offsetof(__CUSZip_GetInfoByZIP_struct, USZip),
NULL,
NULL,
-1,
},
{
0xD0D29D79,
"GetInfoByZIPResult",
L"GetInfoByZIPResult",
sizeof("GetInfoByZIPResult")-1,
SOAPTYPE_STRING,
SOAPFLAG_NONE | SOAPFLAG_OUT | SOAPFLAG_PID | SOAPFLAG_DOCUMENT | SOAPFLAG_LITERAL | SOAPFLAG_NULLABLE,
offsetof(__CUSZip_GetInfoByZIP_struct, GetInfoByZIPResult),
NULL,
NULL,
-1,
},
{ 0x00000000 }
};
extern __declspec(selectany) const _soapmap __CUSZip_GetInfoByZIP_map =
{
0x4F970009,
"GetInfoByZIP",
L"GetInfoByZIPResponse",
sizeof("GetInfoByZIP")-1,
sizeof("GetInfoByZIPResponse")-1,
SOAPMAP_FUNC,
__CUSZip_GetInfoByZIP_entries,
sizeof(__CUSZip_GetInfoByZIP_struct),
1,
-1,
SOAPFLAG_NONE | SOAPFLAG_PID | SOAPFLAG_DOCUMENT | SOAPFLAG_LITERAL,
0x8833C2E7,
"http://www.webserviceX.NET",
L"http://www.webserviceX.NET",
sizeof("http://www.webserviceX.NET")-1
};
extern __declspec(selectany) const _soapmap * __CUSZip_funcs[] =
{
&__CUSZip_GetInfoByAreaCode_map,
&__CUSZip_GetInfoByCity_map,
&__CUSZip_GetInfoByState_map,
&__CUSZip_GetInfoByZIP_map,
NULL
};
template <typename TClient>
inline HRESULT CUSZipT<TClient>::GetInfoByAreaCode(
BSTR USAreaCode,
BSTR* GetInfoByAreaCodeResult
)
{
if ( GetInfoByAreaCodeResult == NULL )
return E_POINTER;
HRESULT __atlsoap_hr = InitializeSOAP(NULL);
if (FAILED(__atlsoap_hr))
{
SetClientError(SOAPCLIENT_INITIALIZE_ERROR);
return __atlsoap_hr;
}
CleanupClient();
CComPtr<IStream> __atlsoap_spReadStream;
__CUSZip_GetInfoByAreaCode_struct __params;
memset(&__params, 0x00, sizeof(__params));
__params.USAreaCode = USAreaCode;
__atlsoap_hr = SetClientStruct(&__params, 0);
if (FAILED(__atlsoap_hr))
{
SetClientError(SOAPCLIENT_OUTOFMEMORY);
goto __skip_cleanup;
}
__atlsoap_hr = GenerateResponse(GetWriteStream());
if (FAILED(__atlsoap_hr))
{
SetClientError(SOAPCLIENT_GENERATE_ERROR);
goto __skip_cleanup;
}
__atlsoap_hr = SendRequest(_T("SOAPAction: \"http://www.webserviceX.NET/GetInfoByAreaCode\"\r\n"));
if (FAILED(__atlsoap_hr))
{
goto __skip_cleanup;
}
__atlsoap_hr = GetReadStream(&__atlsoap_spReadStream);
if (FAILED(__atlsoap_hr))
{
SetClientError(SOAPCLIENT_READ_ERROR);
goto __skip_cleanup;
}
// cleanup any in/out-params and out-headers from previous calls
Cleanup();
__atlsoap_hr = BeginParse(__atlsoap_spReadStream);
if (FAILED(__atlsoap_hr))
{
SetClientError(SOAPCLIENT_PARSE_ERROR);
goto __cleanup;
}
*GetInfoByAreaCodeResult = __params.GetInfoByAreaCodeResult;
goto __skip_cleanup;
__cleanup:
Cleanup();
__skip_cleanup:
ResetClientState(true);
memset(&__params, 0x00, sizeof(__params));
return __atlsoap_hr;
}
template <typename TClient>
inline HRESULT CUSZipT<TClient>::GetInfoByCity(
BSTR USCity,
BSTR* GetInfoByCityResult
)
{
if ( GetInfoByCityResult == NULL )
return E_POINTER;
HRESULT __atlsoap_hr = InitializeSOAP(NULL);
if (FAILED(__atlsoap_hr))
{
SetClientError(SOAPCLIENT_INITIALIZE_ERROR);
return __atlsoap_hr;
}
CleanupClient();
CComPtr<IStream> __atlsoap_spReadStream;
__CUSZip_GetInfoByCity_struct __params;
memset(&__params, 0x00, sizeof(__params));
__params.USCity = USCity;
__atlsoap_hr = SetClientStruct(&__params, 1);
if (FAILED(__atlsoap_hr))
{
SetClientError(SOAPCLIENT_OUTOFMEMORY);
goto __skip_cleanup;
}
__atlsoap_hr = GenerateResponse(GetWriteStream());
if (FAILED(__atlsoap_hr))
{
SetClientError(SOAPCLIENT_GENERATE_ERROR);
goto __skip_cleanup;
}
__atlsoap_hr = SendRequest(_T("SOAPAction: \"http://www.webserviceX.NET/GetInfoByCity\"\r\n"));
if (FAILED(__atlsoap_hr))
{
goto __skip_cleanup;
}
__atlsoap_hr = GetReadStream(&__atlsoap_spReadStream);
if (FAILED(__atlsoap_hr))
{
SetClientError(SOAPCLIENT_READ_ERROR);
goto __skip_cleanup;
}
// cleanup any in/out-params and out-headers from previous calls
Cleanup();
__atlsoap_hr = BeginParse(__atlsoap_spReadStream);
if (FAILED(__atlsoap_hr))
{
SetClientError(SOAPCLIENT_PARSE_ERROR);
goto __cleanup;
}
*GetInfoByCityResult = __params.GetInfoByCityResult;
goto __skip_cleanup;
__cleanup:
Cleanup();
__skip_cleanup:
ResetClientState(true);
memset(&__params, 0x00, sizeof(__params));
return __atlsoap_hr;
}
template <typename TClient>
inline HRESULT CUSZipT<TClient>::GetInfoByState(
BSTR USState,
BSTR* GetInfoByStateResult
)
{
if ( GetInfoByStateResult == NULL )
return E_POINTER;
HRESULT __atlsoap_hr = InitializeSOAP(NULL);
if (FAILED(__atlsoap_hr))
{
SetClientError(SOAPCLIENT_INITIALIZE_ERROR);
return __atlsoap_hr;
}
CleanupClient();
CComPtr<IStream> __atlsoap_spReadStream;
__CUSZip_GetInfoByState_struct __params;
memset(&__params, 0x00, sizeof(__params));
__params.USState = USState;
__atlsoap_hr = SetClientStruct(&__params, 2);
if (FAILED(__atlsoap_hr))
{
SetClientError(SOAPCLIENT_OUTOFMEMORY);
goto __skip_cleanup;
}
__atlsoap_hr = GenerateResponse(GetWriteStream());
if (FAILED(__atlsoap_hr))
{
SetClientError(SOAPCLIENT_GENERATE_ERROR);
goto __skip_cleanup;
}
__atlsoap_hr = SendRequest(_T("SOAPAction: \"http://www.webserviceX.NET/GetInfoByState\"\r\n"));
if (FAILED(__atlsoap_hr))
{
goto __skip_cleanup;
}
__atlsoap_hr = GetReadStream(&__atlsoap_spReadStream);
if (FAILED(__atlsoap_hr))
{
SetClientError(SOAPCLIENT_READ_ERROR);
goto __skip_cleanup;
}
// cleanup any in/out-params and out-headers from previous calls
Cleanup();
__atlsoap_hr = BeginParse(__atlsoap_spReadStream);
if (FAILED(__atlsoap_hr))
{
SetClientError(SOAPCLIENT_PARSE_ERROR);
goto __cleanup;
}
*GetInfoByStateResult = __params.GetInfoByStateResult;
goto __skip_cleanup;
__cleanup:
Cleanup();
__skip_cleanup:
ResetClientState(true);
memset(&__params, 0x00, sizeof(__params));
return __atlsoap_hr;
}
template <typename TClient>
inline HRESULT CUSZipT<TClient>::GetInfoByZIP(
BSTR USZip,
BSTR* GetInfoByZIPResult
)
{
if ( GetInfoByZIPResult == NULL )
return E_POINTER;
HRESULT __atlsoap_hr = InitializeSOAP(NULL);
if (FAILED(__atlsoap_hr))
{
SetClientError(SOAPCLIENT_INITIALIZE_ERROR);
return __atlsoap_hr;
}
CleanupClient();
CComPtr<IStream> __atlsoap_spReadStream;
__CUSZip_GetInfoByZIP_struct __params;
memset(&__params, 0x00, sizeof(__params));
__params.USZip = USZip;
__atlsoap_hr = SetClientStruct(&__params, 3);
if (FAILED(__atlsoap_hr))
{
SetClientError(SOAPCLIENT_OUTOFMEMORY);
goto __skip_cleanup;
}
__atlsoap_hr = GenerateResponse(GetWriteStream());
if (FAILED(__atlsoap_hr))
{
SetClientError(SOAPCLIENT_GENERATE_ERROR);
goto __skip_cleanup;
}
__atlsoap_hr = SendRequest(_T("SOAPAction: \"http://www.webserviceX.NET/GetInfoByZIP\"\r\n"));
if (FAILED(__atlsoap_hr))
{
goto __skip_cleanup;
}
__atlsoap_hr = GetReadStream(&__atlsoap_spReadStream);
if (FAILED(__atlsoap_hr))
{
SetClientError(SOAPCLIENT_READ_ERROR);
goto __skip_cleanup;
}
// cleanup any in/out-params and out-headers from previous calls
Cleanup();
__atlsoap_hr = BeginParse(__atlsoap_spReadStream);
if (FAILED(__atlsoap_hr))
{
SetClientError(SOAPCLIENT_PARSE_ERROR);
goto __cleanup;
}
*GetInfoByZIPResult = __params.GetInfoByZIPResult;
goto __skip_cleanup;
__cleanup:
Cleanup();
__skip_cleanup:
ResetClientState(true);
memset(&__params, 0x00, sizeof(__params));
return __atlsoap_hr;
}
template <typename TClient>
ATL_NOINLINE inline const _soapmap ** CUSZipT<TClient>::GetFunctionMap()
{
return __CUSZip_funcs;
}
template <typename TClient>
ATL_NOINLINE inline const _soapmap ** CUSZipT<TClient>::GetHeaderMap()
{
static const _soapmapentry __CUSZip_GetInfoByAreaCode_atlsoapheader_entries[] =
{
{ 0x00000000 }
};
static const _soapmap __CUSZip_GetInfoByAreaCode_atlsoapheader_map =
{
0xFBDA3B2A,
"GetInfoByAreaCode",
L"GetInfoByAreaCodeResponse",
sizeof("GetInfoByAreaCode")-1,
sizeof("GetInfoByAreaCodeResponse")-1,
SOAPMAP_HEADER,
__CUSZip_GetInfoByAreaCode_atlsoapheader_entries,
0,
0,
-1,
SOAPFLAG_NONE | SOAPFLAG_PID | SOAPFLAG_DOCUMENT | SOAPFLAG_LITERAL,
0x8833C2E7,
"http://www.webserviceX.NET",
L"http://www.webserviceX.NET",
sizeof("http://www.webserviceX.NET")-1
};
static const _soapmapentry __CUSZip_GetInfoByCity_atlsoapheader_entries[] =
{
{ 0x00000000 }
};
static const _soapmap __CUSZip_GetInfoByCity_atlsoapheader_map =
{
0x2121D1CF,
"GetInfoByCity",
L"GetInfoByCityResponse",
sizeof("GetInfoByCity")-1,
sizeof("GetInfoByCityResponse")-1,
SOAPMAP_HEADER,
__CUSZip_GetInfoByCity_atlsoapheader_entries,
0,
0,
-1,
SOAPFLAG_NONE | SOAPFLAG_PID | SOAPFLAG_DOCUMENT | SOAPFLAG_LITERAL,
0x8833C2E7,
"http://www.webserviceX.NET",
L"http://www.webserviceX.NET",
sizeof("http://www.webserviceX.NET")-1
};
static const _soapmapentry __CUSZip_GetInfoByState_atlsoapheader_entries[] =
{
{ 0x00000000 }
};
static const _soapmap __CUSZip_GetInfoByState_atlsoapheader_map =
{
0x4967ACF7,
"GetInfoByState",
L"GetInfoByStateResponse",
sizeof("GetInfoByState")-1,
sizeof("GetInfoByStateResponse")-1,
SOAPMAP_HEADER,
__CUSZip_GetInfoByState_atlsoapheader_entries,
0,
0,
-1,
SOAPFLAG_NONE | SOAPFLAG_PID | SOAPFLAG_DOCUMENT | SOAPFLAG_LITERAL,
0x8833C2E7,
"http://www.webserviceX.NET",
L"http://www.webserviceX.NET",
sizeof("http://www.webserviceX.NET")-1
};
static const _soapmapentry __CUSZip_GetInfoByZIP_atlsoapheader_entries[] =
{
{ 0x00000000 }
};
static const _soapmap __CUSZip_GetInfoByZIP_atlsoapheader_map =
{
0x4F970009,
"GetInfoByZIP",
L"GetInfoByZIPResponse",
sizeof("GetInfoByZIP")-1,
sizeof("GetInfoByZIPResponse")-1,
SOAPMAP_HEADER,
__CUSZip_GetInfoByZIP_atlsoapheader_entries,
0,
0,
-1,
SOAPFLAG_NONE | SOAPFLAG_PID | SOAPFLAG_DOCUMENT | SOAPFLAG_LITERAL,
0x8833C2E7,
"http://www.webserviceX.NET",
L"http://www.webserviceX.NET",
sizeof("http://www.webserviceX.NET")-1
};
static const _soapmap * __CUSZip_headers[] =
{
&__CUSZip_GetInfoByAreaCode_atlsoapheader_map,
&__CUSZip_GetInfoByCity_atlsoapheader_map,
&__CUSZip_GetInfoByState_atlsoapheader_map,
&__CUSZip_GetInfoByZIP_atlsoapheader_map,
NULL
};
return __CUSZip_headers;
}
template <typename TClient>
ATL_NOINLINE inline void * CUSZipT<TClient>::GetHeaderValue()
{
return this;
}
template <typename TClient>
ATL_NOINLINE inline const wchar_t * CUSZipT<TClient>::GetNamespaceUri()
{
return L"http://www.webserviceX.NET";
}
template <typename TClient>
ATL_NOINLINE inline const char * CUSZipT<TClient>::GetServiceName()
{
return NULL;
}
template <typename TClient>
ATL_NOINLINE inline const char * CUSZipT<TClient>::GetNamespaceUriA()
{
return "http://www.webserviceX.NET";
}
template <typename TClient>
ATL_NOINLINE inline HRESULT CUSZipT<TClient>::CallFunction(
void *,
const wchar_t *, int,
size_t)
{
return E_NOTIMPL;
}
template <typename TClient>
ATL_NOINLINE inline HRESULT CUSZipT<TClient>::GetClientReader(ISAXXMLReader **ppReader)
{
if (ppReader == NULL)
{
return E_INVALIDARG;
}
CComPtr<ISAXXMLReader> spReader = GetReader();
if (spReader.p != NULL)
{
*ppReader = spReader.Detach();
return S_OK;
}
return TClient::GetClientReader(ppReader);
}
} // namespace USZip
编辑:添加源代码。
从 VS2005“MFC 应用程序”模板创建的 MFC 应用程序。除了从高级功能向导设置中选择“基于对话框”的应用程序类型和未选中的“ActiveX 控件”之外的所有默认向导值。
下面代码 (OnBnClickedCallWebWervice) 中的 Web 服务调用在 Vista 和 Win7 系统上运行时有效。从 XP 系统运行时,调用失败并CO_E_NOTINITIALIZED
显示为 HRESULT。如果我添加CoInitialize(NULL)
到 InitInstance 方法,则 Web 服务调用适用于所有三个平台。
测试应用程序.cpp
// TestApp.cpp : Defines the class behaviors for the application.
//
#include "stdafx.h"
#include "TestApp.h"
#include "TestAppDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CTestAppApp
BEGIN_MESSAGE_MAP(CTestAppApp, CWinApp)
ON_COMMAND(ID_HELP, &CWinApp::OnHelp)
END_MESSAGE_MAP()
// CTestAppApp construction
CTestAppApp::CTestAppApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
// The one and only CTestAppApp object
CTestAppApp theApp;
// CTestAppApp initialization
BOOL CTestAppApp::InitInstance()
{
// InitCommonControlsEx() is required on Windows XP if an application
// manifest specifies use of ComCtl32.dll version 6 or later to enable
// visual styles. Otherwise, any window creation will fail.
INITCOMMONCONTROLSEX InitCtrls;
InitCtrls.dwSize = sizeof(InitCtrls);
// Set this to include all the common control classes you want to use
// in your application.
InitCtrls.dwICC = ICC_WIN95_CLASSES;
InitCommonControlsEx(&InitCtrls);
CWinApp::InitInstance();
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need
// Change the registry key under which our settings are stored
// TODO: You should modify this string to be something appropriate
// such as the name of your company or organization
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
CTestAppDlg dlg;
m_pMainWnd = &dlg;
INT_PTR nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}
// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}
测试应用程序Dlg.cpp
// TestAppDlg.cpp : implementation file
//
#include "stdafx.h"
#include "TestApp.h"
#include "TestAppDlg.h"
#include "WebService.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()
// CTestAppDlg dialog
CTestAppDlg::CTestAppDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTestAppDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CTestAppDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CTestAppDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDC_BUTTON_CALLWEBSERVICE, &CTestAppDlg::OnBnClickedCallWebWervice)
END_MESSAGE_MAP()
// CTestAppDlg message handlers
BOOL CTestAppDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CTestAppDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CTestAppDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this function to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CTestAppDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CTestAppDlg::OnBnClickedCallWebWervice()
{
USZip::CUSZip* c = new USZip::CUSZip();
BSTR bstrResult;
HRESULT hr = c->GetInfoByZIP(CComBSTR(_T("90814")), &bstrResult);
if (SUCCEEDED(hr))
{
CString csResult(bstrResult);
GetDlgItem(IDC_STATIC_MSG)->SetWindowText(csResult);
}
else
{
CString csResult;
csResult.Format(_T("Call to web service failed with HRESULT=0x%0x"), hr);
GetDlgItem(IDC_STATIC_MSG)->SetWindowText(csResult);
}
delete c;
}