编写一个DLL,其中有个function需要传入一个i:integer.我在DLL中是这样申明的:
function func1(const i: integer);integer; stdcall; export;function func1(const i: integer):integer;
begin
  Showmessage(IntToStr(i) + #13 + ' -- DLL中的函数"func1"被成功调用了!');
  func1:= i;
end;
在调用时:
procedure InvokeFunc(i :integer);external 'PrjDLL.dll' name 'func1';实现:
procedure TForm1.Button2Click(Sender: TObject);
var
  i: integer;
  s: string;
begin
  if InputQuery('输入','输入要传的数字', s) then
    i := StrToInt(s);
  InvokeFunc(i);
end;当输入了数字,如:输入8 ,显示的并不是期望的:“8-- DLL中的函数"func1"被成功调用了!”
而是“1243260-- DLL中的函数"func1"被成功调用了!”,而且不论输入什么数字都是显示“1243260-- DLL中的函数"func1"被成功调用了!”。我估计是读到了别的内存地址的内容了。删除stdcall一切正常。÷我想问的是:stdcall是起什么作用呢?一般什么时候需要用到?

解决方案 »

  1.   

    聲明要一致:在调用时:
    function InvokeFunc(i :integer):integer; stdcall;external 'PrjDLL.dll' name 'func1';
      

  2.   

    to  aiirii(ari-爱的眼睛):  那加了stdcall 和不加stacall 有什么区别呢?也就是说stdcall有什么作用?(D5开发者指南上好像没说,偶不太懂)。  请教!
      

  3.   

    这个牵扯到参数的入栈顺序
    stdcall是一种基本的处理函数参数的方式,是从C语言带过来的。按照从右往左的的顺序入栈。而你什么都不加,是默认的pascal参数入栈方式。是从左往右顺序入栈。如果你要用,一定要把参数入栈的方式声明为一致的,如果出现类似问题,请参照Delphi帮助第**********页,索引stdcall那一行。参数入栈方式还有其他的三种……解释如下:When you declare a procedure or function, you can specify a calling convention using one of the directives register, pascal, cdecl, stdcall, and safecall. For example,function MyFunction(X, Y: Real): Real; cdecl; ...Calling conventions determine the order in which parameters are passed to the routine. They also affect the removal of parameters from the stack, the use of registers for passing parameters, and error and exception handling. The default calling convention is register.The register and pascal conventions pass parameters from left to right; that is, the left most parameter is evaluated and passed first and the rightmost parameter is evaluated and passed last. The cdecl, stdcall, and safecall conventions pass parameters from right to left.
    For all conventions except cdecl, the procedure or function removes parameters from the stack upon returning. With the cdecl convention, the caller removes parameters from the stack when the call returns.The register convention uses up to three CPU registers to pass parameters, while the other conventions pass all parameters on the stack.
    The safecall convention implements exception 揻irewalls.?On Windows, this implements interprocess COM error notification.The table below summarizes calling conventions.Directive Parameter order Clean-up Passes parameters in registers?
    register Left-to-right Routine Yes
    pascal Left-to-right Routine No
    cdecl Right-to-left Caller No
    stdcall Right-to-left Routine No
    safecall Right-to-left Routine No
    The default register convention is the most efficient, since it usually avoids creation of a stack frame. (Access methods for published properties must use register.) The cdecl convention is useful when you call functions from shared libraries written in C or C++, while stdcall and safecall are recommended, in general, for calls to external code. On Windows, the operating system APIs are stdcall and safecall. Other operating systems generally use cdecl. (Note that stdcall is more efficient than cdecl.)The safecall convention must be used for declaring dual-interface methods. The pascal convention is maintained for backward compatibility. For more information on calling conventions, see Program control.
    The directives near, far, and export refer to calling conventions in 16-bit Windows programming. They have no effect in 32-bit applications and are maintained for backward compatibility only.
      

  4.   

    Ps:D5开发人员指南一定说了。只不过你没有看到,如果出现类似的问题,最好还是看帮助,没有比Delphi的帮助文件更详细地解释了唉……………………
      

  5.   

    When you declare a procedure or function, you can specify a calling convention using one of the directives register, pascal, cdecl, stdcall, and safecall. For example,function MyFunction(X, Y: Real): Real; cdecl;
     ...Calling conventions determine the order in which parameters are passed to the routine. They also affect the removal of parameters from the stack, the use of registers for passing parameters, and error and exception handling. The default calling convention is register.The register and pascal conventions pass parameters from left to right; that is, the left most parameter is evaluated and passed first and the rightmost parameter is evaluated and passed last. The cdecl, stdcall, and safecall conventions pass parameters from right to left.
    For all conventions except cdecl, the procedure or function removes parameters from the stack upon returning. With the cdecl convention, the caller removes parameters from the stack when the call returns.The register convention uses up to three CPU registers to pass parameters, while the other conventions pass all parameters on the stack.
    The safecall convention implements exception "firewalls." On Windows, this implements interprocess COM error notification.The table below summarizes calling conventions.Calling conventions 
    Directive Parameter order Clean-up Passes parameters in registers?
    register Left-to-right Routine Yes
    pascal Left-to-right Routine No
    cdecl Right-to-left Caller No
    stdcall Right-to-left Routine No
    safecall Right-to-left Routine No
    The default register convention is the most efficient, since it usually avoids creation of a stack frame. (Access methods for published properties must use register.) The cdecl convention is useful when you call functions from shared libraries written in C or C++, while stdcall and safecall are recommended, in general, for calls to external code. On Windows, the operating system APIs are stdcall and safecall. Other operating systems generally use cdecl. (Note that stdcall is more efficient than cdecl.)The safecall convention must be used for declaring dual-interface methods. The pascal convention is maintained for backward compatibility. For more information on calling conventions, see Program control.
    The directives near, far, and export refer to calling conventions in 16-bit Windows programming. They have no effect in 32-bit applications and are maintained for backward compatibility only.
    看不懂???????????????????????????????