我正在使用RFIdeas的DLL,并且该文档指出DLL函数的用法是:
short GetActiveID32(BYTE *pBuf, short wBufMaxSz)
他们没有Delphi示例,但有VB.NET,C#等。这是C#的摘录:
public static void getActiveID()
{
pcproxlib.SetDevTypeSrch(PRXDEVTYP_USB);
int rc = pcproxlib.usbConnect();
Thread.Sleep(2500);
if (rc == 1)
{
IntPtr result1 = Marshal.AllocHGlobal(32 * sizeof(int));
byte[] arr = new byte[32];
int nBits = pcproxlib.GetActiveID32(result1, 32);
if(nBits == 0)
{
Console.WriteLine("\nNo Id Found, Please put card on the reader and make sure it must be configured with the card placed on it");
return;
}
int Bytes = (nBits + 7) / 8;
if (Bytes < 8)
{
Bytes = 8;
}
Marshal.Copy(result1, arr, 0, 32);
String cardData = "";
for (int i = 0; i < Bytes; i++)
{
String data = String.Format("{0:X2} ", arr[i]);
cardData = data + cardData;
}
Console.WriteLine("\n" + nBits+"Bits" + ": " + cardData);
}
else
{
Console.WriteLine("\n Reader Not Connected");
}
}
我在Delphi中定义了以下功能。
function GetActiveID32( CardID : pInt; Max_Buffer_Size : integer ) : word; stdcall external 'pcProxAPI32.dll';
我这样调用该函数:
// Connect to USB Reader
SetConnectProduct( PRODUCT_PCPROX );
SetDevTypeSrch( PRXDEVTYP_USB );
USBDisconnect;
try
DeviceID := 0;
Results := GetDevCnt;
Results := USBConnect( );
if Results <> 0 then
begin
// Get Active USB reader ID
//CardID := 0;
repeat
Sleep( 250 );
Results := GetActiveID32( @CardID , 32 );
BytesToProcess := ( Results + 7) / 8;
if ( BytesToProcess < 8 ) then
BytesToProcess := 8;
// StrLCopy( BytesArray, PInt( CardID ), 32 );
// Marshal.Copy(result1, arr, 0, 32);
CardData := '';
for I := 0 to Trunc( BytesToProcess ) do
begin
CardData := CardData + BytesArray[ i ];
end;
until Results > 0;
// Disconnect from USB Reader until next needed
USBDisconnect;
end;
except
on E : Exception do
begin
results := GetLastLibErr;
ShowMessage( 'Error reading card ' + Results.ToString);
end;
end;
lblCardValue.Caption := String( @CardID );
标题始终是垃圾。我究竟做错了什么?我认为这与我滥用指针有关。
我们终于有了解决方案。下面是有效的代码。希望这对以后的人有所帮助。
//function USBConnect( DeviceID : integer ) : SmallInt; external 'pcProxAPI32.dll';
function USBConnect( DeviceID : integer ) : word; stdcall external 'pcProxAPI32.dll';
// short GetActiveID32(BYTE *pBuf, short wBufMaxSz)
function GetActiveID32( CardID : PByte; Max_Buffer_Size : word ) : word; stdcall external 'pcProxAPI32.dll';
// BSHRT USBDisconnect(void)
function USBDisconnect : word; stdcall external 'pcProxAPI32.dll';
var
frmMain : TfrmMain;
DeviceID : integer;
Results : integer;
CardInfo : ^Byte;
const
PRODUCT_PCPROX : integer = 1;
PRXDEVTYP_USB : integer = 0;
procedure TfrmMain.btnReadCardClick(Sender: TObject);
var
CardData : string;
begin
ShowMessage( 'Place card on reader' );
// Connect to USB Reader
SetConnectProduct( PRODUCT_PCPROX );
SetDevTypeSrch( PRXDEVTYP_USB );
USBDisconnect;
try
DeviceID := 0;
Results := GetDevCnt; //Only one device should be on system
Results := USBConnect( DeviceID ); //Connect to the only device on system
if Results <> 0 then
begin
repeat
Sleep( 250 ); //Requried 250ms delay between reads
Results := GetActiveID32( @CardInfo, 8 );
until Results > 0;
USBDisconnect; // Disconnect from USB Reader until needed next
CardData := IntToHex( Integer( Pointer( CardInfo ) ), 8 );
end
else
ShowMessage( 'Unable to connect to reader' );
except
on E : Exception do
begin
results := GetLastLibErr;
ShowMessage( 'Error reading card ' + Results.ToString);
end;
end;
lblCardValue.Caption := CardData; // Display data from card
end;
这是实际解决问题的代码。
CardData := IntToHex( Integer( Pointer( CardInfo ) ), 8 );