Interface-Modification for D2009 - Problem with Serials

Knowledge exchange related to the VPE Report Engine and PDF Library

Moderator: IDEAL Software Support

Interface-Modification for D2009 - Problem with Serials

Postby molkenstehler » Sun Aug 02, 2009 3:05 pm

I've changed the implementation of VPEngine and the usage in VPE_VCL for the use with D2009. Therefore I changed the PChar-declarations to PAnsiChar.

For example:
[VPEngine]
Code: Select all
TVpeLicense = procedure(hDoc: LongInt; serial1: PAnsiChar; serial2: PAnsiChar) stdcall;
@VpeLicense := GetProcAddress(DLLHandle, PAnsiChar(50));


[VPE_VCL]
Code: Select all
procedure TVPEngine.License(serial1, serial2: String);
{$IFNDEF WIN32} var p, p2:PChar; {$ENDIF}
begin
  if hDocument = 0 then raise EVPEError.CreateByCode(VPEVCL_ERR_DOC_NOT_OPEN);
  {$IFNDEF WIN32}
    p:=StrAlloc(length(serial1)+1);
    StrPCopy(p, serial1);
    p2:=StrAlloc(length(serial2)+1);
    StrPCopy(p2, serial2);
    VpeLicense(hDocument, p, p2);
    StrDispose(p);
    StrDispose(p2);
  {$ELSE}
    VpeLicense(hDocument, UnicodeToPAnsiChar(serial1), UnicodeToPAnsiChar(serial2));
  {$ENDIF}
end;


[Unicode2PAnsiChar]
Code: Select all
function UnicodeToPAnsiChar(Value : string) : PAnsiChar;
begin
  result := PAnsiChar(AnsiString(Value));
end;


That works perfectly with the contents, using Write() and so.

The problem is with the Licence. It show's me, it works as a "Demo", also when the Licence-Serials are transferred.

Could someone help me?
molkenstehler
 
Posts: 3
Joined: Sun Aug 02, 2009 2:58 pm

Postby IDEAL Software Support » Mon Aug 03, 2009 7:52 am

In your function UnicodeToPAnsiChar() you are calling AnsiString(). Then you return the pointer to the string and the function exits. When it exits, the string is destroyed, since it is a temporary local variable. In the consequence you are passing a pointer to a non-existing string to VPE.
IDEAL Software Support
 
Posts: 1622
Joined: Thu Nov 18, 2004 4:03 pm

Postby molkenstehler » Mon Aug 03, 2009 9:32 am

As I changed it to a local variable it works. Thank you.

What I do not understand: I post any string with UnicodeToPAnsiChar() to the library, but just the licence-method didn't work. How could that be?
molkenstehler
 
Posts: 3
Joined: Sun Aug 02, 2009 2:58 pm

Postby IDEAL Software Support » Mon Aug 03, 2009 10:47 am

It is just luck. The string is deleted when your function exits. In some cases the string is not overwritten, so it is still in memory. Passing the invalid string pointer around can even make your application crash! This is programming language related, not VPE related.

Solution: Let your UnicodeToPAnsiChar() function return the Ansi string and cast it to the pointer in the calling function. Because the UnicodeToPAnsiChar() function returns the string instead of a pointer, the string is still alive.

Code: Select all
function UnicodeToPAnsiChar(Value : string) : AnsiString;
begin
  result := AnsiString(Value);
end;

VpeLicense(hDocument, PAnsiChar(UnicodeToPAnsiChar(serial1)), PAnsiChar(UnicodeToPAnsiChar(serial2)))


But this renders the UnicodeToPAnsiChar() function almost useless and you can directly use:

Code: Select all
VpeLicense(hDocument, PAnsiChar(AnsiString(serial1)), PAnsiChar(AnsiString(serial2)))
IDEAL Software Support
 
Posts: 1622
Joined: Thu Nov 18, 2004 4:03 pm

Postby molkenstehler » Mon Aug 03, 2009 10:59 am

Danke.
molkenstehler
 
Posts: 3
Joined: Sun Aug 02, 2009 2:58 pm


Return to VPE Open Forum

Who is online

Users browsing this forum: No registered users and 111 guests