I have a delphi project that works fine in delphi 6, but when I upgraded to XE it does not work.
I know it has to do with the new unicode type in delphi XE, I have tried changing the definition of the parameter from pchar to pansichar, ansichar but no succes so far. Can anyone see what i have done wrong?
My project calls a function in an third party dll that is defined like this:
type
PPChar = array of PChar;
{$EXTERNALSYM gsapi_init_with_args}
function gsapi_init_with_args(pinstance:Pgs_main_instance;argc:integer;argv:PPChar):integer; stdcall;
Implementation
{$EXTERNALSYM gsapi_init_with_args}
function gsapi_init_with_args; stdcall; external gsdll32 name 'gsapi_init_with_args';
And here is how I call the function.
procedure PSPDF(input : string; output:string);
var
code:integer;
instance:Pointer;
argv:array of PAnsiChar;
begin
new(instance);
setlength(argv,10);
code:=gsapi_new_instance(@instance,nil);
if code<>0 then
begin
raise Exception.Create('Impossible to open an instance of ghostscript. Error code: '+IntToStr(code));
end;
try
argv[0] := PAnsiChar('ps2pdf');
argv[1] := PAnsiChar('-dNOPAUSE');
argv[2] := PAnsiChar('-dBATCH');
argv[3] := PAnsiChar('-dSAFER');
argv[4] := PAnsiChar('-sDEVICE=pdfwrite');
argv[5] := PAnsiChar(PAnsiString('-sOutputFile='+output));
argv[6] := PAnsiChar('-c');
argv[7] := PAnsiChar('.setpdfwrite');
argv[8] := PAnsiChar('-f');
argv[9] := PAnsiChar(PAnsiString(input));
gsapi_new_instance(instance, nil);
code := gsapi_init_with_args(instance,length(argv),@argv[0]);
if code<0 then
raise Exception.Create('ERROR: init_args: '+IntToStr(code));
gsapi_exit(instance);
gsapi_delete_instance(instance);
finally
end;
end;
I will very much appreciate if someone can help me out.
Mario.
Changing
PChartoPAnsiCharis the correct thing to do, howeverarray of ...is not safe to use in DLL function parameters and was the wrong thing to use even in your Delphi 6 project. After looking at the official Ghostscript documentation, try this instead, in both projects:.
Update: here is a corrected version of the gsapi.pas unit that should work in both Delphi versions: