链节文件如下:library mymath;{ Important note about DLL memory management: ShareMem must be the
  first unit in your library's USES clause AND your project's (select
  Project-View Source) USES clause if your DLL exports any procedures or
  functions that pass strings as parameters or function results. This
  applies to all strings passed to and from your DLL--even those that
  are nested in records and classes. ShareMem is the interface unit to
  the BORLNDMM.DLL shared memory manager, which must be deployed along
  with your DLL. To avoid using BORLNDMM.DLL, pass string information
  using PChar or ShortString parameters. }uses
  SysUtils,
  Windows,
  Classes,
  Messages,
  Graphics, Controls, Forms,ExtCtrls,
  Dialogs;{$R *.res}
procedure transfer(s:string);
begin
  Application.MessageBox(PChar(s),'提示',0);
end;function sqr(num,number:integer):integer;export;
begin
sqr:=num*num*number;
end;exports
  sqr,transfer;
begin
end.
链节文件执行后是成功的。
C#调用Delphi7.0写的链节文件mymath.dll代码如下:
        [DllImport("mymath.dll", EntryPoint = "sqr", CharSet = CharSet.Auto)]
        static extern int AA([In,Out]int a, int b);
        [DllImport("mymath.dll", CharSet = CharSet.Auto                
        static extern void transfer(string s); 
        private void button7_Click(object sender, EventArgs e)
        {            this.Text = AA(3,4).ToString();
            string s = "aaaaaaa";
            transfer(s);
        }
其中执行this.Text = AA(3,4).ToString();后显示为0
执行transfer(s);后弹出的对话框内容本应该为"aaaaaaa",但却显示为空,请大侠相助,谢了

解决方案 »

  1.   

    把delphi中transfer定义改成如下
    procedure transfer(s:PChar);delphi中如果使用string类型,c#无法调用。
      

  2.   

    to:Knight94(愚翁)兄弟
    试过了,弹出的对话框还是空的,能不能再帮下忙?
    其中:[DllImport("mymath.dll", CharSet = CharSet.Auto后少个中括号"]"是我无意中删掉了,而并不是原程序中的错误点
      

  3.   

    如果是pchar的话,可以如下试试
    [DllImport("mymath.dll"]
    static extern void transfer( 
    [MarshalAs(UnmanagedType.LPTStr)]string s);
      

  4.   

    to:Knight94(愚翁)兄弟
    是不是我delphi中或c#中哪写的有错误?弹出的对话框还是空的。
    另外this.Text = AA(3,4).ToString();执行后仍显示为0
      

  5.   

    在delphi中已改成了
    procedure transfer(s:PChar);这种形式了