这里有篇介绍的很详细的文章:https://www.codeproject.com/Articles/633/Introduction-to-COM-What-It-Is-and-How-to-Use-It
这是我根据文章写的demo,能够成功实现
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <WinInet.h> // Shell object uses INTERNET_MAX_URL_LENGTH (go figure)
#if _MSC_VER < 1400
#define _WIN32_IE 0x0400
#endif
#include <atlbase.h> // ATL smart pointers
#include <shlguid.h> // shell GUIDs
#include <shlobj.h> // IActiveDesktop
#include <string>
#include <iostream>
struct __declspec(uuid("F490EB00-1240-11D1-9888-006097DEACF9")) IActiveDesktop;
using namespace std;
LPWSTR ConvertToLPWSTR(const std::string& s)
{
LPWSTR ws = new wchar_t[s.size() + 1]; // +1 for zero at the end
copy(s.begin(), s.end(), ws);
ws[s.size()] = 0; // zero at the end
return ws;
}
int main()
{
WCHAR wszWallpaper[MAX_PATH];
string strPath;
HRESULT hr;
IActiveDesktop* pIAD;
// 1. Initialize the COM library (make Windows load the DLLs). Normally you would
// call this in your InitInstance() or other startup code. In MFC apps, use
// AfxOleInit() instead.</FONT>
CoInitialize(NULL);
//<FONT COLOR = "#009900">// 2. Create a COM object, using the Active Desktop coclass provided by the shell.
// The 4th parameter tells COM what interface we want (IActiveDesktop).</FONT>
hr = CoCreateInstance(CLSID_ActiveDesktop,
NULL,
CLSCTX_INPROC_SERVER,
IID_IActiveDesktop,
(void**)&pIAD);
if (SUCCEEDED(hr))
{
// <FONT COLOR = "#009900">// 3. If the COM object was created, call its GetWallpaper() method.</FONT>
hr = pIAD->GetWallpaper(wszWallpaper, MAX_PATH, 0);
if (SUCCEEDED(hr))
{
// 4. If GetWallpaper() succeeded, print the filename it returned.
// Note that I'm using wcout to display the Unicode string wszWallpaper.
// wcout is the Unicode equivalent of cout.
wcout << L"Wallpaper path is:\n " << wszWallpaper << endl << endl;
}
else
{
cout << _T("GetWallpaper() failed.") << endl << endl;
}
// 5. Release the interface.
pIAD->Release();
}
else
{
cout << _T("CoCreateInstance() failed.") << endl << endl;
}
// 6. Uninit the COM library. In MFC apps, this is not necessary since MFC does
// it for us.
CoUninitialize();
///////////////////////////////////////////////////////////////////////////////////////
wstring ws(wszWallpaper);
string sWallpaper(ws.begin(), ws.end()); // Convert the wallpaper path to ANSI
IShellLink* pISL;
IPersistFile* pIPF;
// 1. Initialize the COM library (make Windows load the DLLs). Normally you would
// call this in your InitInstance() or other startup code. In MFC apps, use
// AfxOleInit() instead.
CoInitialize(NULL);
// 2. Create a COM object, using the Shell Link coclass provided by the shell.
// The 4th parameter tells COM what interface we want (IShellLink).
hr = CoCreateInstance(CLSID_ShellLink,
NULL,
CLSCTX_INPROC_SERVER,
IID_IShellLink,
(void**)&pISL);
if (SUCCEEDED(hr))
{
// 3. Set the path of the shortcut's target (the wallpaper file).
LPWSTR ws = ConvertToLPWSTR(sWallpaper);
hr = pISL->SetPath((LPCSTR)ws);
if (SUCCEEDED(hr))
{
// 4. Get a second interface (IPersistFile) from the COM object.
hr = pISL->QueryInterface(IID_IPersistFile, (void**)&pIPF);
if (SUCCEEDED(hr))
{
// 5. Call the Save() method to save the shortcut to a file. The
// first parameter is a Unicode string.
hr = pIPF->Save(L"C:\\Users\\strives\\Desktop\\wallpaper.lnk", FALSE);
// 6a. Release the IPersistFile interface.
pIPF->Release();
}
}
// 6b. Release the IShellLink interface.
pISL->Release();
}
// Printing of error messages omitted here.
// 7. Uninit the COM library. In MFC apps, this is not necessary since MFC
// does it for us.
CoUninitialize();
}