Re: Problems calling a C function containing pointers.



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
>


.



Relevant Pages

  • Re: 8 bit image
    ... public int biHeight; ... public static extern IntPtr SelectObject ... ref BITMAPINFO ...
    (microsoft.public.dotnet.framework.drawing)
  • Re: Problems using ordinary GDI operations on 32bit Bitmap
    ... public const int transparent = 1; ... public static extern bool AlphaBlend( ... public static extern IntPtr SelectObject ... private void InitializeComponent() ...
    (microsoft.public.win32.programmer.gdi)
  • Re: 8 bit image
    ... > public static extern IntPtr SelectObject ... > public static extern bool BitBlt(IntPtr hdcDest, int nXDest, int nYDest, ... > public static extern IntPtr CreateCompatibleDC(IntPtr hdc); ...
    (microsoft.public.dotnet.framework.drawing)
  • Re: Send Message(string) c# exe to vc++ exe(unmanaged) using WindowsSendMessage
    ... public static extern int FindWindow(string strClassName,string ... LRESULT CTestMfcSendMsgExeDlg::OnDeleteAll(WPARAM wParam, LPARAM lParam) ... public static extern IntPtr SendMessage; ...
    (microsoft.public.dotnet.languages.csharp)
  • Problems calling a C function containing pointers.
    ... It returns a value at the address of the pointer. ... public static extern Char SomeFunction(ref int numberOfRays, IntPtr ...
    (microsoft.public.dotnet.languages.csharp)