我的DLL代码
demo.h:****************************
#ifndef __DEMO_H__
#define __DEMO_H__typedef void (*pfun)(int n);
pfun pfunc;#define DLL_EXPORT extern"C" __declspec(dllexport)DLL_EXPORT void __stdcall GetFunc(pfun p);#endif
***************************demo.cpp***************************
#include"demo.h"DLL_EXPORT void __stdcall GetFunc(pfun p)
{
pfunc = p;
pfunc(123);                         //给出的是123
}
***************************demo.def***************************
LIBRARY "demo"EXPORTS
GetFunc
***************************这是delphi调用DLL的代码***************************
unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;const mydll = 'demo.dll';type
  TFunc = procedure(n: integer);
  TPFunc = procedure(p: TFunc);stdcall;  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;  procedure demo(n: integer);var
  Form1: TForm1;implementationprocedure demo(n: integer);
begin
  showmessage(format('%d',[n]));   //n不是123,是极大的数,这是为什么?
end;{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var wnd: HWND; ptr: TFarProc;
begin
  wnd := LoadLibrary('demo.dll');
  if wnd > 32 then begin
    ptr := GetProcAddress(wnd, 'GetFunc');
    if assigned(ptr) then
      TPFunc(ptr)(@demo);
    FreeLibrary(wnd);
  end;
end;end.
*************************************应该在delphi中显示123才会呢

解决方案 »

  1.   

    是声明demo函数没加stdcall,应是
    procedure demo(n: integer);stdcall;不过第一次运行对了,可接着就运行第二次,还是错误结果,结果就系统出错了
      

  2.   

    指针我用的也不好,给你发个简单的int _stdcall AddTz(int iNum)
    {
    return (iNum + 5);
    }function  AddTz(iNum: Integer): Integer;  stdcall; external 'demo.dll' name 'AddTz';procedure TForm1.Button1Click(Sender: TObject);
    begin
      ShowMessage(IntToStr(AddTz(1))); //1+5=6
    end;
      

  3.   

    dll写得没问题,你的调用有问题
      

  4.   

    谢谢楼上的回贴!
    这个我也知道,我想让DLL不时运行我delphi的函数demo,不是由delphi主动调用的
      

  5.   

    delphi的dll和cb规则是相同的,请参考本人拙帖
    http://topic.csdn.net/u/20090302/17/99d1fbc5-4be6-4744-96d9-09b16c0d93c3.html
      

  6.   

    啊,不好意思啊,没细看,原来是回调函数,
    现在俺手底有活,你搜一下吧,delphi 回调函数
    delphi5 程序员开发指南 关于回调函数有极其详细的讲解还有,回调现在不流行了,用消息不一样的吗?(纯属个人臆想,不要笑话我,可以PK!!)
      

  7.   

    typedef void (__stdcall *pfun)(int n); 
      

  8.   

    果然是typedef void (__stdcall *pfun)(int n);
    多谢楼上,也谢谢回贴的程友,结贴了