缓冲区溢出问题 VC++

发布于 2024-08-29 14:05:48 字数 473 浏览 7 评论 0原文

当我执行代码时,出现此错误

LPTSTR lpBuffer;
::GetLogicalDriveStrings(1024,lpBuffer);
while(*lpBuffer != NULL)
{
  printf("%s\n", lpBuffer); // or MessageBox(NULL, temp, "Test", 0); or whatever
  lpBuffer += lstrlen(lpBuffer)+1;
  printf("sizeof(lpBuffer) %d\n",lstrlen(lpBuffer));
}

OutPut

C

sizeof(lpBuffer) 3

D

sizeof(lpBuffer) 3

E

sizeof(lpBuffer) 3

F

sizeof(lpBuffer) 0

When i execute my code i am getting this error

LPTSTR lpBuffer;
::GetLogicalDriveStrings(1024,lpBuffer);
while(*lpBuffer != NULL)
{
  printf("%s\n", lpBuffer); // or MessageBox(NULL, temp, "Test", 0); or whatever
  lpBuffer += lstrlen(lpBuffer)+1;
  printf("sizeof(lpBuffer) %d\n",lstrlen(lpBuffer));
}

OutPut

C

sizeof(lpBuffer) 3

D

sizeof(lpBuffer) 3

E

sizeof(lpBuffer) 3

F

sizeof(lpBuffer) 0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

春花秋月 2024-09-05 14:05:48

lpBuffer指向随机内存。你需要这样的东西:

LPTSTR lpBuffer = new TCHAR[1025];

编辑:将数组大小更正为1025而不是1024,因为长度参数是1024。这个API需要仔细阅读。

lpBuffer points to random memory. You need something like this:

LPTSTR lpBuffer = new TCHAR[1025];

edit: Corrected the array size to be 1025 instead of 1024, because the length parameter is 1024. This API requires careful reading.

绝不放开 2024-09-05 14:05:48

您应该传递一个将复制字符串的内存地址。但是您还没有分配任何空间来容纳字符。您需要先分配空间,然后再将其传递给 GetLogicalDriveStrings 函数。您可以按照 @Windows 程序员的建议在堆上分配内存,或者如果在编译时已知字符串的最大长度,您可以使用 TCHAR lpBuffer[1024]; 在堆栈上分配它此外,您正在使用printf 打印 unicode(可能取决于编译器标志)。这不起作用,只会打印第一个字符。

You are supposed to pass a memory address where the string will be copied. However you have not allocated any space for holding the characters. You need to allocate space before passing it to the GetLogicalDriveStrings function. You can allocate the memory on heap as @Windows programmer suppgested or if the maximum length of the string is known at compile time you can allocate it on stack using TCHAR lpBuffer[1024]; Additinally, you are using printf to print the unicode (may be as it depends on compiler flag). This will not work and will print only first character.

人疚 2024-09-05 14:05:48

您需要实际传入一个缓冲区 - 请注意,您传入的缓冲区的大小需要比缓冲区的实际大小小一,以考虑最终的终止 '\0' 字符(我不知道为什么 API就是这样设计的)。

这是您的示例的稍微修改的版本:

#include <windows.h>
#include <tchar.h>
#include <stdio.h>

enum {
    BUFSIZE = 1024
};

int _tmain (int argc, TCHAR *argv[])
{
    TCHAR szTemp[BUFSIZE];
    LPTSTR lpBuffer = szTemp;   // point lpBuffer to the buffer we've allocated


    szTemp[0] = _T( '\0');  // I'm not sure if this is necessary, but it was
                            //   in the example given for GetLogicalDriveStrings()

    GetLogicalDriveStrings( BUFSIZE-1, lpBuffer);   // note: BUFSIZE minus 1

    while(*lpBuffer != _T('\0'))
    {
      _tprintf( _T("%s\n"), lpBuffer);
      lpBuffer += lstrlen(lpBuffer)+1;
      _tprintf( _T("length of lpBuffer: %d\n"),lstrlen(lpBuffer));
    }

    return 0;
}

You need to actually pass in a buffer - note that the size of the buffer you pass in needs to be one less than the actual size of the buffer to account for the final terminating '\0' character (I have no idea why the API was designed like that).

Here's a slightly modified version of your example:

#include <windows.h>
#include <tchar.h>
#include <stdio.h>

enum {
    BUFSIZE = 1024
};

int _tmain (int argc, TCHAR *argv[])
{
    TCHAR szTemp[BUFSIZE];
    LPTSTR lpBuffer = szTemp;   // point lpBuffer to the buffer we've allocated


    szTemp[0] = _T( '\0');  // I'm not sure if this is necessary, but it was
                            //   in the example given for GetLogicalDriveStrings()

    GetLogicalDriveStrings( BUFSIZE-1, lpBuffer);   // note: BUFSIZE minus 1

    while(*lpBuffer != _T('\0'))
    {
      _tprintf( _T("%s\n"), lpBuffer);
      lpBuffer += lstrlen(lpBuffer)+1;
      _tprintf( _T("length of lpBuffer: %d\n"),lstrlen(lpBuffer));
    }

    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文