Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Implementing Wireless Print for WinNT/Win2K


November 2001/Implementing Wireless Print for WinNT/Win2K

Listing 2: config.c
Contains four port-management functions: adding, deleting, configuring, and enumerating

/*------------------------------------------------------------------
config.c(part): Adding, deleting, configuring, and enumerating ports
Author:         Zuoliu Ding
------------------------------------------------------------------*/

BOOL AddPort(LPWSTR pName, HWND hWnd, LPWSTR pMonitorName)
{
    LPWSTR pPortName;
    DWORD  ThreadId, WindowThreadId, Error; 
    BOOL   rc = TRUE, DoAddPort = TRUE;
    WCHAR  szMonitor[MAX_PATH+1];

    LoadString(hInst, IDS_WLSMONITORPORT, szMonitor, MAX_PATH);
    if (_wcsicmp(pMonitorName, szMonitor)) { 
        SetLastError(ERROR_INVALID_PARAMETER);
        return FALSE;                    // not a Wireless Port
    }

    ThreadId = GetCurrentThreadId( );
    WindowThreadId = GetWindowThreadProcessId(hWnd, NULL);

    if (AttachThreadInput(ThreadId, WindowThreadId, TRUE))
    {
        pPortName = GetPortName(hWnd); // Get a port name from user
        if (pPortName)
        {
            if (PortExists(pName, pPortName, &Error))
            {
                Message(hWnd, MSG_ERROR, IDS_WLSMONITORPORT,
                        IDS_PORTALREADYEXISTS_S, pPortName );
                DoAddPort = FALSE; // error was handled, return true
            }
            else if (Error != NO_ERROR)
            {
                DoAddPort = FALSE;
                rc = FALSE;
            }

            if (DoAddPort && IsWLSPort(pPortName))
            {
                CharUpperBuff(pPortName, 3);
                if (RegWritePort(pPortName, L"::120")) 
                {   
                    if (!ConfigurePort(NULL, hWnd, pPortName) ||  
                        !CreatePortEntry(pPortName))   
                    {     
                        RegDeletePort(pPortName);
                        rc = FALSE; 
                    } 

                }
            }
        }  // if( pPortName )

        AttachThreadInput(WindowThreadId, ThreadId, FALSE);
    }

    return rc;
}

BOOL DeletePort(LPWSTR pName, HWND hWnd, LPWSTR pPortName)
{
    BOOL rc;
    EnterSplSem();
    if (rc=DeletePortEntry(pPortName)) RegDeletePort(pPortName);
    LeaveSplSem();

    return rc;
    UNREFERENCED_PARAMETER(pName);
}

BOOL ConfigurePort(LPWSTR pName, HWND hWnd, LPWSTR pPortName)
{
    DWORD  ThreadId, WindowThreadId;
    ThreadId = GetCurrentThreadId( );
    WindowThreadId = GetWindowThreadProcessId(hWnd, NULL);

    if (AttachThreadInput(ThreadId, WindowThreadId, TRUE))
    {
        if(IsWLSPort( pPortName ))
        {
           DialogBoxParam(hInst, MAKEINTRESOURCE(DLG_CONFIGURE_WCP),
                          hWnd,    (DLGPROC)ConfigurePortDlg, 
                          (LPARAM)&pPortName); 
        }
        else Message(hWnd, MSG_ERROR, IDS_WLSMONITORPORT, 
                     IDS_NOTHING_TO_CONFIGURE);

        AttachThreadInput(WindowThreadId, ThreadId, FALSE);
    }                                                            
    return TRUE;
}

BOOL EnumPorts(LPWSTR pName, DWORD Level, LPBYTE pPorts, 
               DWORD cbBuf, LPDWORD pcbNeeded, LPDWORD pcReturned)
{
    PINIPORT pIniPort;
    DWORD    cb=0, LastError=0;
    LPBYTE   pEnd;

    EnterSplSem();
    pIniPort=pIniFirstPort;

    while (pIniPort) {
        cb+=GetPortSize(pIniPort, Level);
        pIniPort=pIniPort->pNext;
    }

    *pcbNeeded=cb;

    if (cb <= cbBuf) 
    {
        pEnd=pPorts+cbBuf;
        *pcReturned=0;
        pIniPort=pIniFirstPort;

        while (pIniPort) 
        {
            pEnd = CopyIniPortToPort(pIniPort, Level, pPorts, pEnd);

            switch (Level) 
            {
                case 1: pPorts+=sizeof(PORT_INFO_1);
                        break;
                case 2: pPorts+=sizeof(PORT_INFO_2);
                        break;
                default: LastError = ERROR_INVALID_LEVEL;
                         goto Cleanup;
            }

            (*pcReturned)++;
            pIniPort=pIniPort->pNext;
        }

    } 
    else LastError = ERROR_INSUFFICIENT_BUFFER;

Cleanup:
    LeaveSplSem();

    if (LastError) {
        SetLastError(LastError);
        return FALSE;
    } 
    else return TRUE;
}
/* End of File */

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.