Re: Problems calling a C function containing pointers.
- From: "Nicholas Paldino [.NET/C# MVP]" <mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 20 Jul 2005 09:52:24 -0400
Johann,
The first declaration should work, but you have to correct the return
type:
public static extern byte SomeFunction(ref int number ofRays, IntPtr
rayDirs);
The reason for this is that a char in .NET is a 16-bit value, whereas in
C it is an 8-bit value.
You just have to make sure that you allocate the memory for the array
which the IntPtr points to before you make the call.
If you can use unsafe code, then you can simplify a lot of this by just
declaring the function with pointers, and then declare a pointer to the
array.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx
"Johann Blake" <johannblake@xxxxxxxxx> wrote in message
news:1121867120.961749.212560@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
>I need to call a C DLL function. The first parameter expects a pointer
> to a long. It returns a value at the address of the pointer. The second
> parameter expects a pointer to a pointer. It creates an array and
> returns the data it stores in this array.
>
> I have tried many different combinations of P/Invoke declarations and
> none have worked. When I step from my c# code into the c code, oNumRays
> points to 0x00000000 and when it attempts to assign a value, an error
> gets generated (something about object not set). Here are the
> declarations I have tried:
>
> public static extern Char SomeFunction(ref int numberOfRays, IntPtr
> rayDirs);
> public static extern Char SomeFunction(out int numberOfRays, IntPtr
> rayDirs);
> public static extern Char SomeFunction(IntPtr numberOfRays, IntPtr
> rayDirs);
> public static extern Char SomeFunction([MarshalAs(UnmanagedType.U4)]
> out int numberOfRays, IntPtr rayDirs);
> public static extern Char SomeFunction([MarshalAs(UnmanagedType.U4)]
> int numberOfRays, IntPtr rayDirs);
>
>
> Here is the C function...
>
> static float sRayDirs[8];
>
> GuiInterface_API char SomeFunction(long* oNumRays, float** oRayDirs)
> {
> *oNumRays = 12;
>
> sRayDirs[0] = 1.1f;
> sRayDirs[1] = 2.2f;
> sRayDirs[2] = 3.3f;
> sRayDirs[3] = 4.4f;
> sRayDirs[4] = 5.5f;
> sRayDirs[5] = 6.6f;
> sRayDirs[6] = 7.7f;
> sRayDirs[7] = 8.8f;
>
> *oRayDirs = sRayDirs;
>
> return 0;
> }
>
> Any suggestions on how to make this work?
>
> Thanks for your help!
> Johann Blake
>
.
- Follow-Ups:
- Re: Problems calling a C function containing pointers.
- From: Johann Blake
- Re: Problems calling a C function containing pointers.
- References:
- Problems calling a C function containing pointers.
- From: Johann Blake
- Problems calling a C function containing pointers.
- Prev by Date: Problems calling a C function containing pointers.
- Next by Date: Re: Problems calling a C function containing pointers.
- Previous by thread: Problems calling a C function containing pointers.
- Next by thread: Re: Problems calling a C function containing pointers.
- Index(es):
Relevant Pages
|