本文介绍了GetAdaptersInfo和GetAdaptersAddressess BufferLength参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这里有一些用C ++编写的遗留代码,可以完成一些我不了解的事情.我正在运行Windows XP的计算机上在Visual C ++ 2008 Express Edition中运行它.

I've got some legacy code in C++ here that does some things I don't understand. I'm running it in Visual C++ 2008 Express Edition on a machine running Windows XP.

代码使用一些Windows函数: GetAdaptersInfo 和GetAdaptersAddressess.我意识到这两个参数的最终参数都是指向缓冲区大小的指针,并且由于它是in_out,因此可以在函数中更改它.

The code uses some Windows functions: GetAdaptersInfo and GetAdaptersAddressess. I realize that the final parameter for both of these is a pointer to the size of the buffer and since it's in_out, it can be changed within the function.

我的问题是:这些函数是否应该更改缓冲区长度?

My question is: are these functions supposed to change the buffer length?

在我拥有的代码中,每次调用这些函数时,缓冲区长度变量都会初始化为零,并且在调用该函数后,它仍为0.

In the code I have, every time these functions are called the buffer length variable is initialized to zero, and after the function is called, it's still 0.

推荐答案

您的代码需要看起来像这样:

Your code needs to look something like this:

// First get the desired size.
unsigned long outBufLen = 0;
DWORD dwResult = GetAdaptersInfo(NULL, &outBufLen);
if (dwResult == ERROR_BUFFER_OVERFLOW)  // This is what we're expecting
{
    // Now allocate a structure of the requried size.
    PIP_ADAPTER_INFO pIpAdapterInfo = (PIP_ADAPTER_INFO) malloc(outBufLen);
    dwResult = GetAdaptersInfo(pIpAdapterInfo, &outBufLen);
    if (dwResult == ERROR_SUCCESS)
    {
        // Yay!

编辑:有关此代码为何不够充分的信息,另请参阅Jeremy Friesner的答案.

See also Jeremy Friesner's answer for why this code isn't quite enough.

这篇关于GetAdaptersInfo和GetAdaptersAddressess BufferLength参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 00:05