【发布时间】:2014-06-05 20:11:10
【问题描述】:
带有 VMWare 的 Mac 上的 Windows 7。 GetComputerName 返回 ERROR_BUFFER_OVERFLOW (111),但有效长度为 16 且名称为空。任何想法为什么它会失败?它可以与其他 Windows 仿真一起正常工作。
这是演示失败的控制台应用程序代码:
// GetComputerName.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
#include <string> // for string
#include <vector> // for vector
using std::vector;
using std::string;
#ifdef _UNICODE
using std::wstring;
#define string wstring
using std::wostringstream;
#define ostringstream wostringstream
#else
#endif
typedef vector<TCHAR> VTChar; // vtc
CWinApp theApp; // The one and only application object
using namespace std;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
TCHAR tcLocalComputerName[MAX_COMPUTERNAME_LENGTH + 1]= {_T('\0')};
DWORD dwComputerNameLength(MAX_COMPUTERNAME_LENGTH);
if(!::GetComputerName(tcLocalComputerName, &dwComputerNameLength))
{
string strErrorFormat(_T("GetComputerName failed with %d; returned length was %d."));
VTChar vtcError(strErrorFormat.size() + 20, 0);
_stprintf_s(&vtcError[0], vtcError.size(), strErrorFormat.c_str(), ::GetLastError(), dwComputerNameLength);
AfxMessageBox(&vtcError[0], MB_ICONEXCLAMATION);
}
else
{
string strFormat(_T("GetComputerName returned \"%s\""));
VTChar vtcError(strFormat.size() + 30, 0);
_stprintf_s(&vtcError[0], vtcError.size(), strFormat.c_str(), tcLocalComputerName);
AfxMessageBox(&vtcError[0], MB_ICONEXCLAMATION);
}
return 0;
}
一位同事使用它一段时间后发现,如果我们打第二个电话,它就会起作用。正在运行的应用实际上是以一种迂回的方式做到了这一点。
以下是实际运行的控制台应用程序的代码:
// GetComputerName.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
#include <string> // for string
#include <vector> // for vector
using std::vector;
using std::string;
#ifdef _UNICODE
using std::wstring;
#define string wstring
using std::wostringstream;
#define ostringstream wostringstream
#else
#endif
typedef vector<TCHAR> VTChar; // vtc
CWinApp theApp; // The one and only application object
using namespace std;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
bool gotName(false);
TCHAR tcLocalComputerName[MAX_COMPUTERNAME_LENGTH + 1]= {_T('\0')};
DWORD dwComputerNameLength(MAX_COMPUTERNAME_LENGTH);
if(!::GetComputerName(tcLocalComputerName, &dwComputerNameLength))
{
AfxMessageBox(_T("First GetComputerName failed."));
if(!::GetComputerName(tcLocalComputerName, &dwComputerNameLength))
{
string strErrorFormat(_T("GetComputerName failed with %d; returned length was %d."));
VTChar vtcError(strErrorFormat.size() + 20, 0);
_stprintf_s(&vtcError[0], vtcError.size(), strErrorFormat.c_str(), ::GetLastError(), dwComputerNameLength);
AfxMessageBox(&vtcError[0], MB_ICONEXCLAMATION);
}
else
gotName= true;
}
else
gotName= true;
if(gotName)
{
string strFormat(_T("GetComputerName returned \"%s\""));
VTChar vtcError(strFormat.size() + 30, 0);
_stprintf_s(&vtcError[0], vtcError.size(), strFormat.c_str(), tcLocalComputerName);
AfxMessageBox(&vtcError[0], MB_ICONEXCLAMATION);
}
return 0;
}
【问题讨论】:
-
请提供代码,包括所用变量的声明
-
这极不可能与 VMware 有任何关系。显示您的代码。
-
我会尽快发布。我同意它可能不是 VMWare,但代码是可靠的。它适用于各种风格的 Windows(我几年前写的)、运行 VM 的 PC 和 Mac。但是,不同应用程序中的相同代码可以在同一台机器上运行。我怀疑它与应用程序类型有关。 FWIW,“COMPUTERNAME”的 GetEnvironmentVariable 成功,但返回一个空名称。我知道要测试什么。
-
这台电脑的名字到底是什么?
-
实际名称为“WIN-N6LLD42BT79”