提供的c++ demoDWORD _stdcall ConnectToDev(HWND &hwd,char *ip);
HWND devHwnd = NULL;
char ip[15];
memset(ip,0,15);
memcpy(ip,”192.168.1.55”,12);
DWORD ret = pConnectToDev(devHwnd,ip);
我用delphi
unit Unit1;interfaceuses
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;
tConnectToDev=function(hwd:hwnd;ip:pchar):dword;stdcall;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
th:thandle;
pConnectToDev:tConnectToDev;
aa:dword;
begin
th:=LoadLibrary('test.dll');
try
begin
if th<>0 then
begin
@pConnectToDev:=GetProcAddress(th,'ConnectToDev');
aa:=pConnectToDev(self.Handle,pchar('192.168.168.132'));
if aa=0 then
showmessage('ok');
end;
end;
finally
end;
end;end.
上面代码总是出现"access violation at address XXXXX"错误
因为刚刚学习dll调用,也不知道问题在哪。
HWND devHwnd = NULL;
char ip[15];
memset(ip,0,15);
memcpy(ip,”192.168.1.55”,12);
DWORD ret = pConnectToDev(devHwnd,ip);
我用delphi
unit Unit1;interfaceuses
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;
tConnectToDev=function(hwd:hwnd;ip:pchar):dword;stdcall;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
th:thandle;
pConnectToDev:tConnectToDev;
aa:dword;
begin
th:=LoadLibrary('test.dll');
try
begin
if th<>0 then
begin
@pConnectToDev:=GetProcAddress(th,'ConnectToDev');
aa:=pConnectToDev(self.Handle,pchar('192.168.168.132'));
if aa=0 then
showmessage('ok');
end;
end;
finally
end;
end;end.
上面代码总是出现"access violation at address XXXXX"错误
因为刚刚学习dll调用,也不知道问题在哪。
DWORD _stdcall ConnectToDev(HWND &hwd,char *ip);
HWND devHwnd = NULL;
char ip[15];这样的代码明显编译不会通过的, 俩个ip的变量
代码也没帖完整
明显类型都不同, C++ DLL里是引用类型,
tConnectToDev=function(hwd: PDWORD;ip:pchar):dword;stdcall;
tConnectToDev=function(var hwd: hwnd;ip:pchar):dword;stdcall;
不知道你是想表达什么意思
DWORD _stdcall ConnectToDev(HWND &hwd,char *ip);
HWND devHwnd = NULL;
char ip[15];这样的代码明显编译不会通过的, 俩个ip的变量
代码也没帖完整
DWORD _stdcall GetAddress(HWND hwd,char* ip)
也要这样定义function(var hwd: hwnd;ip:pchar):dword
如果这样就报错呢function(hwd: hwnd;ip:pchar):dword
hwd: hwnd只是值传递
-->function(var hwd: hwnd;ip:pchar):dword;stdcall;
这样是ok的,现有另一函数DWORD _stdcall GetAddress(HWND hwd,char* ip)
是不是应该是这样:function(hwd: hwnd;ip:pchar):dword;stdcall;
实际上这样却出现"access violation at address"错误
若改成function(var hwd: hwnd;ip:pchar):dword;stdcall;不会报错,这是为什么?
你好好检查下原型.
又有问题了c++相关代码如下:typedef struct
{
unsigned char y;
unsigned char m;
unsigned char d;
unsigned char h;
unsigned char mi;
unsigned char s;
unsigned char w;
}DEV_TIME;DWORD _stdcall GetTime(HWND hwd, DEV_TIME *time);//这是原型//调用部分
DEV_TIME time;
DWORD ret = pGetTime(devHwnd,&time);
我在delphi中的写法(有问题,会报错):type
TDEV_TIME=
record
y:pchar;
m:pchar;
d:pchar;
h:pchar;
mi:pchar;
s:pchar;
w:pchar;
end;
function GetTime(hwd:hwnd;Atime:TDEV_TIME):DWORD;stdcall;external 'test.dll';////////////////////////////procedure TForm1.Button4Click(Sender: TObject);
var
atime:TDEV_TIME;
ret:dword;
begin
ret:= Gettime(devHwnd,atime);//这里感觉有问题
end;
===============================================
DWORD _stdcall GetAddress(HWND hwd,char* ip)
你好好检查下原型.
y:pchar;这俩个完全不同的类型
y: uchar;