WNetGetUniversalName 的问题

发布于 2024-08-19 06:31:41 字数 718 浏览 3 评论 0原文

我对一些调用 WNetGetUniversalName 的代码有一个奇怪的问题。当我调用该函数时,我总是收到错误 67 (ERROR_BAD_NET_NAME)。但网络连接确实存在。
所以从头开始。我正在编写一个 Windows shell 扩展,它将对位于指定网络驱动器上的文本文件执行一些操作。因此,当调用 IShellExtInit::Initialize 方法时,我存储拖动的文件,然后使用 WNetGetUniversalName 方法获取连接名称。
所以我可以确定网络驱动器确实存在(因为它来自 DragQueryFile 方法)。
下面是一些代码:

char buffer[4096];  
REMOTE_NAME_INFO *info = (REMOTE_NAME_INFO*)buffer;  
DWORD length = 4096;  
info->lpConnectionName = NULL;  
info->lpRemainingPath = NULL;  
info->lpUniversalName = NULL;  
DWORD error = WNetGetUniversalName(file, REMOTE_NAME_INFO_LEVEL, info, &length);  

file 是来自 DragQueryFile 方法的 ATL::CString,错误始终为 67。
奇怪的是,它几天前确实有效,但现在不再有效,而且我没有更改发布的任何代码。

I have a strange problem with some code that calls WNetGetUniversalName. When I call the function I always get error 67 (ERROR_BAD_NET_NAME). But the network connection really does exist.
So from the scratch. I'm writing a Windows shell extension that shall do some stuff with text files that are located on a specified network drive. So when the IShellExtInit::Initialize method is called I store the dragged file and will then get the connection name by using the WNetGetUniversalName method.
So I can be really sure that the network drive does exist (as it comes from the DragQueryFile method).
Here's some code:

char buffer[4096];  
REMOTE_NAME_INFO *info = (REMOTE_NAME_INFO*)buffer;  
DWORD length = 4096;  
info->lpConnectionName = NULL;  
info->lpRemainingPath = NULL;  
info->lpUniversalName = NULL;  
DWORD error = WNetGetUniversalName(file, REMOTE_NAME_INFO_LEVEL, info, &length);  

file is an ATL::CString that comes from the DragQueryFile method and error is always 67.
Odd thing is that it did work some days ago but not anymore, and I didn't change any of that code posted.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抱着落日 2024-08-26 06:31:41

这个问题有点难以猜测。一种可能性是您作为文件名传递的 file 格式不正确。例如,即使文件位于根共享目录中,类似 z:test.txt 的内容也不起作用 - 它需要:z:\\test.txt

调用 WNetGetUniversalName 的通常方法涉及如下序列:

  1. 使用小缓冲区调用以获得所需的大小
  2. 分配适当大小的缓冲区 使用
  3. 适当大小的缓冲区再次调用

然而,与许多 Windows 网络函数不同,这个函数显然会检查您是否已发送首先是一个具有正大小的非 NULL 指针,然后检查缓冲区是否足够大以容纳它想要返回的内容,因此序列如下所示:

REMOTE_NAME_INFO temp;
REMOTE_NAME_INFO *info = &temp;  
DWORD size = sizeof(temp);

// call with buffer that's valid but too small.    
WNetGetUniversalNameA("z:\\test.txt", REMOTE_NAME_INFO_LEVEL, info, &size);

// allocate large enough buffer:
info = static_cast<REMOTE_NAME_INFO *>(::operator new(size));

// call again with large enough buffer:
WNetGetUniversalNameA("z:\\test.txt", REMOTE_NAME_INFO_LEVEL, info, &size);

// Show result:
std::cout << info->lpUniversalName;

It's a little hard to guess about the problem. One possibility is that the file you're passing as the file name isn't formatted quite correctly. For example, even if the file is in the root shared directory, something like z:test.txt would not work -- it requires: z:\\test.txt.

The usual way to call WNetGetUniversalName involves a sequence like:

  1. Call with small buffer to get required size
  2. allocate proper-sized buffer
  3. call again with buffer of proper size

Unlike quite a few Windows network functions, however, this one apparently checks that you've sent a non-NULL pointer with a positive size first, then checks whether the buffer is large enough for what it wants to return, so the sequence looks something like this:

REMOTE_NAME_INFO temp;
REMOTE_NAME_INFO *info = &temp;  
DWORD size = sizeof(temp);

// call with buffer that's valid but too small.    
WNetGetUniversalNameA("z:\\test.txt", REMOTE_NAME_INFO_LEVEL, info, &size);

// allocate large enough buffer:
info = static_cast<REMOTE_NAME_INFO *>(::operator new(size));

// call again with large enough buffer:
WNetGetUniversalNameA("z:\\test.txt", REMOTE_NAME_INFO_LEVEL, info, &size);

// Show result:
std::cout << info->lpUniversalName;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文