2

I have a problem with using dll library made in delphi. I don't have sources for it - only dll and interface. I want to import it in my c++ program. I don't need to use QT for loading (but it would be probably the best option as ) - also standard Windows api is applicable.

First, interface of the dll:

Unit VPInterface;
interface

type
 TSignal     = array of double;
 ShortVector = array[0..11] of double;

function VP(s: TSignal): ShortVector; stdcall;
 stdcall external 'VoicePrint.dll' name 'VP';

implementation

End {VPInterface.pas}.

The QT version:

typedef double* (*VPFunct)(double[]);
vector<double> v_double;
// put some values in v_double
QLibrary lib("VoicePrint.dll");
VPFunct vpfunct = (VPFunct) lib.resolve("VP");
if (vpfunct) {
  double * res = vpfunct(&v_double[0]);
}
  1. The function is called and some output is returned but it's rubbish. As you can see I passed internal vector's array (&v_double[0]). I tried with double array on stack but the function haven't finished. Can someone explain it why?
  2. Why I don't see correct results? I have similar code in C# and it works:

    namespace WAT_DLL_TEST
    {
    public class VoicePrint
    {
        [DllImport("VoicePrint.dll", CharSet = CharSet.Ansi, EntryPoint = "VP", CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.Struct)]
        public static extern ShortVector VP(double[] signal);
    
    
        [StructLayout(LayoutKind.Explicit, Size = 48)]
        public unsafe struct ShortVector
        {
            [FieldOffset(0)]
            public fixed double m_buffer[6];
        }
    
    }
    }
    

May a structure be the actual problem?

And here is my code for running it using windows api:

HINSTANCE hinstDLL;
VPFunct myFunct;
BOOL fFreeDLL;

hinstDLL = LoadLibrary(L"VoicePrint.dll");
if (hinstDLL != NULL)
{
    myFunct = (VPFunct) GetProcAddress(hinstDLL, "VP");
    if (myFunct != NULL) {
        double * dptr = myFunct(&v_double[0]);
        for(int i=0; i<11; i++)
            cout << dptr[i] << endl;
    }
    fFreeDLL = FreeLibrary(hinstDLL);
}

Thanks for any help.

4

1 回答 1

0

你应该在你的typedef

typedef double* (__stdcall *VPFunct)(double[]);

无效的调用转换会导致堆栈损坏。它可以解释每一个不正确的行为。

您可以尝试使用以下代码模拟 Delphi 动态数组内存布局(此处提到):

#pragma pack (push, 1)

template <uint32_t size>
struct DelphiArray
{
    uint32_t _refCounter;
    uint32_t _size;
    double array[size];
};
#pragma pack (pop)

DelphiArray然后像这样调用函数

DelphiArray<N> arr;
arr._refCounter = 1;
arr._size = N;
//fill arr.array here
myFunc(arr.array);
于 2012-10-25T18:58:53.543 回答