API : modifier le nom d'une imprimante
Animateur WDF
Inscrit:
22/11/2004 11:23
Post(s): 255
Bonjour,

Est-ce que quelqu'un connaitrais la fonction à utiliser pour renommer une imprimante windows via API.
Je suppose que cette fonction doit se trouver dans l'API "winspool.drv". Je récupère le handle de mon imprimante mais après ?

Si quelqu'un connaissais la fonction.

Merci

Contribution le : 02/02/2005 14:25
Créer un fichier PDF de la contribution Imprimer


Re: API : modifier le nom d'une imprimante
Animateur WDF
Inscrit:
26/06/2002 16:24
De wdforge.org
Post(s): 2822
Encore une recherche

L'exemple de code suivant est en c++.
Il récupère le handle de l'imprimante via OpenPrinter().
Ses paramètres sont dans la structure PRINTER_INFO_2.
Utiliser alors la fonction GetPrinter.
Modifie ensuite les paramètres en créant une instance de la structure PRINTER_INFO_2.
Afecter les modifications par la fonction SetPrinter.
Enfin fermer l'imprimante avec la fonction ClosePrinter.

Je vous laisse traduire en WinDev...

void CRenamePrinterDlg::OnRenamePrinter()
{
HANDLE hPrinter = NULL;
DWORD dwNeeded = 0;
PRINTER_INFO_2 *pi2 = NULL;
DEVMODE *pDevMode = NULL;
PRINTER_DEFAULTS pd;
BOOL bFlag;
LONG lFlag;

// Open printer handle (on Windows NT, you need
full-access because you
// will eventually use SetPrinter)...
LPTSTR pPrinterName = "LASER-Printer" ;
ZeroMemory(&pd, sizeof(pd));
pd.DesiredAccess = PRINTER_ALL_ACCESS;

bFlag = OpenPrinter(pPrinterName, &hPrinter, &pd);
if (!bFlag || (hPrinter == NULL))
return ;

// The first GetPrinter tells you how big the buffer
should be in
// order to hold all of PRINTER_INFO_2. Note that
this should fail with
// ERROR_INSUFFICIENT_BUFFER.  If GetPrinter fails
for any other reason
// or dwNeeded isn't set for some reason, then there
is a problem...
SetLastError(0);
bFlag = GetPrinter(hPrinter, 2, 0, 0, &dwNeeded);
         if ((!bFlag) && (GetLastError() !=
ERROR_INSUFFICIENT_BUFFER) ||
         (dwNeeded == 0))
{
ClosePrinter(hPrinter);
return ;
}

// Allocate enough space for PRINTER_INFO_2...
pi2 = (PRINTER_INFO_2 *)GlobalAlloc(GPTR, dwNeeded);
if (pi2 == NULL)
{
ClosePrinter(hPrinter);
return ;
}

// The second GetPrinter fills in all the current
settings, so all you
// need to do is modify what you're interested in...
bFlag = GetPrinter(hPrinter, 2, (LPBYTE)pi2,
dwNeeded, &dwNeeded);
CString bval ;
bval.Format(" GetPrinter: %d",bFlag) ;
AfxMessageBox(bval) ;

if (!bFlag)
{
GlobalFree(pi2);
ClosePrinter(hPrinter);
return ;
}

// If GetPrinter didn't fill in the DEVMODE, try to
get it by calling
// DocumentProperties...
if (pi2->pDevMode == NULL)
{
dwNeeded = DocumentProperties(NULL, hPrinter,
pPrinterName,
NULL, NULL, 0);
if (dwNeeded <= 0)
{
GlobalFree(pi2);
ClosePrinter(hPrinter);
return ;
}

pDevMode = (DEVMODE *)GlobalAlloc(GPTR, dwNeeded);
if (pDevMode == NULL)
{
GlobalFree(pi2);
ClosePrinter(hPrinter);
return ;
}

lFlag = DocumentProperties(NULL, hPrinter,
pPrinterName,
pDevMode, NULL,
DM_OUT_BUFFER);
if (lFlag != IDOK || pDevMode == NULL)
{
GlobalFree(pDevMode);
GlobalFree(pi2);
ClosePrinter(hPrinter);
return ;
}

pi2->pDevMode = pDevMode;
}

// Driver is reporting that it doesn't support this
change...
if (!(pi2->pDevMode->dmFields & DM_ORIENTATION))
{
GlobalFree(pi2);
ClosePrinter(hPrinter);
if (pDevMode)
GlobalFree(pDevMode);
return ;
}

// Specify exactly what we are attempting to
change...
/*pi2->pDevMode->dmFields = DM_ORIENTATION;
pi2->pDevMode->dmOrientation = dmOrientation;*/
//CTS_SR
LPTSTR lpPrinterName = "Hi! I am the new printer" ;


pi2->pPrinterName = lpPrinterName ;

// Do not attempt to set security descriptor...
pi2->pSecurityDescriptor = NULL;

// Make sure the driver-dependent part of devmode is
updated...
lFlag = DocumentProperties(NULL, hPrinter,
  pPrinterName,
  pi2->pDevMode, pi2->pDevMode,
  DM_IN_BUFFER | DM_OUT_BUFFER);
if (lFlag != IDOK)
{
GlobalFree(pi2);
ClosePrinter(hPrinter);
if (pDevMode)
GlobalFree(pDevMode);
return ;
}

// Update printer information...
bFlag = SetPrinter(hPrinter, 2, (LPBYTE)pi2, 0);
if (!bFlag)
// The driver doesn't support, or it is unable to
make the change...
{
GlobalFree(pi2);
ClosePrinter(hPrinter);
if (pDevMode)
GlobalFree(pDevMode);
return ;
}

// Tell other apps that there was a change...
SendMessageTimeout(HWND_BROADCAST, WM_DEVMODECHANGE,
0L,
  (LPARAM)(LPCSTR)pPrinterName,
  SMTO_NORMAL, 1000, NULL);

// Clean up...
if (pi2)
GlobalFree(pi2);
if (hPrinter)
ClosePrinter(hPrinter);
if (pDevMode)
GlobalFree(pDevMode);

return ;

}

Contribution le : 02/02/2005 15:01
_________________
R&B
Contact, CV.
Créer un fichier PDF de la contribution Imprimer



 Haut   Précédent   Suivant




Enregistrer votre réponse
CompteNom   Mot de passe   Authentification
Message:


Vous ne pouvez pas débuter de nouveaux sujets.
Vous pouvez voir les sujets.
Vous ne pouvez pas répondre aux contributions.
Vous ne pouvez pas éditer vos contributions.
Vous ne pouvez pas effacez vos contributions.
Vous ne pouvez pas ajouter de nouveaux sondages.
Vous ne pouvez pas voter en sondage.
Vous ne pouvez pas attacher des fichiers à vos contributions.
Vous ne pouvez pas poster sans approbation.

[Recherche avancée]


Connexion
Menu
Chercher WDForge
Chercher Web
Partenaires
Visualiser tous les Partenaires...
WinDev, WebDev, WinDev Mobile et HyperFile sont des marques déposées par PCSoft. |  Voter |  Legal |  Contact |   XOOPS 2.0.13.2