初めての C++ DLL の作成に関するチュートリアルを見つけました。特定の周波数のオクターブ数を計算する関数を作成して試してみたかったのです。最初にサンプル関数を試し、2 つの値を乗算しましたが、うまくいきました。次に、最初に標準の C++ プロジェクトでテストした計算関数を DLL コードに入れます。Game Maker で関数を呼び出すと、このポップアップが表示され、[OK] ボタンをクリックするとプログラムがハングします。このアクセス違反の原因を知っている人はいますか?
コンパイラ情報: 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.]