"dim":找不到标识符

'dim': identifier not found

本文关键字:标识符 找不到 dim      更新时间:2023-10-16

我得到这个错误信息:

错误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);

为什么这是一个错误?

看起来你想要的是sizeof:

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

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

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

你想要sizeof

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

我过去使用过以下宏:

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

它不是由任何标准包含提供的,您必须自己定义它。

你也可以使用模板函数做一个更现代的版本:

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