47. FAQ o Win API

Q> Как поменять default принтер?

A> 
int CALLBACK WinMain( HINSTANCE hInst, HINSTANCE hPrevInst,
		LPSTR lpCmdLine, int nShowCmd )
{
	/* Get the port & device settings for the specified printer */
	char szDeviceString[260];

	/* First (and only) parameter is the printer name */
	lstrcpy( szDeviceString, __argv[1] );
	const int Pos = lstrlen( szDeviceString );

	/* Append a ',' */
	szDeviceString[ Pos ] = ',';

	GetProfileString( "Devices", __argv[1], "",
			&szDeviceString[Pos+1],
			sizeof( szDeviceString ) - (Pos+1) );

	/* Have we got the printer? */
	if ( szDeviceString[Pos+1] != '\0' )
	{
		/* Set the default printer */
		WriteProfileString( "windows", "device", szDeviceString );

		SendMessageTimeout( HWND_BROADCAST, WM_SETTINGCHANGE, 0L,
			(LPARAM)(LPCTSTR)"windows", SMTO_NORMAL,
			1000, NULL );
	}
	else
	{
		MessageBox( NULL, "Printer not found",
			"Set Default Printer Utility",
			MB_OK | MB_ICONERROR );
	}

	return 0;
}

References and samples:

Knowledge Base Article Q135387: "How to Get and Set the Default Printer in 
Windows" 

Knowledge Base Article Q140560 "How to Set the Default Printer Programmatically
in Windows 95" 

2000(c) DM

Электронные Книги по компьютерным программам
На правах рекламы:
Логин
Пароль
Запомнить меня