问题:DLL接口定义为testprocedure(const var1,var2,var3: string);VB中声明为Private Declare Sub DesignReport Lib "Estimate_Report.dll" (ByVal DBPath As string, ByVal DBName As string, ByVal ReportFile As string)实参类型也为string,但是在DLL中用showmessage显示参数的值是乱码。请问高手该怎么办?很急!

解决方案 »

  1.   

    DLL接口绝对不能使用String类型,强调N次了!!!
    只能用PCHar!!!http://lysoft.7u7.net
      

  2.   

    呵呵 string只是delphi自己弄的一个东东。。
    类似 VC的 CString吧
      

  3.   

    to ly_liuyang:
      DLL接口用Pchar,VB中使用string也是一样的。(VB我不熟临时抱佛脚)VB中用哪种类型?
      

  4.   

    DLL接口用PCHar,vb可用string,默认地址传递,要分配空间,如str1 = Space$(255)
      

  5.   

    to scottzy:
     谢谢!我试试.
      

  6.   

    to scottzy:
      问题依旧.请大家继续关注!
      

  7.   

    把String转换成PChar.
    要不行。你把代码贴出来。
    偶帮你调试。
      

  8.   

    to ksaiy:
      我这边的DLL用到数据库,代码贴出来你也没办法帮我调试。不过我写了一个很简单的测试程序,劳驾帮忙看看。代码如下:
    1、delphi DLL接口函数代码:
    procedure TestP(const VarName: Pchar);Export;
    begin
      form1 := Tform1.Create(nil);
      Application.MessageBox(VarName,'Test',MB_YESNO+MB_ICONQUESTION);
      form1.Free;
      form1 := nil;
    end;
    2、VB的调用代码
    Private Declare Sub TestP Lib "Project1.dll" (ByVal VarName As String)
    Private Sub Command3_Click()
    Dim VN As String
    VN = "XYZ"
    TestP VN
    End Sub
      

  9.   

    我测试通过的代码
    vb
    Public Declare Function testdll Lib "dlltest.dll" (ByVal str1 As String, ByVal str2 As String, ByVal str3 As String) As IntegerPrivate Sub Command1_Click()
    testdll "aaa", "awrr", "ccc"
    End Subdelphi
    function testdll( str1,str2,str3: pchar): integer;
    begin
      with tform1.Create(Application) do
      try
         Edit1.Text := str1;
         Edit2.Text := str2;
         Edit3.Text := str3;
         //result := 1;
         ShowModal;
      finally
      end;
      end;
      

  10.   

    to scottzy:
      请帮忙看看我那段代码。
      

  11.   

    改为:
    function testdll( VarName: pchar): integer;
    begin
     form1 := Tform1.Create(nil);
      try
    Application.MessageBox(VarName,'Test',1);
      finally
      FreeAndNil(form1);
      end;
    可用
      

  12.   

    定义改为 procedure  testdll( VarName: pchar);stdcall; export;
    不加stdcall;会成乱码
      

  13.   

    to scottzy:
      感谢你热情支持!前面我在delphi中调试DLL时,加stdcall出错我才去掉的。我再试试。
      

  14.   

    to scottzy:
      通过,谢谢!但是出现一个新问题(再打扰),在VB中编译执行提示地址冲突,生成EXE文件执行正常。
      

  15.   

    to scottzy:
      错了,提示“应用程序产生异常 软件未知异常(xxxxxxxxx)位置xxxxxxxxx.
      

  16.   

    vb代码:
    Public Declare Sub testdll Lib "dlltest.dll" (ByVal str1 As String)
    Private Sub Command1_Click()
    testdll "aaa" 或"通过"
    End Sub
    没有你说的现象
      

  17.   

    delphi中用了FreeAndNil(form1)吗?
      

  18.   

    to scottzy:
      能肯定是没打sp5补丁的问题吗?能否麻烦解释一下sp5的作用,对vb我不熟。
      

  19.   

    to scottzy:
      delphi中用
    form1.Free;
    form1 := nil;
      

  20.   

    应该是了,我的环境d7 ,vb6.0+sp5,再不行把你的代码发上来
      

  21.   

    to scottzy:
    我的环境d6 ,vb6.0。这时其中的一个接口,定义报表.(用fastreport报表控件,数据库是ACCESS)
    procedure PrintReport(DBPath,DBName,ReportFile: pchar);stdcall;export;
    ---------------------------------
    procedure DesignReport(DBPath,DBName,ReportFile: pchar);Export;
    var
      FileName: string; 
      Frm_Estimate_Report: TFrm_Estimate_Report;
      StrConnection: String;
    begin
      Frm_Estimate_Report := TFrm_Estimate_Report.Create(nil);
      try
      StrConnection := '';
       with Frm_Estimate_Report do
        begin
          ADOCon_DB.Connected := false;
           StrConnection := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+
                             DBPath+DBName+
                             ';Mode=Read;Persist Security Info='+
                             'False;Jet OLEDB:Database Password=conenet99';  
          ADOCon_DB.ConnectionString := StrConnection;
          ADOCon_DB.Connected:=true;      ADOQry_DYCS1.Active := true;
          FileName := extractfilepath(application.ExeName)+ReportFile;
          if FileExists(FileName) then
            begin
              frReport1.LoadFromFile(FileName);
              Screen.Cursor := CrDefault;
              frDesignerClass := TfrDesignerForm; 
              frReport1.DesignReport;
            end
          else
            begin
              Screen.Cursor := CrDefault;
              application.MessageBox(pchar(FileName + '报表文件不存在,' + #13 + #10
                + '请核对文件名及其路径后重试!'), '提示', MB_OK + MB_ICONINFORMATION);
            end;
        end;
      finally
        Frm_Report.Free;
        Frm_Report := nil;
      end;
      

  22.   

    to scottzy:
      太麻烦你了,抱歉!
      

  23.   

    错了,提示“应用程序产生异常 软件未知异常(xxxxxxxxx)位置xxxxxxxxx.
    ---------------------------------------------------------------------
      scottzyv,b6打sp6补丁,上述问题解决。谢谢大家的支持,马上结帖。