Windows 7 taskbar state with minimal code - c++

What would be the shortest code to set the state of a Windows 7 taskbar button for a known window handle?
The goal is to write a console utility that changes the progress and state (colour) of the console window taskbar item from a batch script. While the script performs different tasks, the taskbar item of its console window should represent the current state.
I get the window handle with the GetConsoleWindow() function, but then it seems to require loads of COM and Shell API stuff that I don't understand. One example I've found uses a whole GUI application with MFC to demonstrate the API, but most of it is way too complicated for my little tool and I don't understand it well enough to remove the stuff I don't need.
The tool should compile on Windows 7 with VS2010 (C++) but also run on earlier Windows versions (doing nothing if a feature is not available).

I created a class to set the progress in the Win7 taskbar for a project at one time. It's a wrapper for the ITaskBarList3 interface available from the Windows Shell. It's specifically done with ITaskBarList3.SetProgressState and ITaskBarList3.SetProgressValue functions.
This is the code I dug up:
#include <shobjidl.h>
#include <windows.h>
#pragma comment(lib, "Shell32.lib")
#pragma comment(lib, "Ole32.lib")
class Win7TaskbarProgress
{
public:
Win7TaskbarProgress();
virtual ~Win7TaskbarProgress();
void SetProgressState(HWND hwnd, TBPFLAG flag);
void SetProgressValue(HWND hwnd, ULONGLONG ullCompleted, ULONGLONG ullTotal);
private:
bool Init();
ITaskbarList3* m_pITaskBarList3;
bool m_bFailed;
};
Win7TaskbarProgress::Win7TaskbarProgress()
{
m_pITaskBarList3 = NULL;
m_bFailed = false;
}
Win7TaskbarProgress::~Win7TaskbarProgress()
{
if (m_pITaskBarList3)
{
m_pITaskBarList3->Release();
CoUninitialize();
}
}
void Win7TaskbarProgress::SetProgressState( HWND hwnd, TBPFLAG flag )
{
if (Init())
m_pITaskBarList3->SetProgressState(hwnd, flag);
}
void Win7TaskbarProgress::SetProgressValue( HWND hwnd, ULONGLONG ullCompleted, ULONGLONG ullTotal )
{
if (Init())
m_pITaskBarList3->SetProgressValue(hwnd, ullCompleted, ullTotal);
}
bool Win7TaskbarProgress::Init()
{
if (m_pITaskBarList3)
return true;
if (m_bFailed)
return false;
// Initialize COM for this thread...
CoInitialize(NULL);
CoCreateInstance(CLSID_TaskbarList, NULL, CLSCTX_INPROC_SERVER, IID_ITaskbarList3, (void **)&m_pITaskBarList3);
if (m_pITaskBarList3)
return true;
m_bFailed = true;
CoUninitialize();
return false;
}

Note you still need to call RegisterWindowMessage("TaskbarButtonCreated") and ChangeWindowMessageFilterEx() to setup an message filter before SetProgressValue() can work.
According to the MSDN docs you are supposed to recreate your object each time you get the created message but I found I just had to do the ChangeWindowMessageFilterEx() and it works fine for normal circumstances.

Related

Using IManagedAddin in VC++ to load VSTO VBA add-ins

