首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

delphi调用vc dll有关问题

2012-12-14 
delphi调用vc dll问题我想在delphi里调用一个用vc编译的dll里的一个函数,动态的。我对这个封装好了的dll的

delphi调用vc dll问题
我想在delphi里调用一个用vc编译的dll里的一个函数,动态的。

我对这个封装好了的dll的所知全部来自一个read me.txt文件,内容如下:

bool hiddata(BYTE* pWriteData, BYTE* pReadData ) ;

返回true成功 返回false失败

pWriteData指针为要下发的数据缓冲区指针,Dll会发送这个指针指向的8个字节数据。

pReadData指针为接收USB IC的数据缓冲区指针,Dll会把接收的8个字节数据放到指针指向的数据缓冲区中。注意,该缓冲区由应用程序初始化空间。Dll不检查指针的有效性。



好了,对dll我就只知道这么多了。现在我的代码如下,很简答的一个程序:


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type
    p_byte = ^Byte;
    
type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Panel2: TPanel;
    Panel3: TPanel;
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;


var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
type
     //Taddc=function(pWriteData,pReadData:p_byte):Boolean;stdcall;
     Taddc=function(pWriteData,pReadData:p_byte):Boolean;cdecl;
var
     H: THandle;
     addc:Taddc;
     i,j:byte;
begin
     H:=LoadLibrary('Byte8HID.dll');
    
     if h<>0 then
     begin
          @addc:=GetProcAddress(h,'hiddata');
          if @addc<>nil then
          begin
               ShowMessage('not null');
               i:=26;
               j:=26; //随便初始化下。
               try
                   begin
                        if addc(@i,@j) then
                        begin
                             ShowMessage('apply successfully');
                             memo1.Lines.Add(inttostr(Integer(@j)));
                        end
                        else


                        begin
                             ShowMessage(IntToStr(GetLastError));
                        end;
                   end;
               except on e:exception  do
                   begin
                        Memo1.Lines.Add(e.Message);
                   end;
               end;
          end
          else
          begin
               ShowMessage('it''s null');
               ShowMessage(IntToStr(GetLastError));
          end;
     end;

     FreeLibrary(H);
end;

end.

现在我遇到的问题就是,做到ShowMessage('not null');这步之后,程序就完全没反应了,怎么点都不响应。估计是一直在内存里读啊读的,但是不知道读的是什么地址的……

有哪位大侠可以告诉我,我的问题到底出在哪儿?我总觉得是我什么小地方没注意到,做错了。跪求指点。
[最优解释]
var
 sc1 : array[0..100] of byte;
 sc2 : array[0..100] of byte;
begin
 hiddata(sc1,sc2);
end;
[其他解释]
var
 sc1 : array[0..10] of char;
 sc2 : array[0..10] of char;
begin
  ......
  hiddata(@sc1[0],@sc2[0]);
  ......
end;
[其他解释]
function hiddata(pWriteData : Pchar;pReadData :Pchar):boolean;stdcall;Stdcall;external 'XXX.dll';

procedure TForm1.Button1Click(Sender: TObject);
var
 sc1 : array[0..10] of char;
 sc2 : array[0..10] of char;
begin
 hiddata(sc1,sc2);
end;
[其他解释]
function hiddata(pWriteData : Pbyte;pReadData :Pbyte):boolean;stdcall;Stdcall;external 'XXX.dll';

procedure TForm1.Button1Click(Sender: TObject);
var
 sc1 : byte;
 sc2 : byte;
begin
 hiddata(sc1,sc2);
end;


没有测试,LZ参考下。如果出错,见意将byte分配内存空间
[其他解释]
楼上OK,Byte*对应的是DELPHI里的PByte也就是arry of byte;
[其他解释]

引用:
var
 sc1 : array[0..100] of byte;


 sc2 : array[0..100] of byte;
begin
 hiddata(sc1,sc2);
end;




楼上,我试过了,不行,连编译都通不过。

Field definition not allowed after methods or properties
[其他解释]
该缓冲区由应用程序初始化空间!!!!!!!!

你没有分配空间,焉有不出错之理?
[其他解释]
我试过这样子:


var
  Form1: TForm1;
    function  hiddata(pWriteData:Pbyte; pReadData:Pbyte):boolean;Stdcall;external 'Byte8HID.dll';

implementation

{$R *.dfm}


procedure TForm1.Button2Click(Sender: TObject);
var
     sc1 : array[0..100] of byte;
     sc2 : array[0..100] of byte;
begin
     hiddata(@sc1,@sc2);
end;


编译可以通过,但一运行就死机。也不是死机,就是跟我原先的那段代码效果一样,点什么都没反应了,连窗口都拖不动了。
[其他解释]
引用:
var
 sc1 : array[0..10] of char;
 sc2 : array[0..10] of char;
begin
  ......
  hiddata(@sc1[0],@sc2[0]);
  ......
end;


回楼上,我这样子也试过了,效果跟 hiddata(@sc1,@sc2); 这么写完全一样。运行后继续保持假死状态,怎么点都没反应,窗口拖也拖不动。

热点排行