I found a tutorial about making your first C++ DLL, which I wanted to try by making a function that calculates the octave number of a certain frequency. I first tried the example function, multiplying two values, and that worked. Then I put my calculating function, which I first tested in a standard c++ project, in the DLL code. Now when I want to call the function in Game Maker, it gives me this popup and when I click the OK button the program hangs. Does anyone have an idea what could cause this access violation?
Compiler information: I am using NetBeans IDE 7.3 in combination with Cygwin 4 (gcc). Compiled and tested on Windows 7.
DLL code:
#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;
}
Game Maker code:
//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.]
Regarding these exported functions:
Plug-in functions must have a specific format. They can have between 0 and 16 arguments, each of which can either be a real number (double in C) or a null-terminated string. (For more than 4 arguments, only real arguments are supported at the moment.) They must return either a real or a null-terminated string.
Yours returns an integer, not a double. Game Maker will be trying to interpret that integer as a double, which does not go well.