“暗淡”:找不到标识符

发布于 11-26 10:20 字数 1900 浏览 2 评论 0原文

我收到此错误消息:

错误 C3861:“dim”:找不到标识符

这是我的包含:

#include "stdafx.h"
#include "HSMBTPrintX.h"
#include "HSMBTPrintXCtrl.h"
#include "HSMBTPrintXPropPage.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#endif

这是我的函数:

#define MSS_PORTS_BASE _T("Software\\Microsoft\\Bluetooth\\Serial\\Ports")
bool FindBluetoothPort(TCHAR name[16]) {
    HKEY hKey, hRoot;
    TCHAR szPort[20] = _T(""), szPortString[20];
    DWORD len, dwIndex=0;
    bool bFound=false;
    INT i = 0, rc;
    DWORD dwNSize;
    DWORD dwCSize;
    TCHAR szClass[256];
    TCHAR szName[MAX_PATH];
    FILETIME ft;
    hRoot = HKEY_LOCAL_MACHINE;
    if (RegOpenKeyEx (hRoot, MSS_PORTS_BASE, 0, 0, &hKey) != ERROR_SUCCESS) {
        rc = GetLastError();
        return 0;
    }
    dwNSize = dim(szName);    <---- ~~ !! HERE IS THE LINE THAT ERRORS
    dwCSize = dim(szClass);     <---- HERE IS THE LINE THAT ERRORS  !! 
    rc = RegEnumKeyEx (hKey, i, szName, &dwNSize, NULL, szClass, &dwCSize, &ft);
    while (rc == ERROR_SUCCESS)
    {
        // how many children
        TCHAR szCurrentKey[MAX_PATH];
        wcscpy(szCurrentKey, MSS_PORTS_BASE);
        wcscat(szCurrentKey, TEXT("\\"));
        wcscat(szCurrentKey, szName);
        wcscat(szCurrentKey, TEXT("\\"));
        len = sizeof(szPort);
        if(RegGetValue(hRoot, szCurrentKey, _T("Port"), NULL, (LPBYTE)szPort, &len)) {
            wsprintf(szPortString, _T("%s:"), szPort);
            bFound = true;
            break;
        }
        dwNSize = dim(szName);
        rc = RegEnumKeyEx(hKey, ++i, szName, &dwNSize, NULL, NULL, 0, &ft);
    }

    if(bFound)
        _tcscpy(name, szPortString);

    return bFound;
}

如您所见,使用此函数的两行是:

dwNSize = dim(szName);

dwCSize = dim(szClass);

为什么这是一个错误?

I am getting this error message:

error C3861: 'dim': identifier not found

Here are my includes:

#include "stdafx.h"
#include "HSMBTPrintX.h"
#include "HSMBTPrintXCtrl.h"
#include "HSMBTPrintXPropPage.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#endif

Here is my function:

#define MSS_PORTS_BASE _T("Software\\Microsoft\\Bluetooth\\Serial\\Ports")
bool FindBluetoothPort(TCHAR name[16]) {
    HKEY hKey, hRoot;
    TCHAR szPort[20] = _T(""), szPortString[20];
    DWORD len, dwIndex=0;
    bool bFound=false;
    INT i = 0, rc;
    DWORD dwNSize;
    DWORD dwCSize;
    TCHAR szClass[256];
    TCHAR szName[MAX_PATH];
    FILETIME ft;
    hRoot = HKEY_LOCAL_MACHINE;
    if (RegOpenKeyEx (hRoot, MSS_PORTS_BASE, 0, 0, &hKey) != ERROR_SUCCESS) {
        rc = GetLastError();
        return 0;
    }
    dwNSize = dim(szName);    <---- ~~ !! HERE IS THE LINE THAT ERRORS
    dwCSize = dim(szClass);     <---- HERE IS THE LINE THAT ERRORS  !! 
    rc = RegEnumKeyEx (hKey, i, szName, &dwNSize, NULL, szClass, &dwCSize, &ft);
    while (rc == ERROR_SUCCESS)
    {
        // how many children
        TCHAR szCurrentKey[MAX_PATH];
        wcscpy(szCurrentKey, MSS_PORTS_BASE);
        wcscat(szCurrentKey, TEXT("\\"));
        wcscat(szCurrentKey, szName);
        wcscat(szCurrentKey, TEXT("\\"));
        len = sizeof(szPort);
        if(RegGetValue(hRoot, szCurrentKey, _T("Port"), NULL, (LPBYTE)szPort, &len)) {
            wsprintf(szPortString, _T("%s:"), szPort);
            bFound = true;
            break;
        }
        dwNSize = dim(szName);
        rc = RegEnumKeyEx(hKey, ++i, szName, &dwNSize, NULL, NULL, 0, &ft);
    }

    if(bFound)
        _tcscpy(name, szPortString);

    return bFound;
}

As you can see, the two lines that use this are:

dwNSize = dim(szName);

dwCSize = dim(szClass);

Why is that an error?

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

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

发布评论

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

评论(3

[浮城]2024-12-03 10:20:06

看起来您想要 sizeof

dwNSize = sizeof(szName);
dwCSize = sizeof(szClass);

sizeof 返回对象/变量的字节数。但是,我刚刚查看了 API RegEnumKeyEx 的文档,它需要字符数。所以我认为它实际上应该除以 TCHAR 的大小(这将是 1 或 2,具体取决于您是否为 Unicode 构建)。

dwNSize = sizeof(szName) / sizeof(TCHAR);
dwCSize = sizeof(szClass) / sizeof(TCHAR);

It looks like you are wanting sizeof:

dwNSize = sizeof(szName);
dwCSize = sizeof(szClass);

sizeof returns the number of bytes of the object/variable. However, I just looked at the documentation for the API RegEnumKeyEx, and it needs the number of characters. So I think it actually should divide by the size of a TCHAR (which will be 1 or 2 depending on if your are building for Unicode).

dwNSize = sizeof(szName) / sizeof(TCHAR);
dwCSize = sizeof(szClass) / sizeof(TCHAR);
甜嗑2024-12-03 10:20:06

你需要sizeof

如果您最初学习了 dim,那可能是一个在幕后真正调用 sizeof 的宏。

You want sizeof.

If you originally learned dim, that was probably a macro that really calls sizeof behind the scenes.

┈┾☆殇2024-12-03 10:20:06

我过去使用过以下宏:

#define DIM(x) (sizeof(x)/sizeof((x)[0]))

任何标准都没有提供它,您必须自己定义它。

您还可以使用模板函数制作更现代的版本:

template<typename T, size_t N>
size_t dim(const T (& array)[N])
{
   return N;
}

I have in the past used the following macro:

#define DIM(x) (sizeof(x)/sizeof((x)[0]))

It is not provided by any of the standard includes, you have to define it for yourself.

You can also do a more modern version using a template function:

template<typename T, size_t N>
size_t dim(const T (& array)[N])
{
   return N;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文