John Woodiwiss

Hello everyone.

I hope that someone out there can help me.

I have searched everywhere I can think of, but cannot find any reference to pointers to arrays and they appear to behave differently to pointers to single values.

I am attempting to talk to a C DLL whose signature is

void SDA_Multiply( const SFLOAT* Source array pointer
SFLOAT* Destination array pointer
const SFLOAT Scalar multiplier
const SFIX Array length

where SFLOAT is a double and SFIX is a long

My problem is that I don't know how to pass pointers to arrays in an unsafe environment.

I understand that I need to use System.Runtime.InteropServices.

I also understand that I need to use

[DllImport("siglib.dll")]
public static extern void SDA_Multiply( double*[] Data, double*[] Result, double Multiplier, long Points);

However, this is where I run out of steam!

Assuming that I have defined the following:

public long Points = 10;

public double Multiplier = 2;
public double[] Data = new double[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
public double[] Result = new double[10] { 2, 3, 4, 5, 6, 7, 8,9,10,11};

how do I pass Data and Result into

SDA_Multiply( Data , result , Multiplier , Points )

Any help would be desperately welcome.

Code examples especially welcome as my sorry, tired brain can usually make more sense of this!!!

John Woodiwiss

Live long, prosper & understand C#



Re: Visual C# General C# using unmanaged code which requires pointers to arrays

marco.ragogna

With byte type I define the DLLimport to the "C" function in this way:

[DllImport("Unmanaged.dll", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Ansi)]
public static extern int ReadData (uint startOffset, uint length, ref byte dataBuffer);

and when I use it

byte[] DataBuffer = new byte[2000];
int result = ReadData (0, DataBuffer.Length, ref dataBuffer[0]);

I don't know if it's the correct way, but it works




Re: Visual C# General C# using unmanaged code which requires pointers to arrays

John Woodiwiss

Hi Marco,

Many thanks for your reply.

I tried this:

[DllImport("siglib.dll",CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Ansi)]

public static extern void SDA_Multiply( ref double[] Source, ref double[] Result, double Multiplier, long Points);

using the following call

SDA_Multiply( ref Data[0], ref Result[0], Multiplier, Points);

gave Argument 1 & 2 cannot convert from ref double to ref double[]

using the following call

SDA_Multiply( ref Data, ref Result, Multiplier, Points);

gave

PInvokeStackImbalance was detected
Message: A call to PInvoke function 'TestSigLib!TestSigLib.Form1::SDA_Multiply' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature
.

There is obviously something different in the way that pointers are used with unmanaged code, but I can't find it.

Any one out there know what I am doing wrong

John Woodiwiss





Re: Visual C# General C# using unmanaged code which requires pointers to arrays

marco.ragogna

try this:

[DllImport("siglib.dll",CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Ansi)]

public static extern void SDA_Multiply( ref double Source, ref double Result, double Multiplier, long Points);

with the following call:

SDA_Multiply( ref Data[0], ref Result[0], Multiplier, Points);





Re: Visual C# General C# using unmanaged code which requires pointers to arrays

John Woodiwiss

Hi Marco,

Thanks for the suggestion.

I tried that but got the same error

PInvokeStackImbalance was detected
Message: A call to PInvoke function 'TestSigLib!TestSigLib.Form1::SDA_Multiply' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.

However, the values returned in Result are now correct!

Any more ideas

John Woodiwiss