need to send a pointer in C#

  • Thread starter Thread starter vidalsasoon
  • Start date Start date
V

vidalsasoon

I need to send a pointer value to a fuction :( This is what it is
expecting.

BaseEffect.SetValue(Microsoft.DirectX.Direct3D.EffectHandle, void*,
int)

The problem is the void* part.

I'm tring to send an array of floats but the compiler doesn't like it.
Anyway I could push it through without having to resort to "unsafe"?
 
Is that quite true, Mattias? What about the IntPtr Structure?

That's not a pointer (not in the C# meaning anyway), it's a
pointer-sized integer.



Mattias
 
Mattias Sjögren said:
That's not a pointer (not in the C# meaning anyway), it's a
pointer-sized integer.

...that can be used to represent a pointer without going unmanaged, right?
Couldn't it be used to represent the pointer void* in the following method?

BaseEffect.SetValue(Microsoft.DirectX.Direct3D.EffectHandle, void*, int)

Chris.
 
I tried sending it "new IntPtr(myobject)" but the compiler still did
not like. I don't really understand what is going on behind the scenes
enough to troubleshoot.
 
vidalsasoon said:
I tried sending it "new IntPtr(myobject)" but the compiler still did
not like. I don't really understand what is going on behind the scenes
enough to troubleshoot.

Neither do I, I'm afraid. Wouldn't it be nice if Microsoft made the .NET
Framework source code available from within the VS.NET IDE <g> ?

Chris.
 
There is no SetValue method overload that takes a void*.
However, there is one overload that takes a System.Void
public void SetValue(
EffectHandle parameter,
void data,
int dataSize
);

Willy.
 
Well, compiler is expecting "void*". Besides, sending data as "void" in
C# makes my brain hurt.

I'll have to resort to unsafe call it seems. :(
 
There is no SetValue method overload that takes a void*.
However, there is one overload that takes a System.Void

Huh? Void can't be used as a parameter type. I don't know anything
about DirectX, but if the assembly really contains a method with such
a signature, it would be invalid and not callable.



Mattias
 
..that can be used to represent a pointer
Absolutely.


without going unmanaged

I suppose you mean unsafe. You're still calling out to unmanaged code.

Couldn't it be used to represent the pointer void* in the following method?

Well the parameter type already is void*, and to call such a method
you need to use unsafe. The alternative would be to modify the method
signature to accept an IntPtr instead of void*, but in the OP case
that didn't seem like an option.



Mattias
 
One problem with the idea of pointers in .Net is that the CLR will, at its
own discretion, move items around in managed memory. If you send the
address of a value in the CLR to another application, the item may not be at
that address by the time the receiving application goes to read it. Just
because there are ways to get pointers does not make them safe. The only
way to use a pointer in .Net is to use the
System.Runtime.InteropServices.Marshal class to move values outside of the
CLR managed memory space and into unmanaged memory.

DalePres
MCAD, MCDBA, MCSE
 
vidalsasoon said:
Well, compiler is expecting "void*". Besides, sending data as "void" in
C# makes my brain hurt.

I'll have to resort to unsafe call it seems. :(

A bunch of the D3D methods are using pointers internaly and some are taking
pointer arguments , as such these methods are marked unsafe and NON CLS
compliant. Wonder how VB.NET will handle this.

Following illustrates how one can pass a float array as void*

float[] buffer = new float[2];
buffer[0] = 1.0F;
buffer[1] = 2.0F;

unsafe
{
fixed(float* cptr = &buffer[0])
{
MyEffect.SetValue(..., (void*) cptr, sizeof(float) *
buffer.Length);
}
}

Willy.
 
Back
Top