我正在尝试编写一个本地的Node插件,用于枚举Windows机器上的所有窗口,并将它们的标题以数组形式返回给JS端。但是,我被这个错误难住了:
也许我的方法有些幼稚。
据我所知,我没有执行数组的括号初始化?C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\xmemory0(655): error C3074: an array cannot be initialized with a parenthesized initializer [C:\xampp\htdocs\enum-windows\build\enumWindows.vcxproj]
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\xmemory0(773): note: see reference to function template instantiation 'void std::allocator<_Ty>::construct<_Objty,char(&)[255]>(_Objty (*),char (&)[255])' being comp iled with [ _Ty=char [255], _Objty=char [255] ]
#include <vector>
#include <node.h>
#include <v8.h>
#include <windows.h>
#include <stdio.h>
using namespace node;
using namespace v8;
BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM ptr) {
std::vector<char[255]>* windowTitles =
reinterpret_cast<std::vector<char[255]>*>(ptr);
if (IsWindowVisible(hWnd)) {
int size = GetWindowTextLength(hWnd);
char buff[255];
GetWindowText(hWnd, (LPSTR) buff, size);
windowTitles->push_back(buff);
}
return true;
};
void GetWindowTexts(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = Isolate::GetCurrent();
HandleScope scope(isolate);
Local<Array> arr = Array::New(isolate);
std::vector<char[255]> windowTitles;
EnumWindows(
&EnumWindowsProc,
reinterpret_cast<LPARAM>(&windowTitles));
for (unsigned int i = 0; i < windowTitles.size(); i++) {
const char* ch = reinterpret_cast<const char*>(windowTitles.at(i));
Local<String> str = String::NewFromUtf8(isolate, ch);
arr->Set(i, str);
}
args.GetReturnValue().Set(arr);
}
void init(Handle<Object> exports, Handle<Object> module) {
NODE_SET_METHOD(module, "exports", GetWindowTexts);
}
NODE_MODULE(enumWindows, init);
我认为错误与这行代码有关:
windowTitles->push_back(buff);
也许我的方法有些幼稚。
std::string
的向量。在C++11中,可以安全地使用&s[0]
将字符串数据写入。此外,对于LPSTR
的强制转换看起来非常可疑。在您的情况下,强制转换什么也没做。 - chrisconst char* ch = reinterpret_cast<const char*>(windowTitles.at(i));
看起来没问题。我发现你可以通过检查以下编译器错误来查看哪一行导致了模板实例化错误。所以它将从xmemory开始,就像这种情况一样,然后可能会跟随另一个标准库的另一行,但最终你会在你的源文件中得到一个行号。只需继续阅读即可。一旦你看到这一行,就变得更容易了。 - wallywindowTitles->push_back(buff);
这一行。 - wally