这是在 Windows 8 / Server 2012 及更高版本中重现加载程序锁定挂起的方法。请注意,此代码不是直接调用加载库,而是使用触发加载库调用的 Windows API。
创建一个 Visual Studio C++ DLL 项目并在 DLL main 中使用此代码:
#define WIN32_LEAN_AND_MEAN
#include "framework.h"
#include <windows.h>
#include <winsock2.h>
#include <iphlpapi.h>
#include <ws2tcpip.h>
#include <stdio.h>
#pragma comment(lib, "IPHLPAPI.lib")
#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
#define FREE(x) HeapFree(GetProcessHeap(), 0, (x))
// Need to link with Ws2_32.lib
#pragma comment(lib, "ws2_32.lib")
BOOL APIENTRY DllMain(HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
WORD wVersionRequested;
WSADATA wsaData;
int err;
/* Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h */
wVersionRequested = MAKEWORD(2, 2);
err = WSAStartup(wVersionRequested, &wsaData);
if (err != 0) {
printf("WSAStartup failed with error: %d\n", err);
return 1;
}
if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) {
printf("Could not find a usable version of Winsock.dll\n");
WSACleanup();
return 1;
}
else
printf("The Winsock 2.2 dll was found okay\n");
FIXED_INFO* pFixedInfo;
ULONG ulOutBufLen;
DWORD dwRetVal;
IP_ADDR_STRING* pIPAddr;
pFixedInfo = (FIXED_INFO*)MALLOC(sizeof(FIXED_INFO));
if (pFixedInfo == NULL) {
printf("Error allocating memory needed to call GetNetworkParams\n");
return 1;
}
ulOutBufLen = sizeof(FIXED_INFO);
// Make an initial call to GetAdaptersInfo to get
// the necessary size into the ulOutBufLen variable
if (GetNetworkParams(pFixedInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) {
FREE(pFixedInfo);
pFixedInfo = (FIXED_INFO*)MALLOC(ulOutBufLen);
if (pFixedInfo == NULL) {
printf("Error allocating memory needed to call GetNetworkParams\n");
return 1;
}
}
if (dwRetVal = GetNetworkParams(pFixedInfo, &ulOutBufLen) == NO_ERROR) {
printf("Host Name: %s\n", pFixedInfo->HostName);
printf("Domain Name: %s\n", pFixedInfo->DomainName);
printf("DNS Servers:\n");
printf("\t%s\n", pFixedInfo->DnsServerList.IpAddress.String);
pIPAddr = pFixedInfo->DnsServerList.Next;
while (pIPAddr) {
printf("\t%s\n", pIPAddr->IpAddress.String);
pIPAddr = pIPAddr->Next;
}
printf("Node Type: ");
switch (pFixedInfo->NodeType) {
case BROADCAST_NODETYPE:
printf("Broadcast node\n");
break;
case PEER_TO_PEER_NODETYPE:
printf("Peer to Peer node\n");
break;
case MIXED_NODETYPE:
printf("Mixed node\n");
break;
case HYBRID_NODETYPE:
printf("Hybrid node\n");
break;
default:
printf("Unknown node type %0lx\n", pFixedInfo->NodeType);
break;
}
printf("DHCP scope name: %s\n", pFixedInfo->ScopeId);
if (pFixedInfo->EnableRouting)
printf("Routing: enabled\n");
else
printf("Routing: disabled\n");
if (pFixedInfo->EnableProxy)
printf("ARP proxy: enabled\n");
else
printf("ARP Proxy: disabled\n");
if (pFixedInfo->EnableDns)
printf("DNS: enabled\n");
else
printf("DNS: disabled\n");
}
else {
printf("GetNetworkParams failed with error: %d\n", dwRetVal);
return 1;
}
if (pFixedInfo)
FREE(pFixedInfo);
//WSACleanup();
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
从第二个应用程序(尚未导入任何网络 API 或调用任何网络函数)创建一个包含以下代码的控制台桌面 C++ 应用程序:
HMODULE hModule;
hModule = LoadLibrary(L"<specify DLL created in previous example>"); // application will hang here