Trying to marshal a float[,]

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • coriscow
    New Member
    • Feb 2008
    • 5

    Trying to marshal a float[,]

    Hi all!!

    I am developing an app for a Pocket PC platform on C#. However, I need to make use of a C written dll. The problem arises when calling the function
    .........
    int capture_input( float32 ***cepstrum, int nrOfSeconds, int playBack, int *nrOfCoeff );
    .........

    where it returns an int (not an error code), a bidimensional array allocated within the function (cepstrum) and another integer (nrOfCoeff). I call the function from C# managed code using the following declaration and code:

    .........
    [DllImport ( "JanitorMobiled ll.dll", EntryPoint = "capture_in put" )]
    private static extern int capture_input ( float[,] cepstrum, int nrOfSeconds,
    int playback, ref int nrOfCepstra );
    .........
    int numberOfFrames = 0;
    int numberOfCepstra = 0;
    float[,] cepstrum;
    .........
    numberOfFrames = JHandShaker.cap ture_input ( cepstrum, maxNrOfSeconds, 1, ref numberOfCepstra );
    .........

    The problem arises in the unmanaged code when redirecting the received pointer to the allocated memory region (*cepstrum=tmp_ cep;).

    What am I doing wrong??

    Thanks!!!!
  • RedSon
    Recognized Expert Expert
    • Jan 2007
    • 4980

    #2
    Originally posted by coriscow
    Hi all!!

    I am developing an app for a Pocket PC platform on C#. However, I need to make use of a C written dll. The problem arises when calling the function
    .........
    int capture_input( float32 ***cepstrum, int nrOfSeconds, int playBack, int *nrOfCoeff );
    .........

    where it returns an int (not an error code), a bidimensional array allocated within the function (cepstrum) and another integer (nrOfCoeff). I call the function from C# managed code using the following declaration and code:

    .........
    [DllImport ( "JanitorMobiled ll.dll", EntryPoint = "capture_in put" )]
    private static extern int capture_input ( float[,] cepstrum, int nrOfSeconds,
    int playback, ref int nrOfCepstra );
    .........
    int numberOfFrames = 0;
    int numberOfCepstra = 0;
    float[,] cepstrum;
    .........
    numberOfFrames = JHandShaker.cap ture_input ( cepstrum, maxNrOfSeconds, 1, ref numberOfCepstra );
    .........

    The problem arises in the unmanaged code when redirecting the received pointer to the allocated memory region (*cepstrum=tmp_ cep;).

    What am I doing wrong??

    Thanks!!!!
    Hi, next time don't forget about our "[.CODE]" tags. In your code you I believe you are marshaling arrays and reference types. In you method signature might need to be something like:
    Code:
    private static extern int capture_input ( ref float[,] cepstrum, int nrOfSeconds, int playback, ref int nrOfCepstra );
    or you might want to try an array of reference floats that point to the first element of each other array, like an array of pointers to pointers, if that makes sense.

    Are you writing the source of the unmanaged bit as well?

    Comment

    • coriscow
      New Member
      • Feb 2008
      • 5

      #3
      Hi Redson!

      Sory for the "[.CODE]" tags thing, I'll do it from now on.

      I have access to the unmanaged code, but I cannont change it. About the 'ref' suggestion, I dont know why but it raises a NotSupportedExc eption. And about the array of pointers, it is a good one, but as I cannot change the unmanaged code....

      Would it be possible to use a [In, Out] IntPtr ? But how can I cast it into a float[,]??

      Regards!!!

      Comment

      • RedSon
        Recognized Expert Expert
        • Jan 2007
        • 4980

        #4
        Originally posted by coriscow
        Hi Redson!

        Sory for the "[.CODE]" tags thing, I'll do it from now on.

        I have access to the unmanaged code, but I cannont change it. About the 'ref' suggestion, I dont know why but it raises a NotSupportedExc eption. And about the array of pointers, it is a good one, but as I cannot change the unmanaged code....

        Would it be possible to use a [In, Out] IntPtr ? But how can I cast it into a float[,]??

        Regards!!!
        Hmm... well looking at it intuitively an IntPtr is just a 32 bit pointer to any object in memory. So you wouldn't need to cast it as a float but just dereference the first element as element [0,0] of your array. Alternatively you can create an IntPtr[] where each index points to the first element of your float arrays?

        You are a little bit past the limit of my knowledge about these things so I'm speculating somewhat. Try this: post in the .NET forum with a link to this post and say "Can anyone help me with this?". You might also need to try the MSDN forums if you haven't already.

        Comment

        Working...