1

我找到了一个关于制作你的第一个 C++ DLL 的教程,我想通过制作一个计算某个频率的八度数的函数来尝试。我首先尝试了示例函数,将两个值相乘,并且成功了。然后我将我首先在标准 c++ 项目中测试的计算函数放入 DLL 代码中。现在,当我想在 Game Maker 中调用该函数时,它会给我这个弹出窗口,当我单击“确定”按钮时,程序会挂起。有谁知道什么可能导致这种访问冲突?

编译器信息:我将 NetBeans IDE 7.3 与 Cygwin 4 (gcc) 结合使用。在 Windows 7 上编译和测试。

DLL 代码:

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <cstdio>
#include <windows.h>

#define GMEXPORT extern "C" __declspec (dllexport)

double A440 = 440;

GMEXPORT double __cdecl SampleFunction(double a, double b) {
    return a * b;
}

GMEXPORT int __cdecl freqGetOctave(double f) {
  double a = 12*log2(f/(A440/16))+.505;
  int c = (int)(a+9)/12;
  return c;
}

游戏制作者代码:

//script: dll_init
globalvar _exmpl,_freq;
var dll_name;
dll_name = "c:\Users\<me>\Documents\NetBeansProjects\GMDLLtest\dist\Debug\Cygwin_4.x-Windows\libGMDLLtest.dll";
_exmpl = external_define(dll_name, "SampleFunction", dll_cdecl, ty_real, 2, ty_real, ty_real);
_freq  = external_define(dll_name, "freqGetOctave", dll_cdecl, ty_real, 1, ty_real);

//script: example
return external_call(_exmpl,argument0,argument1);

//script: freq_octave
return external_call(_freq,argument0);

//Watch Excpressions in Debug Screen:
example(3,3)        9
freq_octave(440)    [error popped up:]

//    [Access violation at address 00405B33 in module 'DLLtest.exe'.
//     Read of access 00000004.]
4

1 回答 1

1

关于这些导出的函数:

插件函数必须具有特定格式。它们可以有 0 到 16 个参数,每个参数可以是实数(在 C 中为双精度数)或以 null 结尾的字符串。(对于超过 4 个参数,目前仅支持实参数。)它们必须返回实数或以空字符结尾的字符串。

你的返回一个整数,而不是一个双精度数。Game Maker 将尝试将该整数解释为双精度数,但效果不佳。

于 2014-01-23T15:04:53.290 回答