1
我發現了一個tutorial關於製作你的第一個C++ DLL,我想通過一個函數來計算某個頻率的八度數。我首先嚐試了示例函數,將兩個值相乘,然後運行。然後,我把我的計算函數,我第一次在一個標準的C++項目中測試,在DLL代碼中。現在,當我想在Game Maker中調用函數時,它會給我this popup,當我單擊確定按鈕時,程序將掛起。 有沒有人有一個想法可能會導致此訪問衝突?爲什麼我的遊戲製作DLL會導致訪問衝突?
編譯器信息:我將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.]
解引用'NULL'指針的典型症狀。 – 2014-01-23 14:11:39
一個可能的錯誤是你的C++函數有一個'int'返回類型,但你在'external_define'調用中聲明它爲'ty_real'。 (查看文檔,看起來您只能使用'real's或'string's) – benjymous
運行調試器並編寫測試用例的時間。記錄:http://coliru.stacked-crooked.com/a/22f1a4962bd01c34 –