sizeof 不能获取托管类型变量的大小
sizeof Cannot take the size of a variable of a managed type
我正在尝试为某些硬件使用一些示例代码,MicroGate controller card。他们提供了一些示例代码,而我 运行 遇到了使用 sizeof 的行的错误。这是示例代码:
public static uint GetPortID(string name)
{
uint i, rc, count;
uint port_id = 0;
MGSL_PORT[] ports;
/* get count of available ports */
rc = MgslEnumeratePorts(null, 0, out count);
if (rc != 0 || count == 0)
return 0;
/* allocate memory to hold port information */
ports = new MGSL_PORT[count];
/* get port information */
rc = MgslEnumeratePorts(ports, (uint)(count * sizeof(MGSL_PORT)), out count);
if (rc != 0 || count == 0)
return 0;
/* search for entry with matching name */
for (i=0; i < count; i++) {
string port_name;
char[] port_chars = new char[25];
uint j;
fixed (byte* sendBuf = ports[i].DeviceName)
{
for (j=0 ; j < 25; j++)
port_chars[j] = (char)sendBuf[j];
}
port_name = new string(port_chars);
if (String.Compare(port_name.ToUpper(), name.ToUpper()) == 0) {
port_id = ports[i].PortID;
break;
}
}
return port_id;
}
上线即:
rc = MgslEnumeratePorts(ports, (uint)(count * sizeof(MGSL_PORT)), out count);
Visual Studio 表示“无法获取托管类型变量的大小 'MGSL_PORT'。出于好奇,我们认为这段代码在过去可能有效吗?我需要一个Visual Studio 的不同版本?关于如何修复它有什么建议吗?我无法想象他们会提供这个代码示例并且不希望它工作。任何帮助将不胜感激。
sizeof
(reference) can only take certain types. From the reference, it can take a struct
only if it doesn't contain any reference types. If the type does have a constant, known size (API reference) 你可以这样做:
const int MGSL_SIZE = 37;
rc = MgslEnumeratePorts(ports, (uint)(count * MGSL_SIZE)), out count);
我能够使用 Marshal.SizeOf 获取结构的大小。我的代码行现在是:
rc = MgslEnumeratePorts(ports, (uint) (count * Marshal.SizeOf(typeof(SerialApi.MGSL_PORT))), out count);
我正在尝试为某些硬件使用一些示例代码,MicroGate controller card。他们提供了一些示例代码,而我 运行 遇到了使用 sizeof 的行的错误。这是示例代码:
public static uint GetPortID(string name)
{
uint i, rc, count;
uint port_id = 0;
MGSL_PORT[] ports;
/* get count of available ports */
rc = MgslEnumeratePorts(null, 0, out count);
if (rc != 0 || count == 0)
return 0;
/* allocate memory to hold port information */
ports = new MGSL_PORT[count];
/* get port information */
rc = MgslEnumeratePorts(ports, (uint)(count * sizeof(MGSL_PORT)), out count);
if (rc != 0 || count == 0)
return 0;
/* search for entry with matching name */
for (i=0; i < count; i++) {
string port_name;
char[] port_chars = new char[25];
uint j;
fixed (byte* sendBuf = ports[i].DeviceName)
{
for (j=0 ; j < 25; j++)
port_chars[j] = (char)sendBuf[j];
}
port_name = new string(port_chars);
if (String.Compare(port_name.ToUpper(), name.ToUpper()) == 0) {
port_id = ports[i].PortID;
break;
}
}
return port_id;
}
上线即:
rc = MgslEnumeratePorts(ports, (uint)(count * sizeof(MGSL_PORT)), out count);
Visual Studio 表示“无法获取托管类型变量的大小 'MGSL_PORT'。出于好奇,我们认为这段代码在过去可能有效吗?我需要一个Visual Studio 的不同版本?关于如何修复它有什么建议吗?我无法想象他们会提供这个代码示例并且不希望它工作。任何帮助将不胜感激。
sizeof
(reference) can only take certain types. From the reference, it can take a struct
only if it doesn't contain any reference types. If the type does have a constant, known size (API reference) 你可以这样做:
const int MGSL_SIZE = 37;
rc = MgslEnumeratePorts(ports, (uint)(count * MGSL_SIZE)), out count);
我能够使用 Marshal.SizeOf 获取结构的大小。我的代码行现在是:
rc = MgslEnumeratePorts(ports, (uint) (count * Marshal.SizeOf(typeof(SerialApi.MGSL_PORT))), out count);