Re[3]: C#: Как получить доступ к static struct из C++ dll
От: Serginio1 СССР https://habrahabr.ru/users/serginio1/topics/
Дата: 26.03.19 15:59
Оценка:
Здравствуйте, newalex77, Вы писали:

N>Здравствуйте, Serginio1, Вы писали:



S>>https://stackoverflow.com/questions/17561699/how-to-get-an-intptr-to-a-struct


N>В этой статье показано как использовать функцию из dll, в которую передается структура.

N>Мне надо получить доступ к статической структуре из dll (в которой есть указатели на функции).

Не совсем там показано копирование из объекта в нативную память и наоборот

private IntPtr MarshalToPointer(
    object data)
{
    IntPtr buf = Marshal.AllocHGlobal(
        Marshal.SizeOf(data));
    Marshal.StructureToPtr(data,
        buf, false);
    return buf;
}

This simply returns an IntPtr to an area of the global heap that contains a copy of the data. The only problem with this function is that you have to remember to release the allocated heap memory after use. For example:

IntPtr lpstruct =
    MarshalToPointer(Sinfo);
result = AVIFileCreateStream(pFile,
        ref pStream, lpstruct);
Marshal.FreeHGlobal(lpstruct);

…works exactly like default marshalling. But don’t forget that lpstruct is itself still being marshalled as a pass-by-value integer. To copy the result back to the struct an additional function is required:

private object MarshalToStruct(
        IntPtr buf,Type t)
{
    return Marshal.PtrToStructure(
        buf, t);



Доступ к структуре ты можешь получить через экспортируемые функции
и солнце б утром не вставало, когда бы не было меня
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.