Shared memory Managed<->Unmanaged

  • Thread starter Thread starter AgilentCoder
  • Start date Start date
A

AgilentCoder

I am experimenting with shared memory in windows ce 5 and .net 2.0.

I want to be able to :

Create a dll that allocates some memory.
call into the dll from .net 2.0 and get a pointer to that memory
change the memory directly (without all the marshal.this and marshal.that)

Is this just me dreamin' or can this be done? I try to pass down an out
IntPtr to my dll function and it sets the pointer to the address of a simple
array, but when it returns, I have zeros for my intpointer.

I am marking my methods unsafe, and willing to take the risk. I think it
will be fast - if it works.

Anybody have some tips or a good website that discusses this that I could
read? I have not found anything that really tries to cover this.

Thanks,

Paul
 
Sure - it works fine and I've done it many times. Unsafe is one method,
creating a pinned GCHandle is another. Directly P/Invoking LocalAlloc is
yet another. The object is to create a memory region that the GC cannot
move.

Speed is good, provided you know the limitations of the language. Method
calls are expensive, so operating on pointers is a better route. The
question at that point though is is there really a point to it all? Why not
just do the work in unmanaged code?

-Chris
 
Hi paul,
This can be done. See in the dll if you are making your
intptr to point to a local array (that is array defined inside that
function) it will be problem since after exiting the function the array
becomes obselete and you will only get zeros.

So just try out by making the array as a global variable in the dll.
That is define the array in the top of C or CPP file you are using it
(do not define it inside any functions, but you can use it inside any
function of the dll and modify it's value). Then make your intptr
variable point to this global array and there should be no zeros but
some sensible values.

If you still have problem with this, then add '__declspec (dllexport)
before the array you are using, which helps you to access the array
from your managed code.

Ah last but not least, make sure that the data types of array you are
using in the dll, and if any marshal or data modification to be done in
the memory location (by managed code) are always of same data types. If
you are it as a 'String' data type in managed code, then make sure it
is of 'TCHAR' data type in the dll (unmanaged code).

Hope this resolves your issue.

Regards,

Arun.
 
Back
Top