I was given a VSTO Outlook add-in written in VBA, and have been tasked to reduce the start-up time. I'm rather new to the whole add-in and COM objects thing so I need some help.
The add-in takes anywhere from 0.2s to 2.0s to startup, and Outlook disables the plugin if the average startup time is >1000ms. Using the registry hack to force enable the add-in is unfortunately not an option. I've also tested it with an empty add-in, which also can take up to 1.8s to startup. I've searched SO and other such sites for a solution, and across one involving writing a "stub" in an un-managed language such as Delphi or C++, which does nothing but load the actual add-in. The interface that's supposed to do this is IManagedAddin.
My issue is with implementing this interface. I've created a simple add-in in VC++ The class that implements _IDTExtensibility2 is in Connect.h, shown below. However, I don't have a clue how to implement IManagedAddin::Load to load my add-in into Outlook, and there doesn't seem to be a lot of documentation on this. Any help would be much appreciated!
EDIT: Updated code below
// Connect.h : Declaration of the CConnect
#pragma once
#include "resource.h" // main symbols
#include "NativeAddin_i.h"
#include "IManagedAddin.h"
#include <Windows.h>
#include <iostream>
#if defined(_WIN32_WCE) && !defined(_CE_DCOM) && !defined(_CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA)
#error "Single-threaded COM objects are not properly supported on Windows CE platform, such as the Windows Mobile platforms that do not include full DCOM support. Define _CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA to force ATL to support creating single-thread COM object's and allow use of it's single-threaded COM object implementations. The threading model in your rgs file was set to 'Free' as that is the only threading model supported in non DCOM Windows CE platforms."
#endif
using namespace ATL;
// CConnect
class ATL_NO_VTABLE CConnect :
public CComObjectRootEx<CComSingleThreadModel>,
public CComCoClass<CConnect, &CLSID_Connect>,
public IDispatchImpl<IConnect, &IID_IConnect, &LIBID_PixelLLib, /*wMajor =*/ 1, /*wMinor =*/ 0>,
public IDispatchImpl<_IDTExtensibility2, &__uuidof(_IDTExtensibility2), &LIBID_AddInDesignerObjects, /* wMajor = */ 1, /* wMinor = */ 0>
{
public:
static Outlook::_Application* outlookApp;
static ext_ConnectMode connectMode;
static LPDISPATCH addInInst;
static SAFEARRAY** customArr;
static UINT_PTR timerId;
CConnect()
{
}
DECLARE_REGISTRY_RESOURCEID(106)
BEGIN_COM_MAP(CConnect)
COM_INTERFACE_ENTRY(IConnect)
COM_INTERFACE_ENTRY2(IDispatch, _IDTExtensibility2)
COM_INTERFACE_ENTRY(_IDTExtensibility2)
END_COM_MAP()
DECLARE_PROTECT_FINAL_CONSTRUCT()
HRESULT FinalConstruct()
{
return S_OK;
}
void FinalRelease()
{
}
static VOID CALLBACK TimerCallback(HWND hWnd, UINT nMsg, UINT timerId, DWORD dwTime)
{
KillTimer(NULL, timerId);
HRESULT hr;
BSTR manifestUrl = SysAllocString(L"file://path/to/manifest.vsto");
// load add-in
IID clsid;
hr = IIDFromString(OLESTR("{99D651D7-5F7C-470E-8A3B-774D5D9536AC}"), &clsid); // VSTOAddinLoader CLSID
IID iid;
hr = IIDFromString(OLESTR("{B9CEAB65-331C-4713-8410-DDDAF8EC191A}"), &iid);
IManagedAddin* loader;
hr = CoCreateInstance(clsid, NULL, CLSCTX_ALL, iid, (void**)&loader);
hr = loader->Load(manifestUrl, outlookApp);
_IDTExtensibility2* ext;
hr = loader->QueryInterface(IID__IDTExtensibility2, (void**)&ext);
hr = ext->OnConnection(outlookApp, connectMode, addInInst, customArr);
MSO::IRibbonExtensibility* ribbon;
hr = (???)->QueryInterface(MSO::IID_IRibbonExtensibility, (void**)&ribbon);
}
STDMETHOD(OnConnection)(LPDISPATCH App, ext_ConnectMode ConnectMode, LPDISPATCH AddInInst, SAFEARRAY * * custom)
{
HRESULT hr;
UINT time = 200;
Outlook::_Application* app;
hr = App->QueryInterface(__uuidof(Outlook::_Application), (void**)&app);
// init static members
outlookApp = app;
connectMode = ConnectMode;
addInInst = AddInInst;
customArr = custom;
timerId = SetTimer(NULL, 0, time, (TIMERPROC)&TimerCallback);
return S_OK;
}
STDMETHOD(OnDisconnection)(ext_DisconnectMode RemoveMode, SAFEARRAY * * custom)
{
return S_OK;
}
STDMETHOD(OnAddInsUpdate)(SAFEARRAY * * custom)
{
return S_OK;
}
STDMETHOD(OnStartupComplete)(SAFEARRAY * * custom)
{
return S_OK;
}
STDMETHOD(OnBeginShutdown)(SAFEARRAY * * custom)
{
return S_OK;
}
};
OBJECT_ENTRY_AUTO(__uuidof(Connect), CConnect)
Outlook::_Application* CConnect::outlookApp = NULL;
ext_ConnectMode CConnect::connectMode = ext_cm_AfterStartup;
LPDISPATCH CConnect::addInInst = NULL;
SAFEARRAY** CConnect::customArr = NULL;
UINT_PTR CConnect::timerId = 0;
// IManagedAddin.h
#pragma once
#include "resource.h"
#include "NativeAddin_i.h"
struct __declspec(uuid("B9CEAB65-331C-4713-8410-DDDAF8EC191A"))
IManagedAddin : IUnknown
{
public:
virtual STDMETHOD(Load)(BSTR bstrManifestUrl, LPDISPATCH pdisApplication) = 0;
virtual STDMETHOD(Unload)() = 0;
};
// pch.h
#ifndef PCH_H
#define PCH_H
// add headers that you want to pre-compile here
#include "framework.h"
#import "libid:AC0714F2-3D04-11D1-AE7D-00A0C90F26F4" raw_interfaces_only, raw_native_types, named_guids, auto_search, no_namespace
#import "libid:2DF8D04C-5BFA-101B-BDE5-00AA0044DE52" raw_interfaces_only, raw_native_types, named_guids, auto_search, rename_namespace("MSO")
#import "libid:00062FFF-0000-0000-C000-000000000046" raw_interfaces_only, raw_native_types, named_guids, auto_search, rename_namespace("Outlook")
#endif //PCH_H
What I did was to wait until OnConnection callback fired and started a timer - you might be able to use a separate thread, but in my case the processing had to be done on the main thread due to some functionality that had thread affinity. OnConnection will give you Outlook.Application object.
In the timer callback (Outlook is not looking), create an instance of the IManagedAddin COM object using CoCreateInstance(CLSID_IManagedAddin, ...). Call IManagedAddin::Load. The path must be in the form file://c:/the/folder/myaddin.vsto.
QI the IManagedAddin object for IDTExtensibility2 interface and call IDTExtensibility2::OnConnection() using the parameters saved from the native OnConnection callback.
If Outlook has already called your C++ implementation of OnStartupComplete, call OnStartupComplete on the VSTO addin. If not, you can do it later.
QI for IRibbonExtensibility and call IRibbonExtensibility::GetCustomUI. Or you can hardcode the ribbon XML in the C++ addin. Note that if Outlook calls IRibbonExtensibility::GetCustomUI on your C++ addin, and you have to delegate the call to the VSTO addin before you had a chance to run your timer code, you have no choice but to call the above code immediately rather than in a timer callback since GetCustomUI cannot be postponed.
If you are using task panes (i.e. ICustomTaskPaneConsumer interface is implemented by your VSTO addin), your C++ addin must also implement it (besides the IDTExtensibility2 and ICustomTaskPaneConsumer interfaces). QI IManagedAddin for IServiceProvider interface and use it to call IServiceProvider::QueryService(GUID_NULL, IID_ICustomTaskPaneConsumer, ...). then call ICustomTaskPaneConsumer.CTPFactoryAvailable - note that ICustomTaskPaneConsumer does not come from your VSTO addin's IDTExtensibility2 interface but rather though the IServiceProvider object off the IManagedAddin interface.

