f



Problem changing default printer

I have to add one or more printers to to the profile of a user logged onto a
remote computer.  I've done this by updating the HKCU registry settings.
This part works fine.   I also have installed a utility on the remote
computer that the user will execute to change the default printer to one of
the newly installed printers.  This is where I encounter problems.  When the
use executes the utility, it will NOT change the default printer.  But if
the user restarts the computer and executes the utility, the default printer
will be successfully changed.

So, my question is why the computer has to be rebooted in order to change
the default printer?

The code I'm using is as follows which is run on the local machine by the
logged on user.

VOID ChangeDefaultPrinter(VOID)
{
    char szLclSrvrbuffer[MAX_PATH + 1] = {0};
    char szDefaultPrinter[MAX_PATH + 1] = {0};
    char szPrnName[128]= {0};
    ExpandEnvironmentStrings("%LCLSRVR%",szLclSrvrbuffer,sizeof(szLclSrvrbuffer));
    ExpandEnvironmentStrings("%DEFPTR%",szDefaultPrinter,sizeof(szDefaultPrinter));


    sprintf(szPrnName, "\\\\%s\\%s", szLclSrvrbuffer,szDefaultPrinter);
    typedef BOOL ( WINAPI *SETDEFPRN)(LPCSTR );
    HINSTANCE hSpoolib = LoadLibrary("winspool.drv");
    if(hSpoolib)
    {
        SETDEFPRN SetDefPrn =
(SETDEFPRN)GetProcAddress(hSpoolib,_T("SetDefaultPrinterA"));
        if(SetDefPrn)
        {
            if(!SetDefPrn((LPCSTR)szPrnName))
            {
                printf("Failed to set default printer\n");
            }
            else printf("Default printer is set\n");
        }
        else
        {
            printf("Default printer NOT found\n");
        }
        FreeLibrary(hSpoolib);
    }
    else
    {
        printf("LoadLibrary failed\n");
    }
}



0
BobS0327
4/4/2009 1:12:44 AM
comp.os.programmer.win32 14523 articles. 0 followers. Post Follow

2 Replies
434 Views

Similar Articles

[PageSpeed] 56

BobS0327 wrote:
> I have to add one or more printers to to the profile of a user logged onto a
> remote computer.  I've done this by updating the HKCU registry settings.
> This part works fine.   I also have installed a utility on the remote
> computer that the user will execute to change the default printer to one of
> the newly installed printers.  This is where I encounter problems.  When the
> use executes the utility, it will NOT change the default printer.  But if
> the user restarts the computer and executes the utility, the default printer
> will be successfully changed.
> 
> So, my question is why the computer has to be rebooted in order to change
> the default printer?
> 
> The code I'm using is as follows which is run on the local machine by the
> logged on user.

Have you seen the KB246772 ?
(WM_SETTINGCHANGE, ..)
0
Christian
4/4/2009 2:04:42 AM
"Christian ASTOR" <castorix@club-internet.fr> wrote in message 
news:49d6bfd1$0$21741$7a628cd7@news.club-internet.fr...
> BobS0327 wrote:
>> I have to add one or more printers to to the profile of a user logged 
>> onto a
>> remote computer.  I've done this by updating the HKCU registry settings.
>> This part works fine.   I also have installed a utility on the remote
>> computer that the user will execute to change the default printer to one 
>> of
>> the newly installed printers.  This is where I encounter problems.  When 
>> the
>> use executes the utility, it will NOT change the default printer.  But if
>> the user restarts the computer and executes the utility, the default 
>> printer
>> will be successfully changed.
>>
>> So, my question is why the computer has to be rebooted in order to change
>> the default printer?
>>
>> The code I'm using is as follows which is run on the local machine by the
>> logged on user.
>
> Have you seen the KB246772 ?
> (WM_SETTINGCHANGE, ..)

Yep, KB246772 addresses and resolves my problem.

Thanx 


0
BobS0327
4/5/2009 12:44:29 PM
Reply: