我爱builder

C++Builder 程序员博客

C Builder中使用Delphi文件问题中,我还是无法在.pas同目录下找到生成.hpp文件。我就步骤是我把我需要用的text1.pas源文件放到一个文件夹中,打开C++ Builder,打开我的工程选择Projet ⦁dd to Project ,从Add to Project对话框中的文件类型选择PAS文件名选择text.pas,然后点击OK。
别人说会生成对应的.hpp文件(这个文件和你的单元文件在同一目录下面) ,可我没找到,不知道什么原因,是我做的顺序有问题?

是不是编译正确了才生成.hpp.在delphi中没错,怎么一加入后出现很多原来没有的错误

最好把代码/错误贴上来看一下

要确认你的BCB IDE没有装bcc32pch,或在编译的时候bcc32pch需要关闭。

怎么知道我的 IDE是否装bcc32pch

是不是在delphi中showmessage可以全是小写,但其中一个错误是 Undeclared identifier: 'showmessage'。
这句showmessage("数据开始有非法字符!");还有2个错误
[Pascal Error] test1.pas(96): Illegal character in input file: '"' ($22) 
[Pascal Error] test1.pas(96): Illegal character in input file: '非' ($B7C7)

看上去你是用c++的编译器去编译pas文件了,不要把pas的代码贴入BCB中.只能加入pas的unit,然后再
#include <pas_unit.hpp>
具体可参考:
hnsyf(鱼片儿)写的:
http://topic.csdn.net/t/20020618/17/813364.html

我加入的就是pas的unit,我选择Projet ⦁dd to Project 加入test1.pas后上面显示还是test.pas不是test.hpp?

研究了很长时间了,一直弄不好,心里很急,很需要兄弟的帮忙

上面只会显示unit的名字test,没有.pas或.hpp

显示.pas,不过我是按照你和我说的也就是你给我的文章中的步骤做的

你的BCB是什么版本?

6.0

你有QQ吗?,你是否可以用QQ上的远程协助给我演示个简单的例子

那就奇怪了,可能是你的bcb本身的问题了

很需要keiy的帮忙,为了这个问题,我愁死了,我现在终于承认自己别正常人笨一些了

写一个简单的test.pas
unit test  ;
interface
implementation
function testf:integer;
begin
testf:=100;
end;
end.
然后存盘,在同一目录下,新建一个bcb项目,将test.pas加入,BCB的程序为:

C/C++ code
//————————————————————————— #include <vcl.h> #pragma hdrstop #include "Unit1.h" #include "test.hpp" //————————————————————————— #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //————————————————————————— __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //————————————————————————— void __fastcall TForm1::Button1Click(TObject *Sender) { ShowMessage(testf()); } //————-

编译看看结果

用delphi建简单例子的时候
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

type
  TForm1 = class(TForm)
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
这些东西删掉吗?

可以,可以生成.hpp
看来是我的程序有问题
我的test.pas是我截取的我原来的一个.pas文件的一部分,是不是与这个有关

上面我的也有小问题:
unit test  ;
interface
function testf:integer; //少了声明
implementation
function testf:integer;
begin
testf:=100;
end;
end.
不然能编译不能运行

引用 19 楼 springworker 的回复:
可以,可以生成.hpp
看来是我的程序有问题
我的test.pas是我截取的我原来的一个.pas文件的一部分,是不是与这个有关

当然有关
1.unit test 必须后的test必须与文件名一致
2.在interface中必须含函数声明,如果你用到系统函数或其它VCL函数,必须用uses将它所在的unit包含进来
如:你用ShowMessage函数必须uses Dialogs
终于明白你的问题,你的pas没能编译通过,当然不能生成.hpp

回头再看了一下你的错:

引用 6 楼 springworker 的回复:
是不是在delphi中showmessage可以全是小写,但其中一个错误是 Undeclared identifier: 'showmessage'。
这句showmessage("数据开始有非法字符!");还有2个错误
[Pascal Error] test1.pas(96): Illegal character in input file: '"' ($22)
[Pascal Error] test1.pas(96): Illegal character in input file: '非' ($B7C7)

1.你没有uses Dialogs,就会报:
Undeclared identifier: 'showmessage'
2.showmessage("数据开始有非法字符!");
delphi的字中用不能",要用',要改成
showmessage('数据开始有非法字符!');

