Required libraries: blicectr.lib and BlackIceDEVMODE.lib. Define WINDOWS_SERVER if you are using a printer driver for server operating systems.
#include "blicectr.h"
#include "BlackIceDEVMODE.h"
#define PRINTER_NAME _T("Black Ice ColorPlus")
#define WM_USER_BI_PRINTER (WM_USER+1000)
WCHAR szPipeName[256];
void StartCapture(HWND hWnd)
{
BlackIceDEVMODE* pDevMode = LoadBlackIceDEVMODE(PRINTER_NAME);
//define this if you are using the TS version of the printer driver for 2022/2019/2016/2012
#ifdef WINDOWS_SERVER
// tell the printer driver which session id should be used
DWORD dwSessionId;
ProcessIdToSessionId(GetCurrentProcessId(), &dwSessionId);
SetSessionID(pDevMode, dwSessionId);
SaveBlackIceDEVMODE(PRINTER_NAME, pDevMode);
_swprintf(szPipeName, L"\\\\.\\pipe\\%s%d", GetInterfaceName(pDevMode), dwSessionId);
#else
_swprintf(szPipeName, L"\\\\.\\pipe\\%s", GetInterfaceName(pDevMode));
#endif
ReleaseBlackIceDEVMODE(pDevMode);
// start capturing
WaitForPrnPipe(szPipeName, hWnd, WM_USER_BI_PRINTER);
}
void StopCapture()
{
EndWaitPrnPipe(szPipeName);
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
...
switch (message)
{
case WM_CREATE:
StartCapture(hWnd);
break;
case WM_USER_BI_PRINTER:
{
TSPrnMessage* pPrnMsg = (TSPrnMessage*)GlobalLock((HGLOBAL)wParam);
switch (pPrnMsg->dwMessage)
{
case BLACKICE_MESSAGE_DEVMODE:
{
BlackIceDEVMODE* pDevMode = (BlackIceDEVMODE*)lParam;
if(pDevMode)
{
// Set file name generation method to exact filename
SetFileGenerationMethod(FILENAME_EXACT_DEVMODE, pDevMode);
// Set the filename (you can give a full path here)
SetImageFileName(_T("name.pdf"), pDevMode);
}
break;
}
}
break;
}
case ...
}
...
}
Using Message Capture and Printing from the Same Process
If you wish to extend the Message Capture samples with printing using AutoPrint, or otherwise combine message capture and printing in the same application, the printing thread must be separated from the message capture thread.
Printing and capturing messages from the same thread will result in lost BLACKICE_MESSAGE_DEVMODE messages. Other messages may appear to work, but they will be delayed. This is because BLACKICE_MESSAGE_DEVMODE messages will wait for response and eventually time out, while other messages do not wait for response and are queued.