Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3977330
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:54:39+00:00 2026-05-20T04:54:39+00:00

I have a C++ DLL with an exported function: extern C __declspec(dllexport) double* fft(double*

  • 0

I have a C++ DLL with an exported function:

extern "C" __declspec(dllexport) double* fft(double* dataReal, double* dataImag)
{
  [...]
}

The function calculates the FFT of the two double arrays (real and imaginary) an returns a single double array with the real an imaginary components interleaved: { Re, Im, Re, Im, … }

I’m not sure how to call this function in C#.
What I’m doing is:

[DllImport("fft.dll")]
static extern double[] fft(double[] dataReal, double[] dataImag);

and when I test it like this:

double[] foo = fft(new double[] { 1, 2, 3, 4 }, new double[] { 0, 0, 0, 0 });

I get a MarshalDirectiveException exception:

Cannot marshal ‘return value’: Invalid managed/unmanaged type combination.

I’m assuming this is because C++ double* isn’t quite the same as C# double[], but I’m not sure how to fix it. Any ideas?

Edit:
I’ve changed the signatures so that I now pass some extra information:

extern "C" __declspec(dllexport) void fft(double* dataReal, double* dataImag, int length, double* output);

We always know the length of output will be 2x length

and

[DllImport("fft.dll")]
static extern void fft(double[] dataReal, double[] dataImag, int length, out double[] output);

tested like this:

double[] foo = new double[8];
fft(new double[] { 1, 2, 3, 4 }, new double[] { 0, 0, 0, 0 }, 4, out foo);

Now I’m getting an AccessViolationException rather than a MarshalDirectiveException.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T04:54:39+00:00Added an answer on May 20, 2026 at 4:54 am

    There are a few problems with your example:

    1. The C++ code has no idea how big those arrays are. The marshaller will pass them a valid pointer, but without a corresponding length parameter, there’s no way to tell how big they are. sizeof(dataReal) and sizeof(dataImag) is likely 4 or 8 on most platforms (i.e. sizeof(void*)). Probably not what you intended.
    2. While it’s possible to marshal a pointer back as the return value (you could then use it to populate a managed array), there is no implied owner of the return value’s memory, leaving open the possibility of memory leaks. Was the buffer allocated inside of fft with new? If so, then you’d need another export the managed code can call to free the memory or use LocalAlloc instead (and then Marshal.FreeHGlobal on the managed side). That’s problematic at best.

    Instead, my suggestion would be to define fft this way:

    
    extern "C" __declspec(dllexport) void __stdcall fft(double const* dataReal, int dataRealLength, double const* dataImag, int dataImagLength, double* result, int resultLength)
    {
      // Check that dataRealLength == dataImagLength
      // Check that resultLength is twice dataRealLength
    }
    

    The corresponding P/Invoke signature would be:

    
    [DllImport("fft.dll")]
    static extern void fft(double[] dataReal, int dataRealLength, double[] dataImag, int dataImagLength, double[] result, int resultLength);
    

    And then an example of a call:

    
    double[] dataReal = new double[] { 1.0, 2.0, 3.0, 4.0 };
    double[] dataImag = new double[] { 5.0, 6.0, 7.0, 8.0 };
    double[] result = new double[8];
    fft(dataReal, dataReal.Length, dataImag, dataImag.Length, result, result.Length);
    

    Edit: updating based on what fft is described to do.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two WIN32 DLL projects in the solution, main.dll should call a function
I have a DLL one.dll that uses a class TwoClass exported from two.dll via
I have two DLL files that have the same namespace but they have different
I have a function that is exported by a C library with the following
I have to use a simple function from a DLL; I am able to
I have a DLL with some COM objects . Sometimes, this objects crashes and
We have a DLL used as the middle layer between our website front end
I have a dll that must be useable from C etc, so I cant
I have a dll that was written in c++, I need to use this
I have a dll that contains a dot net assembly - common intermediate language.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.