【发布时间】:2012-01-25 16:13:42
【问题描述】:
我正在尝试将代码从 c++ 转换为 c#.. 但我被卡住了。 谁能告诉我我哪里错了?
C++代码是:
void CALLBACK Test(void *buffer, DWORD length)
{
BYTE *b=(BYTE*)buffer, temp[20000];
DWORD p=0;
while (p<length)
{
DWORD c=min(length-p, sizeof(temp));
memcpy(temp, b+p, c);
c=SendData(dummy, temp, c);
if (c==-1) break;
p+=c;
}
}
where is SendData(int, byte[], int)
以及当前的 c# 代码
void Test(IntPtr buffer, int length)
{
byte[] temp = new byte[20000];
byte[] b = new byte[length];
Marshal.Copy(buffer, b, 0, length);
long p=0;
while (p<length)
{
long c=Math.Min(length-p, 20000);
Array.Copy(temp, b+p, c);
c=SendData(dummy, temp, (int)c);
if (c==-1) break;
p+=c;
}
}
我不确定我做的是否正确,但我可以看到我无法将运算符 + 应用于 b+s,因为它是 long 和 byte[]。
谢谢
【问题讨论】:
-
与其进行“文字”转换,不如看看函数的作用并在 C# 中重新制作它,而不是更好?