简体   繁体   English

为什么需要在FormatMessage中强制转换lpBuffer(LPTSTR)参数?

[英]Why do I need to cast the lpBuffer (LPTSTR) parameter in FormatMessage?

In Windows' FormatMessage() function, the parameter: 在Windows的FormatMessage()函数中,参数:

  _Out_     LPTSTR lpBuffer

Is doing my head in. Following along from Hart's Windows System Programming book, I'm declaring an LPTSTR pointer to be used as the lpBuffer (eg LPTSTR errortext; ), and then calling the FormatMessage() function. 尽我所能。从Hart的Windows系统编程书开始,我要声明一个LPTSTR指针用作lpBuffer (例如LPTSTR errortext; ),然后调用FormatMessage()函数。

The correct way to pass in this parameter is: (LPTSTR)&errorText 传递此参数的正确方法是: (LPTSTR)&errorText

This works fine. 这很好。 But I don't understand why I need to write (LPTSTR) . 但是我不明白为什么我需要写(LPTSTR) I understand that's typecasting and I read about it but it doesn't make sense to me, because I'm not changing the variable type or anything, I declared it as an LPTSTR and I'm passing its memory address to the function, the function expects an LPTSTR and I passed it an LPTSTR , so why do I need to put (LPTSTR) as part of the lpBuffer parameter? 我知道这是类型转换,但我对它没有了解,因为我没有更改变量类型或任何内容,我将其声明为LPTSTR ,并将其内存地址传递给函数,即函数需要一个LPTSTR并且我将它传递给LPTSTR ,那么为什么我需要将(LPTSTR)作为lpBuffer参数的一部分?

The parameter lpBuffer of FormatMessage() is documented as follows: FormatMessage()的参数lpBuffer记录如下:

A pointer to a buffer that receives the null-terminated string that specifies the formatted message. 指向缓冲区的指针,该缓冲区接收以空终止的字符串,该字符串指定格式化的消息。 If dwFlags includes FORMAT_MESSAGE_ALLOCATE_BUFFER, the function allocates a buffer using the LocalAlloc function, and places the pointer to the buffer at the address specified in lpBuffer. 如果dwFlags包含FORMAT_MESSAGE_ALLOCATE_BUFFER,则该函数使用LocalAlloc函数分配缓冲区,并将指向该缓冲区的指针放在lpBuffer中指定的地址处。

So there are 2 different usages of FormatMessage() , 因此, FormatMessage()有2种不同的用法,

1) Provide your own buffer 1)提供您自己的缓冲区

const DWORD bufsize = ....;
TCHAR buf[bufsize];
FormatMessage(.... buf, bufsize, ....); // buf is passed as a TCHAR*

2) FormatMessage allocates a buffer for you 2)FormatMessage为您分配一个缓冲区

const DWORD bufsize = ....;
TCHAR* buf = 0;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | ....,
    .... (LPTSTR)&buf, bufsize, ....); // &buf is a TCHAR** so type-cast needed!
....
LocalFree(buf);

In #1, you have to pass the address of the first TCHAR in your buffer, and the function simply fills it the buffer. 在#1中,您必须在缓冲区中传递第一个TCHAR的地址,该函数只是将其填充到缓冲区中。

In #2, the function needs to tell you where it allocates a new buffer, so you have to tell it where to place that address. 在#2中,该函数需要告诉您它在哪里分配新缓冲区,因此您必须告诉它将该地址放在何处。 You have to pass the address of a pointer variable that receives the address. 您必须传递一个接收该地址的指针变量的地址。

In short: 简而言之:

  • #1 needs a TCHAR* to an existing buffer #1需要一个TCHAR*到现有缓冲区
  • #2 needs a TCHAR** that receives a new buffer #2需要一个接收新缓冲区的TCHAR**

That is why the lpBuffer parameter has to be type-casted when using #2. 这就是使用#2时必须类型转换lpBuffer参数的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM