Возврат станд.:: строка от DLL C++ до c# программы-> Недопустимый Адрес, указанный к RtlFreeHeap

Проблема вашего кода - ваша логика в цикле for.

Вы видите: -

a = 6
b = 2
    for i in range(0, 10):
        a = x[a - b]
        b = x[a] - x[b]

Да было 6 & amp; b равнялось 2, но когда вы впервые вводите цикл for, значение a равно

x [a-b], что равно x [6-2]

, т.е. x [4], что дает вам 11

, так что значение a равно 11, аналогично для b,

b = x[a] - x[b] # which translates to x[11] - x[2]

, который становится b = 32 & amp; цикл продолжает прыгать / изменять значения & amp; б, что приводит вас к IndexError: list index out of range, что ожидается.

Попробуйте выполнить вашу программу в pycharm & amp; отладьте его, вы поймете это лучше, или, может быть, просто поместите несколько операторов печати. ​​

Я не мог набрать столько комментариев, поэтому опубликовал их как ответ.

9
задан Christian 8 January 2010 в 16:21
поделиться

1 ответ

I managed to find the issue. It was the way the C# definition was done. From what I can understand, using the MarshallAs(UnmanagedType.LPStr) in combination with the string return type makes it so that it'll attempt to free the string when its done. But because the string comes from the C++ DLL, and most likely a totally different memory manager, it fails. And even if it didn't fail, I don't want it to be freed anyway.

The solution I found was to change the C# declaration to this (the C++ code is unchanged):

[DllImport("MyDll", EntryPoint = "GetDLLName")]
public static extern IntPtr GetDLLName();

So this makes it so that it just returns a pointer to the string data. And then to change it to a string, pass it to Marshal.PtrToStringAnsi()

return Marshal.PtrToStringAnsi(GetDLLName());

And that gets wrapped into another function for cleanliness.

I found the solution from this page: http://discuss.fogcreek.com/dotnetquestions/default.asp?cmd=show&ixPost=1108

12
ответ дан 4 December 2019 в 19:36
поделиться
Другие вопросы по тегам:

Похожие вопросы: