Newsgroups : Borland : borland.public.delphi.internet.winsock : 2006 Apr : Converting SNMP extension agent from VC++ to Delphi
| Subject: | Converting SNMP extension agent from VC++ to Delphi |
| Posted by: | "Tony Jackson" (tjacks..@iedaudio.com) |
| Date: | Fri, 7 Apr 2006 15:46:55 |
Hello,
This has been a labor trying to translate VC++ to Delphi. I have been
fairly successful except when it comes to how to handle the SnmpVarBindList
in the SnmpExtensionQuery function. First thing I had to do is create an
array to handle the list. However, I am not sure that I have accomplished
this right. My code compiles; however, when I try to access elements of the
list I get an "access violation" which means that I am trying to access an
object which has not been created.
Anyone familiar with this type of problem. If the following code is hard to
follow, I can clarify. I just did not want to make this too long. First is
the VC++ code then the Delphi code.
Thanks.
Tony
{ VC++ code from SNMP.h }
===========
typedef struct {
UINT idLength;
UINT * ids;
} AsnObjectIdentifier;
typedef struct {
BYTE asnType;
union {
( there are 12 elements }
} asnValue;
} AsnAny;
typedef AsnObjectIdentifier AsnObjectName;
typedef AsnAny AsnObjectSyntax;
typedef struct {
AsnObjectName name;
AsnObjectSyntax value;
} SnmpVarBind;
typedef struct {
SnmpVarBind * list;
UINT len;
} SnmpVarBindList;
=================
BOOL SNMP_FUNC_TYPE SnmpExtensionQuery(BYTE bPduType, SnmpVarBindList
*pVarBindList, AsnInteger32 *pErrorStatus, AsnInteger32 *pErrorIndex)
{
//AsnObjectName;
*pErrorStatus = SNMP_ERRORSTATUS_NOERROR;
*pErrorIndex = 0;
for(UINT i=0;i<pVarBindList->len;i++)
{
*pErrorStatus = SNMP_ERRORSTATUS_NOERROR;
switch(bPduType) // what type of request we are getting?
{
case SNMP_PDU_GET:// // gets the variable value passed variable in
pVarBindList
*pErrorStatus = GetRequest(&pVarBindList->list[i]);
if(*pErrorStatus != SNMP_ERRORSTATUS_NOERROR)
*pErrorIndex++;
break;
};
}
return SNMPAPI_NOERROR;
============================================
============================================
{ Delphi code from SNMP.pas }
===============
type
PAsnObjectIdentifier = ^TAsnObjectIdentifier;
TAsnObjectIdentifier = record
idLength: UINT;
ids: PUINT;
end;
PAsnAny = ^TAsnAny;
TAsnAny = record
asnType: byte;
case integer of
( there are 12 elements }
end;
TAsnObjectName = TAsnObjectIdentifier;
TAsnObjectSyntax = TAsnAny;
// PSnmpVarBind = ^TSnmpVarBind; <<< my
deletion
TSnmpVarBind = record
Name: TAsnObjectName;
Value: TAsnObjectSyntax;
end;
TarrSnmpVarBind = array[0..65535] of TSnmpVarBind; <<< my add
PSnmpVarBind = ^TarrSnmpVarBind; <<< my add
PSnmpVarBindList = ^TSnmpVarBindList;
TSnmpVarBindList = record
List: PSnmpVarBind;
len: UINT;
end;
===============
function SnmpExtensionQuery(bPduType: byte; var pVarBindList:
PSnmpVarBindList; var pErrorStatus: TAsnInteger32; var pErrorIndex:
TAsnInteger32): boolean; stdcall;
var
i: integer;
begin
pErrorStatus := SNMP_ERRORSTATUS_NOERROR;
pErrorIndex := 0;
for i := 0 to Pred(pVarBindList.len) do
begin
pErrorStatus := SNMP_ERRORSTATUS_NOERROR;
case bPduType of
SNMP_PDU_GET:
begin
{ my inserted test }
try
{ I had to modify PSnmpVarBind because pVarBindList.List[i]
without the array designation would have given an "array type required
error". }
WriteToLog(IntToStr(pVarBindList.List[i].Name.idLength)); <<<
"pVarBindList.List[i].Name.idLength" causes an "access violation".
except
on E: Exception do
begin
WriteToLog(E.Message);
Break;
end;
end;
// pErrorStatus := GetRequest(pVarBindList.List[i]); <<< This
function call causes an "access violation".
// if (pErrorStatus <> SNMP_ERRORSTATUS_NOERROR) then
// Inc(pErrorIndex);
end;
end;
end;