【发布时间】:2012-10-25 18:50:52
【问题描述】:
我在使用 delphi 制作的 dll 库时遇到问题。我没有它的来源——只有 dll 和接口。我想在我的 c++ 程序中导入它。我不需要使用 QT 进行加载(但这可能是最好的选择)——标准的 Windows api 也适用。
首先,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}.
QT版本:
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]);
}
- 调用了函数并返回了一些输出,但它是垃圾。如您所见,我传递了内部向量的数组 (&v_double[0])。我尝试在堆栈上使用双数组,但函数尚未完成。谁能解释一下为什么?
-
为什么我没有看到正确的结果?我在 C# 中有类似的代码,它可以工作:
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]; } } }
结构可能是实际问题吗?
这是我使用 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);
}
感谢您的帮助。
【问题讨论】:
-
我正在使用 mingw 4.4 和 qt 4.8。
-
VoicePrint.dll 和 s.txt 在这里:speedyshare.com/naVrr/lib.rar。 s.txt 定义了需要传递给 VP 函数的双精度数。