How do I create multiple windows in different classes than the main function in C++ with Win32

How do I create multiple windows in different classes than the WinMain function in C++ with Win32?
What I am trying to do:
My Source Files:
FileThatContainsMainFunction.cpp
Window1.cpp
Window2.cpp
In the Main Function I would like to make two objects (one for Window1 and one or Window2) that create Win32 Windows in their constuctors.
eg:
Window1 w1;
Window2 w2;
Note: This question is directed towards C++ classes, not Win32 Classes.
It works pretty much the way you described in your question:
Window1.h
#ifndef Window1H
#include <windows.h>
class Window1
{
private:
HWND hWnd;
public:
Window1();
~Window1();
};
#endif
Window1.cpp
#include "Window1.h"
Window1::Window1()
{
// register and create window as needed
hWnd = CreateWindowEx(...);
}
Window1::~Window1()
{
DestroyWindow(hWnd);
}
Window2.h
#ifndef Window2H
#include <windows.h>
class Window2
{
private:
HWND hWnd;
public:
Window2();
~Window2();
};
#endif
Window2.cpp
#include "Window2.h"
Window2::Window2()
{
// register and create window as needed
hWnd = CreateWindowEx(...);
}
Window2::~Window2()
{
DestroyWindow(hWnd);
}
FileThatContainsMainFunction.cpp
#include "Window1.h"
#include "Window2.h"
int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
Window1 w1;
Window2 w2;
//...
return 0;
}
Everything is possible with the Win API and some C++. But it is not that easy: before creating a window, its WNDCLASS must be registered. As C++ is not very kind with static initialization, you will have to do it in your main before creating any window. After that, you will be able to create your windows with the solution of #RemyLebeau.
If you want to make use of C++ classes to wrap a WNDCLASS, you will need a static function for the WndProc, and store a pointer to the C++ object through SetWindowsLong in the Windows itself. And the only place to do it safely is in the WM_CREATE or WM_NCCREATE notification message. But once this is done, you can simply create many windows in that class, and you will be able to access the c++ object form the (static) WndProc.
As WinAPI is not specially simple nor C++ friendly, my advice would be:
first create your windows directly using the API and forget C++ classes
for the moment
once this works, refactor your application but identifying classes and objects, and optionally use the WM_CREATE trick to allow the WndProc to access directly to the c++ objects
if you want to go further, change the WndProc into a wrapper that just looks for the proper C++ object all calls a non static method on it.
You could look the WTL as an example of using C++ above the C Windows API, but unfortunately the documentation is quite poor.

