C#如何在不编组对象的情况下将对象固定在内存中? [英] C# how can i pin an object in memory without marshalling the object?

查看:129
本文介绍了C#如何在不编组对象的情况下将对象固定在内存中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我正在C#中使用oracle的ContentAccess库,该库是用C编程的.

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

我使用库的某些功能从不同的文件中提取文本. c库通过函数指针(委托)使用异步通信. 我有1个类和1个使用这些函数所需的结构,该结构称为BaseIO,其中包含指向C#中用于读取文件的代码的函数指针. 一切都很好,直到cli移动了我的课程,然后我得到了MemoryAccessException.

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 class, 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是一个带有错误值的枚举.

DAERR is a enum with error values.

IOTYPE是一个具有十六进制值的枚举,用于指示如何使用值pSpec.

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

DAFlags是一个带有十六进制值的枚举,用于指示如何处理文件(如存档,正常或自动检测).

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

每件事都起作用,并且我的函数像应有的那样在托管端被调用,但是经过1次传递后,我得到了MemoryException,表明该对象已在内存中移动.

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.

我不能使用编组,因为我不能在类中使用Stream对象,也不能使用普通的委托.

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

该库仅查看FILE中的第一个对象,即BaseIO结构.

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?

推荐答案

在C#中,有两种方法可以将对象固定在内存中:fixed语句和GCHandle.Alloc.就您而言,我相信您想要GCHandle.Alloc.

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.

有关代码示例,请参见 GCHandle.Alloc 和<一个href ="http://msdn.microsoft.com/zh-cn/library/f58wzh21.aspx" rel ="noreferrer">固定关键字文档.

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

这篇关于C#如何在不编组对象的情况下将对象固定在内存中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