Y
ywan
Unregistered / Unconfirmed
GUEST, unregistred user!
各位好,
小弟有想把一段DELPHI程序转成VB,但不知什么原因,我不能获得结构体的数据。
DELPHI程序如下:
我写的VB程序如下:
当程序运行到 : 回调函数 “watchform.txtdeliver.Text = fDeliver.messagecoding”语句时程序就不再运行,或出现
“内存不能为read”的错误,
请问:这是什么问题?结构体作为回调函数的参数时要怎样处理?谢谢各位!
小弟有想把一段DELPHI程序转成VB,但不知什么原因,我不能获得结构体的数据。
DELPHI程序如下:
代码:
type TDeliver = procedure(deliver: DeliverStr);
type TReport = procedure(Report: ReportStr);
{$R *.DFM}
//StartUp 启动SGIP.DLL
function StartUp(cLocalIp: pchar;
iLocalPort: integer;
cSmgIp: pchar;
iSmgPort: integer;
cLoginName,cPassword: pchar;
lSocketTimeOut,lMTTimeOut,iSrcNode,lMTThread,lMaxListNode,lMTMaxRetryTime,lMOThread: integer;
deliver: TDeliver;
report: TReport;
bb,cc: pointer): integer;stdcall;external 'SGIP.DLL' name 'Startup';
procedure Deliver(FDeliver: DeliverStr);
var
MsgContent: string;
cTemp: char;
iLength, iCount: integer;
MsgArray: array of char;
begin
with Fm_Demodo
begin
//ASCII字符
if FDeliver.messagecoding = '0' then
begin
MsgContent := FDeliver.messagecontent;
mmShow.Lines.Append('手机号:' + FDeliver.usernumber + ';内容:' + MsgContent);
end
//GBK编码、Unicode编码,需要把高低位调换
else
if (FDeliver.messagecoding = '15') or (FDeliver.messagecoding = '8') then
begin
iLength := FDeliver.messagelength;
SetLength(MsgArray, iLength);
for iCount := 0 to iLength - 1do
begin
MsgArray[iCount] := FDeliver.MessageContent[iCount];
end;
for iCount := 0 to iLength - 1do
begin
cTemp := MsgArray[iCount];
MsgArray[iCount] := MsgArray[iCount+1];
MsgArray[iCount+1] := CTemp;
end;
for iCount := 0 to iLength - 1do
begin
MsgContent := MsgContent + MsgArray[iCount];
end;
mmShow.Lines.Append('手机号:' + FDeliver.usernumber + ';内容:' + MsgContent);
SetLength(MsgArray, 0);
end;
end;
end;
//启动API
function TFm_Demo.StartAPI: integer;
var
CurrentDir, TempFile: string;
begin
CurrentDir := ExtractFileDir(ParamStr(0));
TempFile := CurrentDir + '/sgip.ini';
if not FileExists(TempFile) then
begin
SayWarning(cStartError);
Exit;
end
else
begin
mmShow.Lines.LoadFromFile(TempFile);
LocalIP := mmShow.Lines.Values['LocalIP'];
LocalPort := StrToInt(mmShow.Lines.Values['LocalPort']);
SmgIp := mmShow.Lines.Values['SmgIP'];
SmgPort := StrToInt(mmShow.Lines.Values['SmgPort']);
LoginName := mmShow.Lines.Values['LoginName'];
Password := mmShow.Lines.Values['Password'];
SrcNode := StrToInt(mmShow.Lines.Values['SrcNode']);
Result := StartUp(PChar(LocalIP), LocalPort, PChar(SmgIP), SmgPort, PChar(LoginName), PChar(Password), 30, 60, SrcNode, 1, 5000, 5, 1,
Deliver, Report, nil, nil);
end;
end;
procedure TFm_Demo.btnConnectClick(Sender: TObject);
begin
if StartAPI = -1 then
SayError('启动API出错!请检查参数配置是否正确!')
else
mmShow.Lines.Append('连接网关成功!');
end;
代码:
Option Explicit
Public Type DeliverStr
sequencenumber1 As Long
sequencenumber2 As Long
sequencenumber3 As Long
usernumber(22) As Byte
spnumber(22) As Byte
tp_pid As Byte
tp_udhi As Byte
messagecoding As Byte
messagelength As Long
messagecontent(160) As Byte
End Type
Public Type ReportStr
sequencenumber1 As Long
sequencenumber2 As Long
sequencenumber3 As Long
usernumber(22) As Byte
state As Byte
errcode As Byte
End Type
Private Declare Function Startup Lib "SGIP.dll" (ByRef cLocalIp As String, _
ByVal iLocalPort As Long, _
ByRef cSmgIp As String, _
ByVal iSmgPort As Long, _
ByRef cLoginName As String, _
ByRef cPassword As String, _
ByVal lSocketTimeOut As Long, _
ByVal lMTTimeOut As Long, _
ByVal iSrcNode As Long, _
ByVal lMTThread As Long, _
ByVal lMaxListNode As Long, _
ByVal lMTMaxRetryTime As Long, _
ByVal lMOThread As Long, _
ByRef Deliver As Variant, _
ByRef Report As Variant, _
ByRef Response As Variant, _
ByRef Mterror As Variant) As Long
Const StringLocalIP = "192.168.1.10"
Const StringLocalPort = 9881
Const StringSmgIP = "192.168.1.10"
Const StringSmgPort = 8801
Const LoginName = "polowan"
Const Password = "wanxiaojun"
Const SocketTimeOut = 30
Const MTTimeOut = 30
Const SrcNode = 350001
Const MTThread = 100
Const MaxListNode = 1
Const MTMaxRetryTime = 50
Const MOThread = 100
‘回调函数
Public Function fProcessDeliver(ByRef fDeliver As DeliverStr) As Long
watchform.txtdeliver.Text = fDeliver.messagecoding
fProcessDeliver = 1
End Function
’调用API
Private Sub butlink_Click()
result = Startup(StringLocalIP, StringLocalPort, StringSmgIP, StringSmgPort, LoginName, Password, _
MTTimeOut, 120, SrcNode, 50, 30, MTMaxRetryTime, 100, _
AddressOf sgipmodule.fProcessDeliver, AddressOf sgipmodule.fProcessReport, AddressOf sgipmodule.fProcessResponse, AddressOf sgipmodule.fProcessMTerror)
If result <> -1 then
txtsend.
Text = Now &
": 连接成功!" &
Chr(13) &
Chr(10) &
"连接状态值:" &
CStr(result) &
Chr(13) &
Chr(10)
else
txtsend.
Text = Now &
": 连接失败!" &
Chr(13) &
Chr(10)
End If
End Sub
“内存不能为read”的错误,
请问:这是什么问题?结构体作为回调函数的参数时要怎样处理?谢谢各位!