常量是否固定在C#中?

时间:2011-07-18 16:12:38

标签: c# .net garbage-collection

我正在使用Borland C API在C#中工作,该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是固定的,还是有可能被移动?


@Kragen:

这是导入:

[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;
        }
    }
}

2 个答案:

答案 0 :(得分:12)

在这种情况下,如何分配常量无关紧要,因为ASCIIEncoding.ASCII.GetBytes()返回一个新的字节数组(它不能返回常量的内部数组,因为它的编码方式不同(编辑:希望 no) 获取指向string内部数组的指针的方法,因为字符串是不可变的))。但是,GC不会触及数组的保证只能持续fixed范围 - 换句话说,当函数返回时,内存不再固定。

答案 1 :(得分:1)

基于Kragans的评论,我研究了将字符串编组为字节指针的正确方法,现在使用以下内容作为我在问题中使用的第一个示例:

        [DllImport("sidekick.dll", CallingConvention = CallingConvention.Winapi)]
        public static extern int getValueByFunctionFromObject(int serial, int function, [MarshalAs(UnmanagedType.LPStr)]string debugCallString);