Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

.net

pinvoke

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 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 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?

like image 759
Ronald Avatar asked Feb 28 '11 14:02

Ronald


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


1 Answers

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.

like image 52
Lee Berger Avatar answered Oct 19 '22 23:10

Lee Berger