日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

COM单接口实例

發布時間:2025/4/5 编程问答 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

總結

以上是生活随笔為你收集整理的COM单接口实例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。