⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 instlsp.cpp

📁 < WINDOWS网络编程>>英文版,一本详细讲解WINDOWS平台下网络编程的国外经典书籍,适合英文水平高的牛人
💻 CPP
📖 第 1 页 / 共 2 页
字号:
            // Push protocol entries down the protocol chain
            for (k = OriginalProtocolInfo[i].ProtocolChain.ChainLen; k > 0; k--)
            {
                OriginalProtocolInfo[i].ProtocolChain.ChainEntries[k] = OriginalProtocolInfo[i].ProtocolChain.ChainEntries[k-1];
            }
        }
        // Insert our layered provider into the catalog and increment the count
        //
        OriginalProtocolInfo[i].ProtocolChain.ChainLen++;
        OriginalProtocolInfo[i].ProtocolChain.ChainEntries[0] = LayeredCatalogId;
    }
    //
    // For each entry that we're layering over, we need a GUID so create one and
    //  install the layer.
    //
    for(i=0; i < CatalogIdArrayCount ;i++)
    {
        if (UuidCreate(&ProviderChainGuid) != RPC_S_OK)
        {
            printf("UuidCreate() failed: %d\n", GetLastError());
        }

        if (WSCInstallProvider(&ProviderChainGuid, 
                                PROVIDER_PATH, 
                                &OriginalProtocolInfo[i], 
                                1, 
                               &ErrorCode) == SOCKET_ERROR)
        {
            printf("WSCInstallProvider for protocol chain failed %d\n", ErrorCode);
            return -1;
        }
        else
            printf("Installing layer: %S\n", OriginalProtocolInfo[i].szProtocol);
    }
	FreeProviders(ProtocolInfo);

	ProtocolInfo = GetProviders(&TotalProtocols);
    //
    // Set the provider order
    //
	if ((CatalogEntries = (LPDWORD) GlobalAlloc(GPTR, TotalProtocols * sizeof(DWORD))) == NULL)
	{
		printf("GlobalAlloc failed %d\n", GetLastError());
		return -1;
	}
    printf("Reordering catalog...\n");
    //
	// Find our provider entries and put them first in the list
    //
	CatIndex = 0;
	for (j = 0; j < TotalProtocols; j++)
    {
        if (IsIdinChain(&ProtocolInfo[j], LayeredCatalogId))
        {
			CatalogEntries[CatIndex++] = ProtocolInfo[j].dwCatalogEntryId;
        }
    }
	// Put all other entries at the end
    //
	for (j = 0; j < TotalProtocols; j++)
    {
        if (!IsIdinChain(&ProtocolInfo[j], LayeredCatalogId))
        {
			CatalogEntries[CatIndex++] = ProtocolInfo[j].dwCatalogEntryId;
        }
    }

	if ((ErrorCode = WSCWriteProviderOrder(CatalogEntries, TotalProtocols)) != ERROR_SUCCESS)
	{
		printf("WSCWriteProviderOrder failed %d\n", ErrorCode);
		return -1;
	}

	FreeProviders(ProtocolInfo);

    return 0;
}

//
// Function: RemoveIdFromChain
//
// Description:
//    This function removes the given CatalogId from the protocol chain 
//    for pinfo.
//
int RemoveIdFromChain(DWORD CatalogId, WSAPROTOCOL_INFOW *pinfo)
{
    int     i, j;

    for(i=0; i < pinfo->ProtocolChain.ChainLen ;i++)
    {
        if (pinfo->ProtocolChain.ChainEntries[i] == CatalogId)
        {
            for(j=i; j < pinfo->ProtocolChain.ChainLen-1 ; j++)
            {
                pinfo->ProtocolChain.ChainEntries[j] = pinfo->ProtocolChain.ChainEntries[j+1];
            }
            pinfo->ProtocolChain.ChainLen--;
            return 0;
        }
    }
    return 1;
}

//
// Function: IsIdinChain
//
// Description:
//    This function determines whether the given catalog id is referenced
//    in the protocol chain of pinfo.
//
BOOL IsIdinChain(WSAPROTOCOL_INFOW *pinfo, DWORD Id)
{
    for(int i=0; i < pinfo->ProtocolChain.ChainLen ;i++)
    {
        if (pinfo->ProtocolChain.ChainEntries[i] == Id)
            return TRUE;
    }
    return FALSE;
}

