请教dll小侠,中侠和大侠(100分)

  • 主题发起人 主题发起人 dey-999
  • 开始时间 开始时间
D

dey-999

Unregistered / Unconfirmed
GUEST, unregistred user!
我用delphi做了一个dll,delphi中调用没有问题,但是在vc中调用出错:Unhandled exception in PreviewDemo.exe(ReadCardInfo.dll):0xC0000005;Access Violation.
VC调用格式:
typedef CString (__stdcall * ReadCardInfo)();
{
HINSTANCE h0;
FARPROC f0;
ReadCardInfo lpDllMyFunc;
CString rs;

//h0=AfxLoadLibrary("ReadCardInfo.dll");
h0=LoadLibrary("ReadCardInfo.dll");
if (h0!=NULL)
{
lpDllMyFunc = (ReadCardInfo)GetProcAddress(HMODULE(h0),"GetBarCodeNo");
if (h0!=NULL)
rs = lpDllMyFunc();//出错处
//rs = (*lpDllMyFunc)();
}
FreeLibrary(HMODULE(h0));
}
Delphi代码:
library ReadCardInfo;

{ 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,
Forms,
Windows,
Messages,
Classes,
ReadCardMain in 'ReadCardMain.pas',
ReadCardFrm in 'ReadCardFrm.pas' {FrmReadCard};

{$R *.res}

exports
GetBarCodeNo;

begin

end.
unit ReadCardMain;

interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics,
Controls, Forms, ReadCardFrm;

function GetBarCodeNo():string;stdcall;

implementation

function GetBarCodeNo():string;stdcall;
var fReadCard:TFrmReadCard;
begin
Result:='';
fReadCard:=TFrmReadCard.Create(Application);
try
with fReadCard do begin
if ShowModal=mrOK then begin
Result:=ReturnCardNos();
end;
end;
finally
fReadCard.Free;
end;
end;

end.
ReadCardFrm.pas代码忽略
请各位大侠帮我看看,解决了另外付分啊,谢谢:)
 
function GetBarCodeNo():string;stdcall;-->function GetBarCodeNo():PChar;stdcall;
 
function GetBarCodeNo(ANo : PChar; ALen : integer):boolean;stdcall;
var
fReadCard:TFrmReadCard;
s:string;
l:integer;
begin
Result:=false;
fReadCard:=TFrmReadCard.Create(Application);
try
with fReadCard do
begin
if ShowModal=mrOK then
begin
s:=ReturnCardNos();
ZeroMemory(ANo, ALen);
if ALen > length(s) then
l := length(s)
else
l := ALen;
CopyMemory(ANo, PChar(s), l);
result := true;
end;
end;
finally
fReadCard.Free;
end;
end;
 
二位好,现在已经更进一步了,经过修改(因为liuying1129说的比较简单,所以我先试了,可以,根据同样原理,轻舞肥羊的肯定也是对的),已经出现了窗体.但是,按确认还是会出现上面的错误,着急啊,请二位继续!其他各位大侠也发表发表看法好吗?
 
确定里有什么代码?
 
unit ReadCardFrm;
//返回N个字符串,以逗号连接,不过最好每6个字符串为一个最简单
interface

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

type
TFrmReadCard = class(TForm)
ListView1: TListView;
Button1: TButton;
Button2: TButton;
Image1: TImage;
Image2: TImage;
Image3: TImage;
Edit1: TEdit;
procedure FormCreate(Sender: TObject);
procedure Edit1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
RevStr,RevCard:String;
public
{ Public declarations }
function ReturnCardNos():PChar;
end;

var
FrmReadCard: TFrmReadCard;

implementation

{$R *.dfm}

function TFrmReadCard.ReturnCardNos():PChar;
var i:Integer;
S:String;
CurItem:TListItem;
begin
S:='';
for i:=0 to ListView1.Items.Count-1 do begin
CurItem:=ListView1.Items;
if S='' then S:=CurItem.Caption
else S:=S+','+CurItem.Caption;
end;
StrPCopy(Result,S);
end;

procedure TFrmReadCard.FormCreate(Sender: TObject);
begin
RevStr:='';
RevCard:='';
end;

procedure TFrmReadCard.Edit1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
var CurItem:TListItem;
begin
if Key=13 then begin
if Edit1.Text='' then exit;
CurItem:=ListView1.Items.Add;
CurItem.Caption:=Edit1.Text;
Edit1.Text:='';
end;
end;

procedure TFrmReadCard.Button1Click(Sender: TObject);
begin
ModalResult:=mrok;
end;

procedure TFrmReadCard.Button2Click(Sender: TObject);
begin
close;
end;

end.
全了,嘿嘿:)
 
可能格式不一样,不能这样使用
 
反正代码都全了,请各位大侠帮帮我,结贴时小生另开贴多多放分:)
 
你的不对,ReturnCardNos() 中 Result 没有申请内存,按 轻舞肥羊 的改就可以了
 
我的VC不行,是不是这样写:lpDllMyFunc = ( bool (*) ( char *, int) )GetProcAddress(h0,"GetBarCodeNo");
不过这样写语法不对,请教教我:):):)
 
现在好多了(返回值改为整形):
typedef int (__stdcall * ReadCardInfo)(char * ANo, int ALen);
char * ANo1=NULL;
int ALen1;
int B;
CString cstr;
ANo1 = new char[10];
lpDllMyFunc = ( ReadCardInfo)GetProcAddress(h0,"GetBarCodeNo");
if (B==1)
{
cstr=ANo1;
AfxMessageBox(cstr);
}
但是返回值并不是真实的值,郁闷啊!!!
各位大哥,帮帮我啊,太难了啊:)-
 
谢谢,已经搞定了!
liuying1129来拿分:
http://www.delphibbs.com/delphibbs/dispq.asp?lid=3614458
轻舞肥羊来拿分:
http://www.delphibbs.com/delphibbs/dispq.asp?lid=3614459
 

Similar threads

后退
顶部