how to make a *.dll WinAmp plugin work on a friends pc

I just made this very simple .dll plugin for Winamp. It works perfectly fine on my pc but I made it for a friend who requested this. Its the first time I use Visual Studio 2013
and make dll files with c++... my problem is that i can't really figure out why it doesn't work on his pc, I think its an export problem but I am not entirely sure.
#include "stdafx.h"
#include <windows.h>
#include "gen_InfinitePlay.h"
#include "wa_ipc.h"
#include <stdio.h>
using namespace System;
using namespace System::Threading;
// these are callback functions/events which will be called by Winamp
int init(void);
void config(void);
void quit(void);
// this structure contains plugin information, version, name...
// GPPHDR_VER is the version of the winampGeneralPurposePlugin (GPP) structure
winampGeneralPurposePlugin plugin = {
GPPHDR_VER, // version of the plugin, defined in "gen_InfinitePlay.h"
PLUGIN_NAME, // name/title of the plugin, defined in "gen_InfinitePlay.h"
init, // function name which will be executed on init event
config, // function name which will be executed on config event
quit, // function name which will be executed on quit event
0, // handle to Winamp main window, loaded by winamp when this dll is loaded
0 // hinstance to this dll, loaded by winamp when this dll is loaded
};
void play(){
while (true){
if (SendMessage(plugin.hwndParent, WM_WA_IPC, 0, IPC_ISPLAYING) != 1)
SendMessage(plugin.hwndParent, WM_WA_IPC, 0, IPC_STARTPLAY);
Sleep(60000);
}
}
// event functions follow
int init() {
Thread^ t = gcnew Thread(gcnew ThreadStart(play));
t->Start();
return 0;
}
void config() {
//A basic messagebox that tells you the 'config' event has been triggered.
//You can change this later to do whatever you want (including nothing)
//MessageBox(plugin.hwndParent, L"Config event triggered for gen_InfinitePlay.", L"", MB_OK);
}
void quit() {
//A basic messagebox that tells you the 'quit' event has been triggered.
//If everything works you should see this message when you quit Winamp once your plugin has been installed.
//You can change this later to do whatever you want (including nothing)
//MessageBox(0, L"Quit event triggered for gen_InfinitePlay.", L"", MB_OK);
}
// This is an export function called by winamp which returns this plugin info.
// We wrap the code in 'extern "C"' to ensure the export isn't mangled if used in a CPP file.
extern "C" __declspec(dllexport) winampGeneralPurposePlugin * winampGetGeneralPurposePlugin() {
return &plugin;
}
You are missing the Visual Studio 2013 runtime. The proper way to install this is via the Visual Studio 2013 redistributable.
http://www.microsoft.com/en-us/download/details.aspx?id=40784
MSVCR120.DLL was missing, its probably not the "right" way to make it work but I just copied mine to his plugin dir... I still think it could be solved by some export flags or something but I am to lazy for this

VC++ project, how to define DLL missing error messages?