//
// Function: RemoveProvider
//
// Description:
//    This function removes a layered provider. Things can get tricky if
//    we're removing a layered provider which has been layered over by 
//    another provider.
//
void RemoveProvider(void)
{
    WSAPROTOCOL_INFOW *CleanupProtoInfo;
    WCHAR              wszProviderPath[DEFAULT_PATH_LEN];
    GUID              *GuidOrder=NULL;
	DWORD             *ProtoOrder,
                       LayeredCatalogId,
                       CleanupCount;
	INT                wszProviderPathLen,
                       ErrorCode,
                       idx,
                       i, j;

    ProtocolInfo = GetProviders(&TotalProtocols);
    //
    // Find our provider's catalog ID
    //
    for(i=0; i < (INT)TotalProtocols ;i++)
    {
		if (memcmp (&ProtocolInfo[i].ProviderId, &ProviderGuid, sizeof (GUID))==0)
		{
			LayeredCatalogId = ProtocolInfo[i].dwCatalogEntryId;
			break;
		}
    }
    //
    // Remove our layered entries in which we are the first entries in the
    //  protocol chain.
    //
    for(i=0; i < TotalProtocols ;i++)
    {
        if ((ProtocolInfo[i].ProtocolChain.ChainLen > 1) && 
            (ProtocolInfo[i].ProtocolChain.ChainEntries[0] == LayeredCatalogId))
        {
            if (WSCDeinstallProvider(&ProtocolInfo[i].ProviderId, &ErrorCode) == SOCKET_ERROR)
            {
                printf("RemoveProvider: WSCDeinstallProvider() failed [%d] on %S\n",
                    ErrorCode,
                    ProtocolInfo[i].szProtocol);
            }
        }
    }
    //
    // Remove our dummy (hidden) provider.
    //
	if (WSCDeinstallProvider(&ProviderGuid, &ErrorCode) == SOCKET_ERROR)
	{
		printf("WSCDeistallProvider for Layer failed %d\n", ErrorCode);
	}

    FreeProviders(ProtocolInfo);
    ProtocolInfo = GetProviders(&TotalProtocols);

    CleanupCount = 0;
    GuidOrder = (GUID *)GlobalAlloc(GPTR, sizeof(GUID) * TotalProtocols);
    if (!GuidOrder)
    {
        printf("RemoveProvider: GlobalAlloc() failed: %d\n", GetLastError());
        return;
    }
    // Save off the ordering of the catalog
    //
    for(i=0; i < TotalProtocols ;i++)
    {
        memcpy(&GuidOrder[i], &ProtocolInfo[i].ProviderId, sizeof(GUID));
        //
        // Count how many protocol entries reference the just removed
        //  provider (these are layered providers layered over our layer).
        //  
        for(j=0; j < ProtocolInfo[i].ProtocolChain.ChainLen ;j++)
        {
            if (ProtocolInfo[i].ProtocolChain.ChainEntries[j] == LayeredCatalogId)
            {
                CleanupCount++;
                break;
            }
        }
    }
    //
    // If this is greater than zero then we have other layered providers
    //  that reference our layer. We need to fix their protocol chains
    //  so they no longer reference us.
    //
    if (CleanupCount > 0)
    {
        printf("%d protocol entries layered over the removed provider\n", CleanupCount);

        CleanupProtoInfo = (WSAPROTOCOL_INFOW *)GlobalAlloc(GPTR, CleanupCount * sizeof(WSAPROTOCOL_INFOW));
        if (!CleanupProtoInfo)
        {
            printf("RemoveProvider: GlobalAlloc() failed: %d\n", GetLastError());
            return;
        }
        ProtoOrder = (DWORD *)GlobalAlloc(GPTR, TotalProtocols * sizeof(DWORD));
        if (!ProtoOrder)
        {
            printf("RemoveProviders: GlobalAlloc() failed: %d\n", GetLastError());
            return;
        }
        //
        // Copy off each entry which references the just removed layered provider
        //  and fix its protocol chain so it no longer references the removed
        //  provider.
        //
        idx = 0;
        for(i=0; i < TotalProtocols ;i++)
        {
            for(j=0; j < ProtocolInfo[i].ProtocolChain.ChainLen ;j++)
            {
                if (ProtocolInfo[i].ProtocolChain.ChainEntries[j] == LayeredCatalogId)
                {
                    memcpy(&CleanupProtoInfo[idx], &ProtocolInfo[i], sizeof(WSAPROTOCOL_INFOW));
                    RemoveIdFromChain(LayeredCatalogId, &CleanupProtoInfo[idx]);
                    idx++;
                    break;        
                }
            }
        }
        // Now we need to uninstall each of these entries and re-install with
        //  our copies (which no longer reference the removed provider)
        //
        for(i=0; i < idx ;i++)
        {
            // First we need to obtain the provider path since we'll need this
            // info when we re-install.
            //
            wszProviderPathLen = DEFAULT_PATH_LEN;
            if (WSCGetProviderPath(&CleanupProtoInfo[i].ProviderId,
                                   wszProviderPath,
                                   &wszProviderPathLen,
                                   &ErrorCode) == SOCKET_ERROR)
            {
                printf("WSCGetPathProvider() failed: %d\n", ErrorCode);
            }
            // Remove the provider
            //
            if (WSCDeinstallProvider(&CleanupProtoInfo[i].ProviderId, &ErrorCode) == SOCKET_ERROR)
            {
                printf("WSCDeinstallProvider() failed: %d\n", ErrorCode);
            }
            // Reinstall it
            //
            if (WSCInstallProvider(&CleanupProtoInfo[i].ProviderId,
                                    wszProviderPath,
                                   &CleanupProtoInfo[i],
                                    1,
                                   &ErrorCode) == SOCKET_ERROR)
            {
                printf("WSCInstallProvider() failed: %d\n", ErrorCode);
            }
        }

        FreeProviders(ProtocolInfo);

        GlobalFree(CleanupProtoInfo);

        ProtocolInfo = GetProviders(&TotalProtocols);
        //
        // Reorder the catalog back to the original way
        //
        for(i=0; i < TotalProtocols ;i++)
        {
            for(j=0; j < TotalProtocols ;j++)
            {
                if (!memcmp(&ProtocolInfo[j].ProviderId, &GuidOrder[i], sizeof(GUID)))
                {
                    ProtoOrder[i] = ProtocolInfo[j].dwCatalogEntryId;
                    memset(&ProtocolInfo[j].ProviderId, 0, sizeof(GUID));
                    break;
                }
            }
        }
        if ((ErrorCode = WSCWriteProviderOrder(ProtoOrder, TotalProtocols)) != ERROR_SUCCESS)
        {
            printf("WSCWriteProviderOrder() failed: %d\n", ErrorCode);
        }

        GlobalFree(ProtoOrder);
    }

    FreeProviders(ProtocolInfo);

    GlobalFree(GuidOrder);

    return;
}

//
// Function: RemoveAllLayeredEntries
//
// Description:
//    In the event that the layered entries become totally hosed up. This
//    function will remove any non base provider.
//
void RemoveAllLayeredEntries()
{
    BOOL    bLayer;
    int     ErrorCode,
            i;

    while (1)
    {
        bLayer = FALSE;
        ProtocolInfo = GetProviders(&TotalProtocols);
        if (!ProtocolInfo)
        {
            printf("Unable to enumerate Winsock catalog!\n");
            return;
        }
        for(i=0; i < TotalProtocols ;i++)
        {
            if (ProtocolInfo[i].ProtocolChain.ChainLen != BASE_PROTOCOL)
            {
                bLayer = TRUE;
                printf("Removing '%S'\n", ProtocolInfo[i].szProtocol);
                if (WSCDeinstallProvider(&ProtocolInfo[i].ProviderId, &ErrorCode) == SOCKET_ERROR)
                {
                    printf("Failed to remove [%s]: Error %d\n", ProtocolInfo[i].szProtocol, ErrorCode);
                }
                break;
            }
        }
        FreeProviders(ProtocolInfo);
        if (bLayer == FALSE)
        {
            break;
        }
    }
    return;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -