CopyMemory for DirectX call in C#? 
Author Message
 CopyMemory for DirectX call in C#?

Hello!

I'm translating some DirectX samples to C# using InteropServices but I'm
stuck at one crucial point with two (or maybe three) possible solutions.

Choice 1 - Use CopyMemory:
I've got an object of type "Direct3DVertexBuffer" that has been initialized
for me. I also have an array of data. I'd like to copy the data from the
array into the object.
In VB6 the code looks like:
    'lPointer holds the pointer to the "Direct3DVertexBuffer"
    m_VertexBuffer.Lock 0, m_VertexSize * 3, lPointer, 0
    CopyMemory ByVal lPointer, vertices(0), m_VertexSize * 3
    m_VertexBuffer.Unlock
For C#, I have an array named vertices, which is a an array of structs that
I've defined.
I have no idea how to translate the CopyMemory call so that it will accept
my params (an int and an array of my structs). The above code will
definitely be in an unsafe{} code block (I also need the sizeof() function).

Choice 2 - Have the InteropServices recognize a "BAS" module.
Meaning, in VB there is a "module" imported from the COM DLL. Namely,
"D3DAUX". This does not appear to be part of the COM package as far as
DOTNET can see - I assume that DOTNET doesn't allow modules since they're
not contained in a class - and everything must be in a class :)

Choice 3 - Possibly write a VB6 COM DLL that does the CopyMemory for me. I
could pin the memory of the array before the call. I'd rather not do this
one - not even sure it would work - because it involves the extra DLL.

Any suggestions would be greatly appreciated!

Dan



Mon, 20 Sep 2004 04:57:18 GMT  
 CopyMemory for DirectX call in C#?
Hi Dan,

You really really want to consider using managed C++ and then exposing your
mesh and animation engine interfaces to C#... If not, when DX9 comes out,
you'll basically be a managed wrapper, under a wrapper, for a common wrapper
to C++... Not good, unless this is a HelloWorld/Spinning cube example...

If you absolutely want to stay with C# for vertex buffer and indices
creation, look at stackalloc for your countless dynamic buffers and in
unsafe code, use pointers and the Marshal class to convert data between
UInt32 pointers to your LPVOID * type in D3D.

-Moe


Quote:
> Hello!

> I'm translating some DirectX samples to C# using InteropServices but I'm
> stuck at one crucial point with two (or maybe three) possible solutions.

> Choice 1 - Use CopyMemory:
> I've got an object of type "Direct3DVertexBuffer" that has been
initialized
> for me. I also have an array of data. I'd like to copy the data from the
> array into the object.
> In VB6 the code looks like:
>     'lPointer holds the pointer to the "Direct3DVertexBuffer"
>     m_VertexBuffer.Lock 0, m_VertexSize * 3, lPointer, 0
>     CopyMemory ByVal lPointer, vertices(0), m_VertexSize * 3
>     m_VertexBuffer.Unlock
> For C#, I have an array named vertices, which is a an array of structs
that
> I've defined.
> I have no idea how to translate the CopyMemory call so that it will accept
> my params (an int and an array of my structs). The above code will
> definitely be in an unsafe{} code block (I also need the sizeof()
function).

> Choice 2 - Have the InteropServices recognize a "BAS" module.
> Meaning, in VB there is a "module" imported from the COM DLL. Namely,
> "D3DAUX". This does not appear to be part of the COM package as far as
> DOTNET can see - I assume that DOTNET doesn't allow modules since they're
> not contained in a class - and everything must be in a class :)

> Choice 3 - Possibly write a VB6 COM DLL that does the CopyMemory for me. I
> could pin the memory of the array before the call. I'd rather not do this
> one - not even sure it would work - because it involves the extra DLL.

> Any suggestions would be greatly appreciated!

> Dan



Mon, 20 Sep 2004 13:07:07 GMT  
 CopyMemory for DirectX call in C#?
lol me - this was quite a bit easier than I first thought.

I defined the CopyMemory (ie, RtlMoveMemory) function as:
 [System.Runtime.InteropServices.DllImport("KERNEL32.dll",
CharSet=System.Runtime.InteropServices.CharSet.Auto)]
 private static extern void RtlMoveMemory(int pData, CUSTOMVERTEX[]
vertices, int size);

All I had to do was define the function as taking the EXACT parameters I was
using and everything worked fine. This works because the CopyMemory function
can take pointers to anything - even custom DOTNET structs! Notice how the
API call takes an int (pData) and an array of CUSTOMVERTEX structs! I love
DOTNET!

Anyway, I've gotten a pure C# Direct3D sample working now - just draws a
3-colored triangle using Vertex Buffers. But if anyone wants to see the
source, I'll post it. It's quite simple code now that everything is working
:)

Dan Jones


Quote:
> Hi Dan,

> You really really want to consider using managed C++ and then exposing
your
> mesh and animation engine interfaces to C#... If not, when DX9 comes out,
> you'll basically be a managed wrapper, under a wrapper, for a common
wrapper
> to C++... Not good, unless this is a HelloWorld/Spinning cube example...

> If you absolutely want to stay with C# for vertex buffer and indices
> creation, look at stackalloc for your countless dynamic buffers and in
> unsafe code, use pointers and the Marshal class to convert data between
> UInt32 pointers to your LPVOID * type in D3D.

> -Moe



> > Hello!

> > I'm translating some DirectX samples to C# using InteropServices but I'm
> > stuck at one crucial point with two (or maybe three) possible solutions.

> > Choice 1 - Use CopyMemory:
> > I've got an object of type "Direct3DVertexBuffer" that has been
> initialized
> > for me. I also have an array of data. I'd like to copy the data from the
> > array into the object.
> > In VB6 the code looks like:
> >     'lPointer holds the pointer to the "Direct3DVertexBuffer"
> >     m_VertexBuffer.Lock 0, m_VertexSize * 3, lPointer, 0
> >     CopyMemory ByVal lPointer, vertices(0), m_VertexSize * 3
> >     m_VertexBuffer.Unlock
> > For C#, I have an array named vertices, which is a an array of structs
> that
> > I've defined.
> > I have no idea how to translate the CopyMemory call so that it will
accept
> > my params (an int and an array of my structs). The above code will
> > definitely be in an unsafe{} code block (I also need the sizeof()
> function).

> > Choice 2 - Have the InteropServices recognize a "BAS" module.
> > Meaning, in VB there is a "module" imported from the COM DLL. Namely,
> > "D3DAUX". This does not appear to be part of the COM package as far as
> > DOTNET can see - I assume that DOTNET doesn't allow modules since
they're
> > not contained in a class - and everything must be in a class :)

> > Choice 3 - Possibly write a VB6 COM DLL that does the CopyMemory for me.
I
> > could pin the memory of the array before the call. I'd rather not do
this
> > one - not even sure it would work - because it involves the extra DLL.

> > Any suggestions would be greatly appreciated!

> > Dan



Tue, 21 Sep 2004 07:39:23 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. ANN: DirectX 9.0 Audio Player in C#

2. directX and C#

3. Is DirectX usable in C#?

4. C# and DirectX 8.1

5. C# and DirectX problem

6. DirectX in C#

7. C# with OpenGL/CSGL; DirectX 9

8. DirectX in C#

9. C# and DirectX or OpenGL

10. DirectX and C# (.NET)

11. directX or OpenGL from C#?

12. DirectX in C#

 

 
Powered by phpBB® Forum Software