开发者

C# how can i pin an object in memory without marshalling the object?

开发者 https://www.devze.com 2023-02-14 09:28 出处:网络
Ok here it goes, i\'m using oracle\'s ContentAccess library in C#, the library is programmed in C. I use some functions of the library to extract text from diffrent files.

Ok here it goes, i'm using oracle's ContentAccess library in C#, the library is programmed in C.

I use some functions of the library to extract text from diffrent files. the c library uses async communication by function pointers (Delegates). i have 1 class and 1 struct that is needed to use the functions, the struct is called BaseIO and contains functions pointers that points to my code in C# to read files. everything is fine, until cli moves my class, and i get an MemoryAccessException.

here is the 开发者_如何学JAVAclass, struct and function signature:

[UnmanagedFunctionPointerAttribute(CallingConvention.Cdecl)]
public delegate DAERR CloseDelegate(IntPtr hfile);
[UnmanagedFunctionPointerAttribute(CallingConvention.Cdecl)]
public delegate DAERR ReadDelegate(IntPtr hfile, IntPtr dataPtr, UInt32 size, IntPtr count);
[UnmanagedFunctionPointerAttribute(CallingConvention.Cdecl)]
public delegate DAERR WriteDelegate(IntPtr hfile, IntPtr dataPtr, int size, IntPtr count);
[UnmanagedFunctionPointerAttribute(CallingConvention.Cdecl)]
public delegate DAERR SeekDelegate(IntPtr hfile, int wFrom, int dwOffset);
[UnmanagedFunctionPointerAttribute(CallingConvention.Cdecl)]
public delegate DAERR TellDelegate(IntPtr hfile, IntPtr dwOffset);
[UnmanagedFunctionPointerAttribute(CallingConvention.Cdecl)]
public delegate DAERR GetInfoDelegate(IntPtr hfile, UInt32 dwInfoId, IntPtr pInfo);
[UnmanagedFunctionPointerAttribute(CallingConvention.Cdecl)]
public delegate DAERR Seek64Delegate(IntPtr hfile, ushort wFrom, Int64 dwOffset);
[UnmanagedFunctionPointerAttribute(CallingConvention.Cdecl)]
public delegate DAERR Tell64Delegate(IntPtr hfile, Int64 dwOffset);

struct BaseIO
{
    public CloseDelegate IOCLOSEPROC;
    public ReadDelegate IOREADPROC;
    public WriteDelegate IOWRITEPROC;
    public SeekDelegate IOSEEKPROC;
    public TellDelegate IOTELLPROC;
    public GetInfoDelegate IOGETINFOPROC;
    public IntPtr IOOPENPROC;
    public Seek64Delegate IOSEEK64PROC;
    public Tell64Delegate IOTELL64PROC;
}

[StructLayout(LayoutKind.Sequential)]
class FILE
{
    public BaseIO baseIO;
    public Stream Source;
    public Stream Buffer;
}

[DllImport("sccda.dll", CallingConvention = CallingConvention.Cdecl)]
    public static extern DAERR DAOpenDocument(ref IntPtr phDoc, IOTYPE dwSpecType, FILE pSpec, DAFlags dwFlags);

DAERR is a enum with error values.

IOTYPE is a enum with hex values to indicate what to do with the value pSpec.

DAFlags is a enum with hex values to indicate how to treat the file (Like an archive, normal, or autodetect).

oke every thing works, and my functions get called on the managed side like it should, but after a 1 pass I get an MemoryException indicating the object has been moved in memory.

I cannot use marshalling becouse then I cannot use a Stream object in my class and can't use normal delegates.

The library only looks at the first object in FILE and that is the struct BaseIO.

My question is, is it possible to pin an object in memory without marshalling the object?


In C#, there are two ways to pin an object in memory: the fixed statement and GCHandle.Alloc. In your case, I believe you want GCHandle.Alloc.

See GCHandle.Alloc for a code example, and the fixed keyword documentation.

0

精彩评论

暂无评论...
验证码 换一张
取 消