Discussion:
Manipulating BLOBs in a x64 UDF compiled with Delphi XE2
Josef Kokeš
2012-06-18 06:18:57 UTC
Permalink
Hi!

Having bought Delphi XE2, I tried to compile my old (well, very old) UDF
in a 64bit mode. I expected a total fail, because the UDF was written in
Delphi 5 for Interbase 6.0, but after I fixed string types, the UDF
actually works with Firebird 2.1 x64. With one exception - a function
which converts VARCHAR to BLOB causes an Access Violation in the server.
I suspect this might have something to do with the fact that x64
pointers are 64 bits long, which causes the blob record to be of
incorrect lenght, but I would like to get a confirmation (possibly even
a solution :-)) from people more experienced with UDF writing that I am.

The SQL declaration:

DECLARE EXTERNAL FUNCTION STRBLOB
CSTRING(32765), BLOB
RETURNS PARAMETER 2
ENTRY_POINT 'STRBLOB' MODULE_NAME 'pwIBUDF';

The function:

function STRBLOB(Value: PAnsiChar; Dest: PBlob): PBlob;
begin
Result := Dest;
Dest^.PutSegment(Dest^.BlobHandle, Value, StrLen(Value))
end;

The blob record (probably wrong for x64):

TISC_BlobGetSegment = function(BlobHandle: PInt;
Buffer: PAnsiChar;
BufferSize: LongInt;
var ResultLength: LongInt): SmallInt; cdecl;
TISC_BlobPutSegment = procedure(BlobHandle: PInt;
Buffer: PAnsiChar;
BufferLength: SmallInt); cdecl;
TBlob = record
GetSegment: TISC_BlobGetSegment;
BlobHandle: PInt;
SegmentCount: LongInt;
MaxSegmentLength: LongInt;
TotalSize: LongInt;
PutSegment: TISC_BlobPutSegment;
end;
PBlob = ^TBlob;

Did anyone try to work with BLOBs in a x64 UDF already?

Thanks,

Josef Kokes


------------------------------------

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Visit http://www.firebirdsql.org and click the Resources item
on the main (top) menu. Try Knowledgebase and FAQ links !

Also search the knowledgebases at http://www.ibphoenix.com

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Yahoo! Groups Links

<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/firebird-support/

<*> Your email settings:
Individual Email | Traditional

<*> To change settings online go to:
http://groups.yahoo.com/group/firebird-support/join
(Yahoo! ID required)

<*> To change settings via email:
firebird-support-***@yahoogroups.com
firebird-support-***@yahoogroups.com

<*> To unsubscribe from this group, send an email to:
firebird-support-***@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
Josef Kokeš
2012-06-18 06:20:33 UTC
Permalink
Post by Josef Kokeš
function STRBLOB(Value: PAnsiChar; Dest: PBlob): PBlob;
begin
Result := Dest;
Dest^.PutSegment(Dest^.BlobHandle, Value, StrLen(Value))
end;
That's CDECL, too. I copied it from the implementation rather than from
the interface.

Josef Kokes


------------------------------------

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Visit http://www.firebirdsql.org and click the Resources item
on the main (top) menu. Try Knowledgebase and FAQ links !

Also search the knowledgebases at http://www.ibphoenix.com

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Yahoo! Groups Links

<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/firebird-support/

<*> Your email settings:
Individual Email | Traditional

<*> To change settings online go to:
http://groups.yahoo.com/group/firebird-support/join
(Yahoo! ID required)

<*> To change settings via email:
firebird-support-***@yahoogroups.com
firebird-support-***@yahoogroups.com

<*> To unsubscribe from this group, send an email to:
firebird-support-***@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
Dmitry Kuzmenko
2012-06-18 07:15:03 UTC
Permalink
Hello, Josef!

Monday, June 18, 2012, 10:18:57 AM, you wrote:

JK> function STRBLOB(Value: PAnsiChar; Dest: PBlob): PBlob;
cdecl

JK> The blob record (probably wrong for x64):

JK> TISC_BlobGetSegment = function(BlobHandle: PInt;
JK> Buffer: PAnsiChar;
JK> BufferSize: LongInt;
JK> var ResultLength: LongInt): SmallInt; cdecl;

no,
buffersize: word; // two unsigned bytes, NOT longint.
resultlength: word: // the same two unsigned bytes

JK> TISC_BlobPutSegment = procedure(BlobHandle: PInt;
JK> Buffer: PAnsiChar;
JK> BufferLength: SmallInt); cdecl;

that's correct.

JK> TBlob = record

that's correct too
--
Dmitry Kuzmenko, www.ib-aid.com
Josef Kokeš
2012-06-18 07:19:32 UTC
Permalink
Hi!
Post by Dmitry Kuzmenko
Hello, Josef!
JK> function STRBLOB(Value: PAnsiChar; Dest: PBlob): PBlob;
cdecl
Yeah, I realized too late that I should have used the declaration from
interface, not from implementation. I have that.
Post by Dmitry Kuzmenko
JK> TISC_BlobGetSegment = function(BlobHandle: PInt;
JK> Buffer: PAnsiChar;
JK> BufferSize: LongInt;
JK> var ResultLength: LongInt): SmallInt; cdecl;
no,
buffersize: word; // two unsigned bytes, NOT longint.
resultlength: word: // the same two unsigned bytes
I will fix this. It should not matter, though, as I am not using
GetSegment at all. But of course it's a good idea to have a correct
declaration, even if I don't need it right at the moment.

Is it possible that the access violation is caused by e.g. alignment
issues? I tried to use a packed record without success, though.

Josef Kokes


------------------------------------

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Visit http://www.firebirdsql.org and click the Resources item
on the main (top) menu. Try Knowledgebase and FAQ links !

Also search the knowledgebases at http://www.ibphoenix.com

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Yahoo! Groups Links

<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/firebird-support/

<*> Your email settings:
Individual Email | Traditional

<*> To change settings online go to:
http://groups.yahoo.com/group/firebird-support/join
(Yahoo! ID required)

<*> To change settings via email:
firebird-support-***@yahoogroups.com
firebird-support-***@yahoogroups.com

<*> To unsubscribe from this group, send an email to:
firebird-support-***@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
Dmitry Kuzmenko
2012-06-18 07:26:13 UTC
Permalink
Hello, Josef!

JK> Is it possible that the access violation is caused by e.g. alignment
JK> issues? I tried to use a packed record without success, though.

do you know how to debug udfs? The same, as debugging dlls,
but here you run Firebird as application, i.e. specify
host app as
fbserver.exe
with the option
-a

and run it directly from IDE. I hope it will allow to debug
64bit code, at least, no problems with debugging 32bit code.
--
Dmitry Kuzmenko, www.ib-aid.com
Loading...