学无止境哪““

非常感谢keiy,虽然我的还没做出,但你给我的例子我做,,这贴现在结。以后还大有可能向您请教

  • Filed under: C++ Builder
  • 我转化了,但是有错,那就请高手给我转换下了
    if shujvjieshou.table2.FindKey([strtoint(fjh)]) then
              begin
              sleep(10);
              shujvjieshou.q1.Params[4].AsString:=floattostr(strtofloat(shujvjieshou.
                table2.Fields[2].AsString)-(strtofloat(shujvjieshou.Table2.Fields[3].asstring)
                -strtofloat(data)*0.01));

            shujvjieshou.Q1.ExecSQL;

              end
              else
                begin
                showmessage('无'+fjh+'号分机,请先进入系统设置添加该分机');
                exit;
                end;

    怎么还没人理,我最怕这个了

    if shujvjieshou->Table2->FindKey([StrToInt(fjh)])
    {
      //延时10秒         
      DWORD lTime;
      while((GetTickCount()-lTime) / 1000 <10);
      shujvjieshou->Q1->Params[4]->AsString=FloatToStr(StrToFloat(shujvjieshou->Table2.Fields[2]->AsString)-(StrToFloat(shujvjieshou->Table2->Fields[3]->AsString)-StrToFloat(data)*0.01));
      shujvjieshou->Q1->ExecSQL;
    }
    else
    {
      ShowMessage('无'+fjh+'号分机,请先进入系统设置添加该分机');
      return;
    }

    if shujvjieshou.table2.FindKey([strtoint(fjh)]) 为什么不是这样
    if (shujvjieshou.table2.FindKey([strtoint(fjh)]))

    if shujvjieshou.table2.FindKey([strtoint(fjh)]) 为什么不是这样
    if (shujvjieshou.table2.FindKey([strtoint(fjh)]))

  • Filed under: C++ Builder
  • re:tdatetime;
    ts:ttimestamp;
    re:=recodedatetime(now,strtoint(nian),strtoint(yue),
          strtoint(ri),strtoint(shi),strtoint(fen),00,000);

    re:=timestamptodatetime(ts);

    牛!,佩服一会给分,顺便问句
    reception->Q1->Params->Items[0]=StrToInt(fjh);
    Q1是query控件
    出现这样的错误[C++ Error] jieshou.cpp(220): E2034 Cannot convert 'int' to 'TParam *'
    怎么改

    因为Params->Items[0]的类型是TParam *,而你赋一个整数给它,类型转换会失败。
    改成:
    reception->Q1->Params->Items[0]->AsString = StrToInt(fjh);

    就行了。

  • Filed under: C++ Builder
  • 急着交啊.
    c++中声明如下:
    typedef  struct T{
        float Re;
        float Im;
    }TComplex;

    __declspec(dllexport)  void  _stdcall ABCC(TComplex  Source[],TComplex Dest[],int Count);

    delphi中声明和调用如下:
    procedure ABCC(var Source: array of TComplex; var Dest:array of TComplex;var Count: integer);stdcall;external 'DLL.dll';

      var
      t1,t2:array of TComplex;
      i:integer;
      t:single;
      begin
      GetMem(t1,sizeof(TComplex)*2);
      t:=1.11;
      for i:=0 to 1 do
      begin
        t1[i].Re:=t;
        t:=t+1.11;
        t1[i].Im:=t;
        t:=t+1.11;
      end;
    GetMem(t2,sizeof(TComplex)*2);
      i:=2;
      ABCC(t1,t2,i);
      }

      发现进入dll后,第二个参数和第三个参数的值传不进去,只要使用到第二个参数就读写错.
      第一个参数正常,晕死,急着交啊.

    DLL的参数不能使用class相关的东西.

    delphi 中TComplex声明如下:
     
    TComplex = packed record
        Re: float;
        Im: float;
      end;

    不好意思 看错了
    procedure ABCC(var Source: of TComplex; var Dest:TComplex;Count:integer);stdcall;external 'DLL.dll';

    回2楼,用delphi生成的dll,可以这样调用,我现在是把delphi代码改为c++的

    多写了一个of
    procedure ABCC(var Source:TComplex; var Dest:TComplex;Count:integer);stdcall;external 'DLL.dll';

    不好意思复制错了,这三个参数,有两个是动态结构数组;
    var Source: array of TComplex; var Dest:array of TComplex;var Count: integer

    __declspec(dllexport)  void  _stdcall ABCC(TComplex  Source[],TComplex Dest[],int Count);
    前两个参数只TComplex* ,是指向TComplex  类型的指针,
    你用var Source:TComplex是可以的
    ,你也可以传元素的指针进去用source:^TComplex类型的参数.

    var Source:TComplex是可以,但要在DLL中访问数组其他元素怎么办?

    你传入一个数组的第一个元素,其实传递的也还是一个指针.

    dll中自然可以正常访问.

    这样改不行,现在是要求我写C的dll,用delphi调,delphi这边的程序不变了

    引用 11 楼 smileni 的回复:
    这样改不行,现在是要求我写C的dll,用delphi调,delphi这边的程序不变了

    要是要求是这样,哪么帮顶一下,俺无能为力

    谢!

    1.C++导出函数给delphi用要加extern "C"
    2.delphi的record传参数入栈时会多放一个结构长
    3.delphi的var Count传的是地址
    所以你的C++要这样写(我测试一下,通过)
    extern "C" __declspec(dllexport)  void  __stdcall ABCC(TComplex  Source[],int,TComplex Dest[],int,int &Count)

    14楼的朋友,好像不行啊,能在c++中读出第二个参数结构数组的值吗?

    验证后还是不对

    忘了结构定义了:
    typedef  struct T{
        double Re;
        double Im;
    }TComplex; //delphi没有float

    我的delphi的定义:
    type  TComplex=packed record
    Re:real;
    Im:real;
    end;

    procedure ABCC(Source: array of TComplex; Dest:array of TComplex; var Count: integer);stdcall;external 'bcbd.dll';
    完整的测试程序:

    C/C++ code
    //————————————————————————— #include <windows.h> //————————————————————————— // Important note about DLL memory management when your DLL uses the // static version of the RunTime Library: // // If your DLL exports any functions that pass String objects (or structs/ // classes containing nested Strings) as parameter or function results, // you will need to add the library MEMMGR.LIB to both the DLL project and // any other projects that use the DLL. You will also need to use MEMMGR.LIB // if any other projects which use the DLL will be performing new or delete // operations on any non-TObject-derived classes which are exported from the // DLL. Adding MEMMGR.LIB to your project will change the DLL and its calling // EXE’s to use the BORLNDMM.DLL as their memory manager. In these cases, // the file BORLNDMM.DLL should be deployed along with your DLL. // // To avoid using BORLNDMM.DLL, pass string information using "char *" or // ShortString parameters. // // If your DLL uses the dynamic version of the RTL, you do not need to // explicitly add MEMMGR.LIB as this will be done implicitly for you //————————————————————————— #pragma argsused int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved) { return 1; } typedef struct T{ double Re; double Im; }TComplex; extern "C" __declspec(dllexport) void __stdcall ABCC(TComplex Source[],int,TComplex Dest[],int,int &Count); extern "C" __declspec(dllexport) void __stdcall ABCC(TComplex Source[],int,TComplex Dest[],int,int &Count) { for (int i=0;i<Count;i++) { Dest[i].Re= Source[i].Re; Dest[i].Im= Source[i].Im; } } //—————————————————————————

    Delphi(Pascal) code
    unit d1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} type TComplex=packed record Re:real; Im:real; end; procedure ABCC(Source: array of TComplex; Dest:array of TComplex; var Count: integer);stdcall;external bcbd.dll; procedure TForm1.Button1Click(Sender: TObject); var t1,t2:array of TComplex; ct:integer; begin ShowMessage(inttostr(sizeof(TComplex))); GetMem(t1,sizeof(TComplex)*2); GetMem(t2,sizeof(TComplex)*2); t1[0].re:=111; t1[0].Im:=222; t1[1].re:=333; t1[1].Im:=444; t2[0].re:=555; t2[0].Im:=666; t2[1].re:=777; t2[1].Im:=888; ct:=2; ABCC(t1,t2,ct); ShowMessage(Format(%f %f %f %f,[t2[0].re,t2[0].im,t2[1].re,t2[1].im])); end; end.

    非常感谢keiy和akirya ,刚才打电话来说,这个函数用DELPHI自己调,不用我弄了,把我气晕死了,弄这个东西好几天了,不眠不休,我明天结贴给分,今天没有时间了,还要赶,命苦!

    ++

  • Filed under: C++ Builder
  • re:tdatetime;
    ts:ttimestamp;
    getcurdir;
    reset(f1);
    readln(f1,s1);
    write(f2,dd);
    rewrite(f2);
    delete(s1,1,2);
    setlength(tmppar,m); //设定数组长度位数据的对数

    FILE *f1,*f2; //要用fopen打开文件
    char s1[255];
    TDateTime  re;
    TTimeStamp ts;
    GetCurrentDir;  //delphi 没有getcurdir函数
    fseek(f1,0L,SEEK_SET);
    fgets(s1,255,f1);
    fputs(s1,f2);
    f2=freopen(filename,"w",f2);
    String x=s1;

    x.Delete(1,2);
    strcpy(s1,x.c_str());
    String tmppar;
    int m; //m要赋初值
    tmppar.SetLength(m); //设定数组长度位数据的对数
    //要用fclose对f1,f2关闭

    先弄懂本身语句的意思,用C++不一定要原本照译

    还需你推荐几本书

  • Filed under: C++ Builder
  • delphi中的tmpwar:array of integer;转换成C++ Builder是什么?

    append(f1);
    Write(f1,InputString);
    这两句转换成C++ Builder又是什么呢?非常感谢

    引用 2 楼 zhangchunge 的回复:
    append(f1);
    Write(f1,InputString);
    这两句转换成C++ Builder又是什么呢?非常感谢

    这是文本文件操作,append(f1);追加一行文本(文本文件指针移到文件末尾),Write(f1,InputString); 写字符串到文件。
    你可以用C/C++的文件函数或者流进行替换。

    问题还没完全解决,毛哥帮我去http://topic.csdn.net/u/20080823/15/576e1d7f-0042-49b9-8adc-dd13ef3上看看,谢谢了毛哥

  • Filed under: C++ Builder
  • 在delphi中的进程转化成C++ Builder的什么?

    例如procedure madefilename;
    var
      s3,s4:string;
      l,i:integer;
      f1:textfile;
    begin
        filename:=datetostr(now)+'time';
        s3:=timetostr(time);
            l:=length(s3);
            for i:=1 to l do
              begin
                s4:=copy(s3,i,1);
                if s4 <>':' then
                  begin
                  filename:=filename+s4;
                  end
              end;
            assignfile(f1,filename);
            rewrite(f1);
            closefile(f1);
    end;
    我要时转化成C++ Builder中

    C/C++ code
    void __fastcall madefilename() { AnsiString s3,s4; int l,i; FILE *f1; filename = DateToStr(Now()) + "time"; //s3 = TimeToStr(Time()); //l = s3.Length(); //for (i = 1 ;i<= l;i++) //{ // s4 = s3.SubString(i,1); // if (s4 != ":" ) // { // filename = filename + s4; // } //} filename = filename + FormatDateTime("hhnnss", Now()); f1 = fopen(filename,"w"); fclose(f1); }

    僵哥,你在帮我看看这个问题
    http://topic.csdn.net/u/20080819/10/dd19702b-b540-4f04-9a8e-351f330d2397.html
    谢谢!

    僵哥这两句不懂,你能翻译下吗?"hhnnss" ,"w" 各代表什么意思
    filename = filename + FormatDateTime("hhnnss", Now());
      f1 = fopen(filename,"w");

    你用斜线把中间那部分不能使用又时什么意思?

    引用 5 楼 zhangchunge 的回复:
    僵哥这两句不懂,你能翻译下吗?"hhnnss" ,"w" 各代表什么意思
    filename = filename + FormatDateTime("hhnnss", Now());
      f1 = fopen(filename,"w");

    hhnnss这是一个格式化串(FormatString),表示小时两位(HH)分钟两位(NN)秒两位(SS)
    w是fopen的一个参数选项,即打开文件的方式,建议看一下帮助(F1),基础书上大把.

    引用 6 楼 zhangchunge 的回复:
    你用斜线把中间那部分不能使用又时什么意思?

    没必要那么复杂地处理,所以把它注释掉.换用简单的.

    僵哥还有四个错误
    [C++ Error] jieshou.cpp(61): E2451 Undefined symbol 'FILE'
    [C++ Error] jieshou.cpp(61): E2451 Undefined symbol 'f1'
    [C++ Error] jieshou.cpp(63): E2451 Undefined symbol 'filename'
    [C++ Error] jieshou.cpp(75): E2268 Call to undefined function 'fopen'
    [C++ Error] jieshou.cpp(76): E2268 Call to undefined function 'fclose'
    再麻烦你看看

    #include <stdio.h>

    定义filename

    AnsiString filename;

    FILE *f1; 定義在涵數前面,
    filename 沒定義啊。。
    f1 = :fopen(filename,"w");
    :fclose(f1);

    呵呵,不好意思
    还有错,加上AnsiString filename;
    在这句中f1 = fopen(filename,"w"); 出现
    [C++ Error] jieshou.cpp(77): E2034 Cannot convert 'AnsiString' to 'const char *'
    [C++ Error] jieshou.cpp(77): E2342 Type mismatch in parameter '__path' (wanted 'const char *', got 'AnsiString')
    这两个错误
    要是不加就出现这个错误C++ Error] jieshou.cpp(63): E2451 Undefined symbol 'filename'

    把filename換成char 型。才能用fopen

    要是换成char filename;出现更多错误又是怎么回事

    f1 = fopen(filename.c_str(),"w");

    fopen,fclose前面加::不?

    解决了,能给我解释下吗?

    FILE *fopen(const char *filename, const char *mode);
    fopen 只能支持char型啊,,

    filename.c_str(),就把AnsiStirng換成了char型。

  • Filed under: C++ Builder
  • delphi中的

    Delphi(Pascal) code
    try do something; except showmessage(adsfasdf);

    转到bcb中对应的是什么?

    catch里面填什么呢??本来delphi中只要try部分执行有异常,就执行except,但c++中的catch好像需要参数

  • Filed under: C++ Builder
  • TPanel   *pp   =   dynamic_cast <TPanel   *> (Sender);
        TLabel   *ll   =   dynamic_cast <TLabel   *> (Sender);

      if   (!pp   &&   ll)
            pp   =   dynamic_cast <TPanel   *> (ll-> Parent);

        if   (pp)
            ShowMessage(pp-> Caption);

    var
        pp:   TPanel;
        ll:   TLabel;
    begin
        pp   :=   Sender   as   TPanel;
        ll   :=   Sender   as   TLabel;
        if   (pp   <>   nil)   and   (ll   <>   nil)   then
            pp   :=   Parent   as   TPanel;
        if   (pp   <>   nil)   then
            ShowMessage(pp.Caption);
    end;

    //void   __fastcall   TForm1::lwv(TObject   *Sender)
    procedure   TForm1.lwv(Sender:TObject);
    //{TPanel   *pp   =   dynamic_cast <TPanel   *> (Sender);
    //     TLabel   *ll   =   dynamic_cast <TLabel   *> (Sender);

    var
        pp:TPanel;
        ll:TLabel;
    begin
        pp:=nil;
        ll:=nil;

        if   Sender   is   TPanel   then
            pp:=Sender   as   TPanel;

        if   Sender   is   TLabel   then
            ll:=Sender   as   TLabel;

      //if   (!pp   &&   ll)
        if(Not   Assigned(pp))   and   Assigned(ll)   then
            //pp   =   dynamic_cast <TPanel   *> (ll-> Parent);
            if   Assigned(ll.Parent)   then
                if   ll.Parent   is   TPanel   then
                    pp:=ll.Parent   as   TPanel;

        //if   (pp)
        if   Assigned(pp)   then
            //ShowMessage(pp-> Caption);
            ShowMessage(pp.Caption);

    //               }
    end;

    学习下

  • Filed under: C++ Builder
  • 请问delphi里的nil对应C++是什么?

    NULL

    0

    NULL

    NULL

    NULL,0也可以(不过我个人喜欢用NULL),nil表示的是空指针嘛:-)

    准确的来说应该是0.   Delphi的NIL的数值形式永远都是0.

    而C++做一些嵌入式的时候,   地址0可能会有其他用途,   而那些时候NULL就不见得是0了.

    NULL

    NULL       NULL是个宏   也是0

    NULL并非永远为0.   某些平台上NULL可能为-1或者其他值.

  • Filed under: C++ Builder
  • 类别

    最新

    标签

    链接


    存档