获取固定到任务栏的快捷方式的图标

这是之前不知道从哪里找到的demo代码,是获取固定到任务栏的快捷方式的图标信息的。也就是将此程序锁定到任务栏后,通过下面的代码可以获取到锁定到任务栏的信息。相信也可以实现锁定到任务栏,我这里仅仅是搬运过来备忘。代码实测可以运行。

#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <tchar.h>

#include <windows.h>
#pragma comment(lib, "ole32.lib")
#pragma comment(lib, "uuid.lib")
#include <shlobj.h>

#include <initguid.h>

DEFINE_GUID(CLSID_TaskbandPin, 0x90AA3A4E,0x1CBA,0x4233,0xB8,0xBB,0x53,0x57,0x73,0xD4,0x84,0x49);

static IID iids[] = {
	{ 0x60274FA2, 0x611F, 0x4B8A, { 0xA2,0x93,0xF2,0x7B,0xF1,0x03,0xD1,0x48 } },
	{ 0xBBD20037, 0xBC0E, 0x42F1, { 0x91,0x3F,0xE2,0x93,0x6B,0xB0,0xEA,0x0C } },
	{ 0xC3C6EB6D, 0xC837, 0x4EAE, { 0xB1,0x72,0x5F,0xEC,0x52,0xA2,0xA4,0xFD } }
};

DECLARE_INTERFACE_(IFlexibleTaskbarPinnedList, IUnknown)
{
	STDMETHOD(QueryInterface)(THIS_ REFIID riid, void **ppvObject) PURE;
	STDMETHOD_(ULONG, AddRef)(THIS) PURE;
	STDMETHOD_(ULONG, Release)(THIS) PURE;
	STDMETHOD(EnumObjects)(THIS_ IEnumFullIDList **) PURE;
};

int main(int argc, char *argv[])
{
#if defined(UNICODE) || defined(_UNICODE)
	setlocale(LC_CTYPE, ".ACP");
#endif
	HRESULT hr = CoInitialize(NULL);
	if (hr == S_OK)
	{
		IUnknown *punkn = NULL;
		hr = CoCreateInstance(CLSID_TaskbandPin, NULL, CLSCTX_INPROC_SERVER, IID_IUnknown, (void **)&punkn);
		if (hr == S_OK)
		{
			IFlexibleTaskbarPinnedList *pftpl = NULL;
			for (size_t i = 0; i < ARRAYSIZE(iids); i++)
			{
				hr = punkn->QueryInterface(iids[i], (void **)&pftpl);
				if (hr == S_OK)
				{
					break;
				}
			}
			if (hr == S_OK)
			{
				IEnumFullIDList *lpeil = NULL;
				hr = pftpl->EnumObjects(&lpeil);
				if (hr == S_OK)
				{
					ULONG ulFetched = 0;
					LPITEMIDLIST pidl = NULL;
					hr = lpeil->Reset();
					do {
						hr = lpeil->Next(1, &pidl, &ulFetched);
						if (hr == S_OK)
						{
							SHFILEINFO shfi = { 0 };
							SHGetFileInfo((LPCTSTR)pidl, 0, &shfi, sizeof(shfi), SHGFI_PIDL | SHGFI_DISPLAYNAME);
							_tprintf(_T("%s\n"), shfi.szDisplayName);
							CoTaskMemFree(pidl);
						}
					} while (hr == S_OK);
					lpeil->Release();
				}
				pftpl->Release();
			}
			punkn->Release();
		}
		CoUninitialize();
	}
	return !getchar();
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券