关于C ++:使用GetModuleFileNameW铸造错误

Casting error with GetModuleFileNameW

我正在尝试通过一些C ++代码(基本上是这样)来获取模块的版本信息,并且遇到了编译时错误。 这是代码:

1
2
3
WCHAR fileName[MAX_PATH];
HMODULE module = GetModuleHandle(L"some-module");
DWORD size = GetModuleFileName(module, fileName, MAX_PATH);

编译器返回:

1
2
3
    error C2664: 'GetModuleFileNameW' : cannot convert parameter 1 from 'WHANDLE' to
    'HMODULE'
    Conversion from 'void*' to pointer to non-'void' requires an explicit cast

这里发生了什么? GetModuleHandle返回HMODULE,这是GetModuleFileName记录的第一个参数。

这是其价值的编译器版本:

1
2
    Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.30319.01 for 80x86
    Copyright (C) Microsoft Corporation.  All rights reserved.

谢谢!


包含或库中一定有一些时髦的事情,因为它可以在这里编译并正常运行:

1
2
3
4
5
6
7
8
9
10
11
12
13
// Scratch.cpp : Defines the entry point for the console application.
//

#include"stdafx.h"
#include"windows.h"

int _tmain(int argc, _TCHAR* argv[])
{
    WCHAR fileName[MAX_PATH];
    HMODULE module = GetModuleHandle(L"some-module");
    DWORD size = GetModuleFileName(module, fileName, MAX_PATH);
    return 0;
}