Delphi 7调用C++编写的dll 出现"nvalid pointer operation"错误
- C/C++ code
#include "stdafx.h"#include "Calc.h"#define input_min 750#define input_max 1800#define output_min 2500000#define output_max 4500000#define pi 3.1415926 int Calc::GetCalc(unsigned int headfre,double cirlen,double wu,double tvstep) { double grave_N,nt1,nt2,nt3; nt1 =cirlen / (wu / 2.0); nt2 = (unsigned int)(nt1/15) * 15; nt3 = (unsigned int)(nt2/2) * 2; if(nt3 == nt2) if(nt2 > nt1) grave_N = nt2 - 15; else grave_N = nt2 + 15; else grave_N = nt2; double h; h = cirlen * 2.0 / grave_N ; double Yn,Xn; Yn = h * 1000; Xn = tvstep *1000; double arf; arf = atan(Yn/Xn); double thita; thita = arf * 180 / pi; double spinh, carh, sineh; double spin_ab,spin_z; double speed; speed = Yn * headfre / (cirlen * 1000); spinh = speed * 3000; carh = Xn / 0.08 * speed; unsigned int spin_m_t, car_m_t; spin_m_t = (unsigned int)(spinh * pow(2.0,32) / 60000000 + 0.5); spinh = 60000000 / pow(2.0,32) * spin_m_t; spin_z = spinh / 3000; spin_ab = spinh * 12; carh = carh = Xn / 0.08 * speed; unsigned int fp,bp,sm; double pll,diff,least = 10.0; pll_div = 0; for(fp=1;fp<350;fp++) { if((spin_ab/fp < input_min) || (spin_ab/fp > input_max)) continue; for(bp=2;bp<3000;bp++) { if((spin_ab / fp * bp < output_min) || (spin_ab / fp * bp > output_max)) continue; pll = spin_ab / fp * bp; sm = (unsigned int)(pll / 32 / 7500 + 0.5); diff = fabs(pll - sm * 1.0 * 32 * 7500); if(diff < least) { least = diff; pll_div = fp; pll_mul = bp; sine_m = (unsigned int)(pll / 32 / 7500 + 0.5); } } } if(pll_div == 0) return 1; pll = spin_ab / pll_div * pll_mul; car_m_t = (unsigned int)(carh * pow(2.0,32) / pll + 0.5); carh = pll / pow(2.0,32) * car_m_t; sineh = pll / 32 / sine_m; spin_m = spin_m_t; car_m = car_m_t; return 0;}“头文件”:#ifndef CALC_H#define CALC_H#endifclass Calc{ public: unsigned int headfre; double cirlen; double wu; double tvstep; unsigned int spin_m;unsigned int car_m;unsigned int pll_div;unsigned int pll_mul;unsigned int sine_m; int _stdcall GetCalc(unsigned int headfre,double cirlen,double wu,double tvstep);};- Delphi(Pascal) code
unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) Button1: TButton; Edit1: TEdit; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1; headfre:integer; cirlen:double; wu:double; tvstep:double;implementation{$R *.dfm}function GetCalc(headfre:integer;cirlen:double;wu:double;tvstep:double):integer;stdcall;External 'CalcGravure.dll';procedure TForm1.Button1Click(Sender: TObject);begin headfre:= 7500; cirlen:= 570; wu:= 0.1725; tvstep:= 0.1152; Edit1.Text:=IntToStr(GetCalc(headfre,cirlen,wu,tvstep));end;end.[解决办法]
交错性的调用应该注意格式及接口的
[解决办法]
unsigned int 是否应该声明成word。
另外需要注意的地方也就是stacall 是否正确,最后没办法,也可以更换成其他的测试一下。
[解决办法]
先弄个非类函数试试,看是不是因为类的缘故... 仅做参考
[解决办法]
VC 里的INT 型是16位的,而DELPHI中的Integer是32位的,所以,Delphi中的Word型才能对应VC中的int型