我在 C# 中使用 Borland C API 工作,该 API 使用大量字节指针来表示字符串。我一直面临将一些 C# 字符串作为(短暂的)字节 * 传递的需要。
我的自然假设是 const 对象不会分配到堆上,而是直接存储在程序内存中,但我无法在任何文档中验证这一点。
这是我为生成指向常量字符串的指针所做的示例。这确实在测试中按预期工作,我只是不确定它是否真的安全,或者它只是靠运气。
private const string pinnedStringGetWeight = "getWeight";
unsafe public static byte* ExampleReturnWeightPtr(int serial)
{
fixed (byte* pGetWeight = ASCIIEncoding.ASCII.GetBytes(pinnedStringGetWeight))
return pGetWeight;
}
这个 const 真的被固定了吗,或者它有可能被移动吗?
@克拉根:
这里是导入:
[DllImport("sidekick.dll", CallingConvention = CallingConvention.Winapi)]
public static extern int getValueByFunctionFromObject(int serial, int function, byte* debugCallString);
这是实际的功能。是的,它实际上需要一个静态函数指针:
private const int FUNC_GetWeight = 0x004243D0;
private const string pinnedStringGetWeight = "getWeight";
unsafe public static int getWeight(int serial)
{
fixed (byte* pGetWeight = ASCIIEncoding.ASCII.GetBytes(pinnedStringGetWeight))
return Core.getValueByFunctionFromObject(serial, FUNC_GetWeight, pGetWeight);
}
以下是我在模拟我的 API 时使用的另一种方法,使用静态结构,我也希望它被固定。我希望找到一种方法来简化这一点。
public byte* getObjVarString(int serial, byte* varName)
{
string varname = StringPointerUtils.GetAsciiString(varName);
string value = MockObjVarAttachments.GetString(serial, varname);
if (value == null)
return null;
return bytePtrFactory.MakePointerToTempString(value);
}
static UnsafeBytePointerFactoryStruct bytePtrFactory = new UnsafeBytePointerFactoryStruct();
private unsafe struct UnsafeBytePointerFactoryStruct
{
fixed byte _InvalidScriptClass[255];
fixed byte _ItemNotFound[255];
fixed byte _MiscBuffer[255];
public byte* InvalidScriptClass
{
get
{
fixed (byte* p = _InvalidScriptClass)
{
CopyNullString(p, "Failed to get script class");
return p;
}
}
}
public byte* ItemNotFound
{
get
{
fixed (byte* p = _ItemNotFound)
{
CopyNullString(p, "Item not found");
return p;
}
}
}
public byte* MakePointerToTempString(string text)
{
fixed (byte* p = _ItemNotFound)
{
CopyNullString(p, text);
return p;
}
}
private static void CopyNullString(byte* ptrDest, string text)
{
byte[] textBytes = ASCIIEncoding.ASCII.GetBytes(text);
fixed (byte* p = textBytes)
{
int i = 0;
while (*(p + i) != 0 && i < 254 && i < textBytes.Length)
{
*(ptrDest + i) = *(p + i);
i++;
}
*(ptrDest + i) = 0;
}
}
}
最佳答案
在这种情况下,如何分配常量并不重要,因为 ASCIIEncoding.ASCII.GetBytes()
返回一个新的字节数组(它不能返回常量的内部数组,因为它的编码方式不同(编辑:希望没有方法可以获取指向string
内部数组的指针,因为字符串是不可变的))。但是,只要 fixed
作用域存在,GC 不会触及数组的保证就会持续 - 换句话说,当函数返回时,内存不再固定。
关于c# - 常量是否固定在 C# 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6735919/