0

I'm invoking a C++ function from within C#.

This is the function header in C++ :

int src_simple (SRC_DATA *data, int converter_type, int channels) ;

And this is the equivilent C# function :

[DllImport("libsamplerate-0.dll")]
    public static extern int src_simple(ref SRC_DATA sd, int converter_type, int channels);

This is the SRC_DATA structure in C++ and then in C# :

 typedef struct
  {   float  *data_in, *data_out ;

      long   input_frames, output_frames ;
      long   input_frames_used, output_frames_gen ;

      int    end_of_input ;

      double src_ratio ;
  } SRC_DATA ;

This is the C# struct I have defined :

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct SRC_DATA
{

    public IntPtr data_in, data_out;

    public long input_frames, output_frames;
    public long input_frames_used, output_frames_gen;

    public int end_of_input;
    public double src_ratio;

}

The big problem is that the last parameter , src_ratio, doesn't get delivered properly to the C++ function, (it sees it as 0 or something invalid).

Are my declarations correct?

Thanks

3 Answers 3

4

Are you sure that the problem is in src_ratio member? long in C# is 64 bit. In C++ on Win32 platform long is 32 bit, I think you need to use int in C# for long C++ structure members. Also, Pack = 1 looks a bit strange, do you use the same structure member alignment in C++?

Sign up to request clarification or add additional context in comments.

1 Comment

You were right, pack = 1 was a mistake.. I just let it use the default :)
1

You force packing in C# but not in C++. What could be happening is that the C++ compiler is padding the 7 in32's with four additional bytes to ensure the double is 8-byte aligned.

Check the #pragma pack compiler directives.

Comments

0

Check what size an int is in your C++ compiler. A C# int is always 32 bits.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.