So the idea is when the user haven't installed DirectX End-User Runtime the program to show message like "DirectX Runtime missing! Download it from here", instead of the windows loader error (eg.: "d3dx9_43.dll is missing!"). So I find a very funky solution of the problem as I used a delay loaded DLL's and an DLL check before any function defined in the module is invoked using LoadLibrary. If the dll is missing the program shows a user-defined dialog box and exits, otherwise it calls FreeLibrary with the HMODULE returned by LoadLibrary and continues executing. This is implemented as a function like follows:
bool CheckResourcesAvailability() //Mainly check for the existence of delay loaded DLL's
{
HMODULE hMod; //Resourse handle
if((hMod = LoadLibraryEx(_T("d3d9.dll"), NULL, LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE)) == NULL)
{
DialogBox(hProgramInstance, MAKEINTRESOURCE(IDD_DX_RE), 0, (DLGPROC)&DxRedistMissingDlg);
return false;
}
FreeLibrary(hMod);
if((hMod = LoadLibraryEx(_T("D3DX9_43.dll"), NULL, LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE)) == NULL)
{
DialogBox(hProgramInstance, MAKEINTRESOURCE(IDD_DX_RE), 0, (DLGPROC)&DxRedistMissingDlg);
return false;
}
FreeLibrary(hMod);
return true;
}
*As DxRedistMissingDlg and MAKEINTRESOURCE(IDD_DX_RE) creates the user-defined error message dialog.
And In WinMain it's called as follows:
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
hProgramInstance = hInstance;
#ifndef _DEBUG
SetErrorMode(SEM_FAILCRITICALERRORS); //Don't display windows error messages
#endif
//Check for missing delay - loaded dependencies and inform the user
if(!CheckResourcesAvailability())
return -1;
//Some other code.........
}
But I don't think this is the cleanest way to do it. First we aren't sure that d3dx9.lib really inherits from D3DX9_43.dll (I know that because I used IDA PRO) and also the LoadLibrary function is called twice - one time at the CheckResourcesAvailability() function and second when the DLL is delay-loaded. Any ideas for a better implementation?
Use delay load hooks to let the delay loader notify you whenever a given DLL or a specific exported function is missing. Not only does that tell you which DLL/function is missing, but also lets you specify a substitute DLL/function if desired.

MFC Fail to Load Dlg from DLL

I have installed in my PC VS2008 and Windows Mobile 6 SDK.
I have made a SmartDevice MFC application and a Regular DLL MFC, both uses shared MFC DLL.
But when I called DoModal() of the DLL the application hangs, show a "Debug Assertion Failed" message and freeze my device.
Can you help me?
Codes:
The EXE code:
typedef BOOL (CALLBACK* LPFNDLLLOAD)();
typedef BOOL (CALLBACK* LPFNDLLRUN)(HINSTANCE, HWND, LPBYTE *, LONG *);
BOOL CTesteExeDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
//CModule mod;
//mod.Create(L"\\Program Files\\PMA\\Teste.dll");
//mod.Run(AfxGetInstanceHandle(), GetSafeHwnd(), 0, 0);
HMODULE m_hModule = AfxLoadLibrary(L"\\Program Files\\PMA\\TesteDll.dll");
LPFNDLLLOAD m_lpfnLoad= (LPFNDLLLOAD)GetProcAddress(m_hModule, _T("_Load"));
LPFNDLLRUN m_lpfnRun = (LPFNDLLRUN)GetProcAddress(m_hModule, _T("_Run"));
m_lpfnLoad();
m_lpfnRun(AfxGetInstanceHandle(), GetSafeHwnd(), 0, 0);
return TRUE; // return TRUE unless you set the focus to a control
}
The DLL Code:
I remove default CTesteDllApp class and put this:
#include "stdafx.h"
#include "TesteDll.h"
#include "TesteDllDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
extern "C" BOOL PASCAL EXPORT _Load()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
return TRUE;
}
extern "C" BOOL PASCAL EXPORT _Unload()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
return TRUE;
}
extern "C" BOOL WINAPI EXPORT _Run(HINSTANCE hInst,
HWND hwndParent,
LPBYTE *buffer,
LONG *size)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
CTesteDllDlg d;
d.DoModal(); ////-------------> Error Here
return FALSE;
}
The DLL Dlg code:
BOOL CTesteDllDlg::OnInitDialog()
{
CDialog::OnInitDialog();
AfxMessageBox(L"Oi");
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
The def File in DLL
; TesteDll.def : Declares the module parameters for the DLL.
LIBRARY "TesteDll"
EXPORTS
; Explicit exports can go here
_Load #1
_Unload #2
_Run #3
In a similar problem, I had to use AFX_MANAGE_STATE macro in the OnInitDialog, OnKillActive and OnSize methods of the DLL dialog. I had to add OnKillActive and OnSize methods just to call the mentioned macro, they do nothing but to call the macro, then base implementation, and return. Maybe it would work for your case.