Problem: I need an example of how to create and declare a blob UDF. Solution: The information in this article applies to: * InterBase v4.x * InterBase v5.x The examples presented are written in Delphi. There are two blob UDF examples: * StrBlob - will accept a string and return a blob * BlobCat - will concatenate two blobs and return the resultant blob Note: For simplicities sake the code provided doesn't contain error checking. Robust error checking should be included before using these in a production environment. Here is the Delphi code for the two functions: ------------------------ begin Delphi Unit --------------------- unit blobunit; interface uses sysutils, Math, ibase, ib_externals; function BlobCat(Blob1, Blob2, CatBlob: PBlob): PBlob; cdecl; function StrBlob(sz: PChar; Blob: PBlob): PBlob; cdecl; implementation function BlobCat(Blob1, Blob2, CatBlob: PBlob): PBlob; cdecl; var length, bytesRead: Integer; buffer: PChar; begin // will read blobs 1 at a time so allocate enough // memory to hold the larger of the 2 blobs length := Max(Blob1^.TotalSize, Blob2^.TotalSize); // change length to adjust for string terminator inc(length); // allocate memory for buffer // make sure isMultiThread is true for DLL buffer := StrAlloc(length); // get data for blob1 Blob1^.GetSegment(Blob1^.BlobHandle, buffer, length, bytesRead); // store into CatBlob CatBlob^.PutSegment(CatBlob^.BlobHandle, buffer, bytesRead); // concat data for blob2 Blob2^.GetSegment(Blob2^.BlobHandle, buffer, length, bytesRead); // store into CatBlob CatBlob^.PutSegment(CatBlob^.BlobHandle, buffer, bytesRead); // free memory for buffer StrDispose(buffer); Result := CatBlob; end; function StrBlob(sz: PChar; Blob: PBlob): PBlob; cdecl; begin Blob^.PutSegment(Blob^.BlobHandle, sz, StrLen(sz)); result := Blob; end; end. ------------------------ end Delphi Unit --------------------- Here is the SQL script that is used to declare these two UDFs to a database: --------------------- begin SQL script ----------------------- connect employee.gdb; declare external function fn_BlobCat Blob, Blob, Blob Returns parameter 3 entry_point 'BlobCat' module_name 'BlobConcat.dll'; declare external function fn_StrBlob cstring(254), blob returns parameter 2 entry_point 'StrBlob' module_name 'BlobConcat.dll'; commit; --------------------- end SQL script -----------------------
Last Modified: 26-SEP-00