COM单接口实例
英文原文: http://www.codeproject.com/KB/COM/comintro.aspx
翻 譯 版: http://wenku.baidu.com/view/6aee206c1eb91a37f1115ca9.html
代碼:
新建一個空工程就ok了.
1. 在StdAfx.h 中添加: #include <wininet.h>??
2. 添加一個MFC類:COMIntro,
??? 2.1 添加:#include <atlconv.h>// ATL string conversion macros
2.2 之后就是在 int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])中添加:
??? 單接口COM創建函數了~.
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) {// initialize MFC and print and error on failureif (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)){cerr << _T("Fatal Error: MFC initialization failed") << endl;return 1;}// Init the COM library - have Windows load up the DLLs.if ( FAILED( CoInitialize(NULL) )){cerr << _T("Fatal Error: OLE initialization failed") << endl;return 1;}WCHAR wszWallpaper [MAX_PATH];HRESULT hr;IActiveDesktop* pIAD;// Create a COM object from the Active Desktop coclass.hr = CoCreateInstance ( CLSID_ActiveDesktop,NULL,CLSCTX_INPROC_SERVER,IID_IActiveDesktop,(void**) &pIAD );if ( SUCCEEDED(hr) ){// Get the name of the wallpaper file.hr = pIAD->GetWallpaper ( wszWallpaper, MAX_PATH, 0 );if ( SUCCEEDED(hr) ){wcout << L"Wallpaper path is:\n " << wszWallpaper << endl << endl;}else{cout << _T("GetWallpaper() failed.") << endl << endl;}// Release the IActiveDesktop interface, since we're done using it.pIAD->Release();}else{cout << _T("CoInitialize() failed.") << endl << endl;}// If anything above failed, quit the program.if ( FAILED(hr) )return 0;CString sWallpaper = wszWallpaper; // Convert the Unicode string to ANSI.IShellLink* pISL;IPersistFile* pIPF;// Create a COM object from the Shell Link coclass.hr = CoCreateInstance ( CLSID_ShellLink,NULL,CLSCTX_INPROC_SERVER,IID_IShellLink,(void**) &pISL );if ( SUCCEEDED(hr) ){// Set the path of the target file (the wallpaper).hr = pISL->SetPath ( sWallpaper );if ( SUCCEEDED(hr) ){// Get an IPersisteFile interface from the COM object.hr = pISL->QueryInterface ( IID_IPersistFile, (void**) &pIPF );if ( SUCCEEDED(hr) ){// Save the shortcut as "C:\wallpaper.lnk" Note that the first// param to IPersistFile::Save() is a Unicode string, thus the L// prefix.hr = pIPF->Save ( L"C:\\wallpaper.lnk", FALSE );if ( SUCCEEDED(hr) ){cout << _T("Shortcut created.") << endl << endl;}else{cout << _T("Save() failed.") << endl << endl;}// Release the IPersistFile interface, since we're done with it.pIPF->Release();}else{cout << _T("QueryInterface() failed.") << endl << endl;}}else{cout << _T("SetPath() failed.") << endl << endl;}// Release the IShellLink interface too.pISL->Release();}else{cout << _T("CoCreateInstance() failed.") << endl << endl;}CoUninitialize();return 0; }結果:
如果你用的是2010的話, 請在:
在工程的stdafx.h中添加(如有類似語句,需注釋掉)
#ifndef WINVER // Allow use of features specific to Windows 95 and Windows NT 4 or later.
#define WINVER 0x0501 // Change this to the appropriate value to target Windows 98 and Windows 2000 or later.
#endif
#ifndef _WIN32_WINNT // Allow use of?features specific to Windows NT 4 or later.
#define _WIN32_WINNT 0x0501 // Change this to the appropriate value to target Windows 98 and Windows 2000 or later.
#endif
#ifndef _WIN32_WINDOWS // Allow use of features specific to Windows 98 or later.
#define _WIN32_WINDOWS 0x0501 // Change this to the appropriate value to target Windows Me or later.
#endif
#ifndef _WIN32_IE // Allow use of features specific to IE 4.0 or later.
#define _WIN32_IE 0x0601 // Change this to the appropriate value to target IE 5.0 or later.
#endif
?
轉載于:https://blog.51cto.com/tuoxie174/416916
總結
- 上一篇: #.NET分别以GET和POST方式抓取
- 下一篇: 数据交互解决方案