简体   繁体   English

LoadStringW-winuser.h。 它有什么作用?

[英]LoadStringW - winuser.h. What does it do?

I have been unable to find any decent documentation on this function. 我一直找不到有关此功能的任何体面的文档。 The code base I am working with uses a function from winuser.h called LoadStringW which takes as arguments: (HINSTANCE hInstance, UINT uID, LPWSTR lpBuffer, int cchBufferMax) . 我正在使用的代码库使用winuser.h中的一个名为LoadStringW的函数作为参数: (HINSTANCE hInstance, UINT uID, LPWSTR lpBuffer, int cchBufferMax)

What is this function? 这个功能是什么? What is it for? 这是为了什么 When might it return 0? 什么时候可以返回0?

It might be worth a mention that nearly all Win32 APIs that deal with strings have an 'A' and a 'W' variant. 值得一提的是,几乎所有处理字符串的Win32 API都有一个“ A”和“ W”变体。

The variant actually called is determined by the definition of macros that don't end in 'A' or 'W' - those macro names are what you might usually think of as the API function's name ( LoadString() in this case). 实际调用的变体由不以'A'或'W'结尾的宏定义所决定-这些宏名通常是您通常认为的API函数名称(在这种情况下为LoadString() )。 UNICODE builds will use the 'W' names and non-UNICODE builds will use the 'A' names. UNICODE版本将使用“ W”名称,非UNICODE版本将使用“ A”名称。

There are times when you might want to call a Unicode version of an API even if the build isn't Unicode, in which case you just directly use the name with the 'W' tacked on to the end (it's less often necessary to need to call the non-Unicode APIs in a Unicode build, but it's just as possible). 有时,即使构建版本不是Unicode,也可能要调用API的Unicode版本,在这种情况下,您只需直接使用名称,并在其末尾加上“ W”即可(不需要时通常不需要在Unicode版本中调用非Unicode API,但这是尽可能的)。 Since the non-Unicode versions of Windows are obsolete, Microsoft has started more and more to implement only Unicode versions of APIs. 由于Windows的非Unicode版本已过时,因此Microsoft已经开始越来越多地仅实现API的Unicode版本。 Note that in nearly all cases, all that the non-Unicode versions of the APIs do is to convert the ANSI/MBCS strings to Unicode, call the 'W' function, then clean up afterward. 请注意,在几乎所有情况下,API的非Unicode版本所做的全部工作就是将ANSI / MBCS字符串转换为Unicode,调用“ W”函数,然后进行清理。

Here is the documentation for LoadString(): 这是LoadString()的文档: http://msdn.microsoft.com/en-us/library/ms647486%28VS.85%29.aspx http://msdn.microsoft.com/zh-CN/library/ms647486%28VS.85%29.aspx

.. and here is the documentation explaining the differences between ANSI and Unicode functions in the Windows API: ..这是说明Windows API中ANSI和Unicode函数之间差异的文档: http://msdn.microsoft.com/en-us/library/cc500321.aspx . http://msdn.microsoft.com/zh-cn/library/cc500321.aspx

Basically, the function LoadString comes in two flavours, ANSI and Unicode. 基本上,函数LoadString具有两种样式:ANSI和Unicode。 LoadStringW is the Unicode-specific version of LoadString. LoadStringW是LoadString的Unicode特定版本。

Edit: Just to be clear, there aren't really two completely separate functions. 编辑:需要明确的是,实际上并没有两个完全独立的功能。 The ANSI version really just converts the string and calls the unicode version, which does all of the real work. ANSI版本实际上只是转换字符串并调用unicode版本,它可以完成所有实际工作。

LoadStringW is the Unicode version of LoadString . LoadStringW是Unicode版本加载链

The documentation states "If the function succeeds, the return value is the number of TCHARs copied into the buffer, not including the terminating NULL character, or zero if the string resource does not exist. To get extended error information, call GetLastError ." 文档指出“如果函数成功,则返回值是复制到缓冲区中的TCHAR的数量,不包括终止NULL字符;如果字符串资源不存在,则返回零。要获取扩展的错误信息,请调用GetLastError 。”

LoadStringW() is the WideCharacter version of the LoadString function. LoadStringW()是LoadString函数的WideCharacter版本。

See MSDN 参见MSDN

It loads a widestring from a stringtable resource using the Windows Unicode Layer for Win95 and NT 3.51. 它使用Windows Unicode层Win95和NT 3.51从可字符串化的资源中加载一个宽字符串。 See MSDN for details (see the remarks section). 有关详细信息,请参见MSDN (请参阅备注部分)。

For the umpteenth time, I just confirmed that when the resource compiler is instructed to null terminate the strings , the count returned by LoadString includes the terminal NULL character. 第三次,我只是确认, 当指示资源编译器将字符串终止为null时LoadString返回的计数包括终端NULL字符。 I did so by examining the output buffer that I made available to LoadString . 我通过检查可用于LoadString的输出缓冲区来做到这一点。

Resource strings are not null terminated by default. 资源字符串默认情况下不为 null终止。 In that case, the returned count excludes the terminal null character, as described in the documentation, because the null is appended by the function after the string is copied into the output buffer. 在那种情况下,返回的计数不包括终端空字符,如文档中所述,因为在将字符串复制到输出缓冲区之后,该函数会附加空值。

I suspect this behavior is due to the fact that LoadString disregards the fact that the resource compiler was instructed to null terminate the strings. 我怀疑这种行为是由于LoadString忽略了资源编译器被指示以null终止字符串的事实。 Indeed, I suspect that it has no way of knowing that they were. 的确,我怀疑无法知道它们是什么。

With respect to why you would want to null terminate your resource strings in the first place, when they work just fine without them, and your PE file is thereby a tad smaller, the reason is that the wide character implementation of LoadString , at the LoadStringW entry point, returns a pointer to the string, rather than copying it into a buffer, if the buffer address passed into it is a NULL pointer. 关于为什么首先要使资源字符串为null终止的原因,当资源字符串在没有它们的情况下也可以正常工作,因此PE文件要小一点时,原因是LoadString的宽字符实现在LoadStringW入口点,如果传递给该字符串的缓冲区地址是NULL指针,则返回指向该字符串的指针 ,而不是将其复制到缓冲区中。 Unless your strings are null terminated, using LoadString in this way produces quite unwelcome results. 除非您的字符串以null结尾,否则以这种方式使用LoadString会产生非常不受欢迎的结果。

Since resource strings are always stored as Unicode (wide character) strings, the ANSI implementation of LoadString cannot return a pointer, as the string must be converted to ANSI; 由于资源字符串始终以Unicode(宽字符)字符串存储,因此LoadString的ANSI实现无法返回指针,因为必须将字符串转换为ANSI。 hence, it cannot simply be copied. 因此,不能简单地复制它。

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

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