在VC++里面要使用loadlibrary的方式来使用该API,我看过MSDN后,尝试自己将头文件的定义里面加入这个API,并且comment了shell32.dll,但是还是提示找不到外部变量。
http://msdn.microsoft.com/en-us/library/dd378422(VS.85).aspx
后面google了一下,得到下面的文章
http://code.logos.com/blog/c_1/
以下为代码
不过需要注意的是,C++和C#两种方法设置的APPID不能通用,也就是不能将C++和 C#的程序pin到同一个icon上,原因未明。
当然一个办法是:
用C++去 Load C#写的dll,这个DLL的作用是使用C#的方式去设置AppID
下面同一般使用DLL的方法:
typedef HRESULT (__stdcall *SETCURRENTPROCESSEXPLICITAPPUSERMODELIDPROC)(PCWSTR AppID);
// 给这个进程赋予一个指定的APPID,这样win7就会把他们放到一个ICON上
void SetAppUserModelId()
{
// try to load Shell32.dll
HMODULE hmodShell32 = LoadLibrary(L"shell32.dll" );
if (hmodShell32 != NULL)
{
// see if the function is exposed by the current OS
SETCURRENTPROCESSEXPLICITAPPUSERMODELIDPROC pfnSetCurrentProcessExplicitAppUserModelID =
reinterpret_cast<SETCURRENTPROCESSEXPLICITAPPUSERMODELIDPROC>(GetProcAddress(hmodShell32,
"SetCurrentProcessExplicitAppUserModelID" ));
if (pfnSetCurrentProcessExplicitAppUserModelID != NULL)
{
pfnSetCurrentProcessExplicitAppUserModelID(L"YourCompany.YourApp.1" );
}
FreeLibrary(hmodShell32);
}
}
To the C# WPF application, add similar code (with a version check):
C# WPF的ap也是添加类似的方法
/// <summary>
/// Sets the Windows 7 application user model ID that will apply to the entire process. This identifier allows an application to
/// group its associated processes and windows under a single taskbar button.
/// </summary>
/// <param name="appId"> The application user model ID. </param>
public static void SetApplicationUserModelId(string appId)
{
// check for Windows 7
Version version = Environment .OSVersion.Version;
if ((version.Major > 6) || (version.Major == 6 && version.Minor >= 1))
{
SafeNativeMethods .SetCurrentProcessExplicitAppUserModelID(appId);
}
}
[SuppressUnmanagedCodeSecurity ]
internal static class SafeNativeMethods
{
[DllImport ("shell32.dll" )]
public static extern void SetCurrentProcessExplicitAppUserModelID([MarshalAs(UnmanagedType .LPWStr)] string AppID);
}
需要在代码里面尽快调用这个方法——要在界面出现之前。