gpt4 book ai didi

c# - 在 C# 中将字符串数组编码为 char **

转载 作者:太空狗 更新时间:2023-10-29 22:35:17 26 4
gpt4 key购买 nike

我正在调用 C DLL 函数并需要提供以下 C 结构:

typedef struct
{
char *mTableId;
char **mFieldNames;
int mNumFields;
char *mFilter;
char *mSort;
int mOffset;
int mMaxRecords;
char *mTargetRecordFilter;
int mSurroundingRecordsCount;
int *mOwnerIds;
int mNumOwnerIds;
gsi_bool mCacheFlag;
} SAKESearchForRecordsInput;

问题出在 char **mFieldNames;我试过像这样自动编码:

[MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPTStr, SizeConst = 9)] public String[] mFieldNames;

这样我在 Marshal.SizeOf() 中得到一个错误 - 无法计算正确的大小。然后我决定手动处理指针。它实际上只是一个指向 C 字符串数组的指针。这是我的代码,它导致

System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

所以我在某处搞砸了指针。代码对我来说似乎没问题,错误在哪里?

C#:

 [StructLayout(LayoutKind.Sequential)]
unsafe public class SAKESearchForRecordsInput {
[MarshalAs(UnmanagedType.LPTStr)]
public String mTableId;
//[MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPTStr, SizeConst = 9)] // HARDCODED!?!
//public String[] mFieldNames; // char **mFieldNames;
public IntPtr mFieldNames;
public int mNumFields;
[MarshalAs(UnmanagedType.LPTStr)]
public String mFilter;
[MarshalAs(UnmanagedType.LPTStr)]
public String mSort;
public int mOffset;
public int mMaxRecords;
//[MarshalAs(UnmanagedType.LPTStr)]
public IntPtr mTargetRecordFilter;
public int mSurroundingRecordsCount;
public IntPtr mOwnerIds;
public int mNumOwnerIds;
public gsi_bool mCacheFlag;
}

[DllImport("saketestd.dll")]
unsafe static extern void* sakeSearchForRecords(
IntPtr sake,
IntPtr input, //SAKESearchForRecordsInput *
SAKERequestCallback callback, //SAKERequestCallback
IntPtr userData);

unsafe public bool sakeSearchForRecordsE() {
bool ret = false;
try {
searchInput.mTableId = "bbdx_score";
//searchInput.mFieldNames = mFieldNames.to;
searchInput.mFilter = "num_ratings = 0 AND filestore > 0";
searchInput.mSort = "";
searchInput.mOffset = 0;
searchInput.mMaxRecords = 1;
//searchInput.mTargetRecordFilter = "";
searchInput.mSurroundingRecordsCount = 0;
searchInput.mOwnerIds = IntPtr.Zero;
searchInput.mNumOwnerIds = 0;
searchInput.mCacheFlag = true;

int sakeSize = Marshal.SizeOf(sake);
debug.AddLine(this.getMethodName() + ": sizeof(sake): " + sakeSize);
IntPtr pSake = Marshal.AllocHGlobal(sakeSize);
Marshal.StructureToPtr(sake, pSake, true);

int inputSize = Marshal.SizeOf(searchInput);
debug.AddLine(this.getMethodName() + ": sizeof(input): " + inputSize);
IntPtr pInput = Marshal.AllocHGlobal(inputSize);
Marshal.StructureToPtr(searchInput, pInput, true);

IntPtr[] mFieldNamesPtr;
int i;
if (true) { // IntPtr[]
mFieldNamesPtr = new IntPtr[mFieldNames.Length];
i = 0;
foreach (string str in mFieldNames) {
mFieldNamesPtr[i++] = Marshal.StringToHGlobalAnsi(str);
}
//searchInput.mFieldNames = mFieldNamesPtr;
} else {
//searchInput.mFieldNames = mFieldNames;
}
searchInput.mNumFields = mFieldNames.Length;

void* pRequestInternal = null;
void* p = mFieldNamesPtr[0].ToPointer();
searchInput.mFieldNames = (IntPtr)p;
pRequestInternal = sakeSearchForRecords(
pSake,
pInput,
new SAKERequestCallback(this.sakeSearchForRecordsCB),
IntPtr.Zero
);


sake = (SAKEInternal)Marshal.PtrToStructure(pSake, typeof(SAKEInternal));
if (searchRequest == null) {
debug.AddLine(this.getMethodName() + ": mStartRequestResult: " + sake.mStartRequestResult);
} else {
ret = true;
this.searchRequest = (SAKERequestInternal)Marshal.PtrToStructure(
new IntPtr(pRequestInternal),
typeof(SAKERequestInternal)
);
searchInput = (SAKESearchForRecordsInput)Marshal.PtrToStructure(
pInput,
typeof(SAKESearchForRecordsInput)
);

if (true) {
i = 0;
foreach (string str in mFieldNames) {
Marshal.FreeHGlobal(mFieldNamesPtr[i++]);
}
}

PrintStruct ps = new PrintStruct(sake);
debug.AddLine(this.getMethodName() + ": sake: " + ps);
ps = new PrintStruct(searchRequest);
debug.AddLine(this.getMethodName() + ": searchRequest: " + ps.print_r());
ps = new PrintStruct(searchInput);
debug.AddLine(this.getMethodName() + ": searchInput: " + ps.print_r());
}
Marshal.FreeHGlobal(pSake);
Marshal.FreeHGlobal(pInput);
} catch (Exception ex) {
debug.Text += ex.ToString();
}
return ret;
}

最佳答案

编码(marshal)讨厌的字符串指针,尤其是结构中的双指针的最佳方法是简单地使用 IntPtr。

public IntPtr mFieldNames;

这将正确地编码,尽管类型不是那么有用。但是,如果您了解 IntPtr 的结构,则很容易得出结果字符串。

public static List<string> GetAllStrings(IntPtr ptr, int size) {
var list = new List<string>();
for ( int i = 0; i < size; i++ ) {
var strPtr = (IntPtr)Marshal.PtrToStructure(ptr, typeof(IntPtr));
list.Add(Marshal.PtrToStringUni(strPtr));
ptr = new IntPtr(ptr.ToInt64()+IntPtr.Size);
}
return list;
}

唯一真正的缺点是你必须手动释放内存

关于c# - 在 C# 中将字符串数组编码为 char **,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1498931/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com