Page 1 of 1

Another old delphi issue

PostPosted: Wed Jan 04, 2017 10:54 am
by HsiaLin
I really dont know much about delphi, especially string conversions, so sorry if i sound
ignorant here. I am trying to make a package with an old delphi twain.pas, which is
attached below. Anyway, there are 2 filename string declares listed:

Code: Select all
  TW_SETUPFILEXFER = packed record
       FileName: array [0..255] of Char;  //<<---------------------------
       Format: TW_UINT16;            // Any TWFF_ constant
       VRefNum: TW_INT16;            // Used for Mac only
     end;
  pTW_SETUPFILEXFER = ^TW_SETUPFILEXFER;


Code: Select all
FFileName: string


and when it comes time to open said filename:
hF := OpenFile(setup.Filename, ofs, OF_CREATE);

it gives error message
[DCC Error] OopsTwain.pas(859): E2010 Incompatible types: 'PAnsiChar' and 'array[0..255] of Char'

I have tried casting to setup.Filename to PAnsiChar but it doesnt work.
Can someone help me figure this out :-)

Re: Another old delphi issue

PostPosted: Wed Jan 04, 2017 2:23 pm
by rlebeau
HsiaLin wrote:I am trying to make a package with an old delphi twain.pas, which is
attached below.


What version of Delphi/C++Builder are you using? In Delphi 2007 and earlier, 'Char' is 'AnsiChar' ('char' in C++). In Delphi 2009 and later, 'Char' is 'WideChar' ('wchar_t' in C++).

HsiaLin wrote:And when it comes time to open said filename:
hF := OpenFile(setup.Filename, ofs, OF_CREATE);

it gives error message
[DCC Error] OopsTwain.pas(859): E2010 Incompatible types: 'PAnsiChar' and 'array[0..255] of Char'

I have tried casting to setup.Filename to PAnsiChar but it doesnt work.


The code was clearly written for a pre-Unicode version of Delphi, it will not work as-is in a Unicode version. You are going to have to make some tweaks to the code to re-Ansify it without having to re-write it for Unicode.

You cannot simply type-cast a WideChar array to a PAnsiChar pointer (well, you can, but it won't do what you are expecting). Change the array declaration to use 'AnsiChar' instead of 'Char'.

You will likely have to do the same thing for the TW_STR... type declarations as well.

You also need to change the 'ptr' variable in TOopsTwain.FileTransfer() from 'PChar' to either 'PByte' or 'PAnsiChar' (I would suggest 'PByte'), since it is being used in pointer arithmetic for a binary data buffer, not a text string.

There may be other issues. I didn't go through the whole code.

Re: Another old delphi issue

PostPosted: Wed Jan 04, 2017 2:52 pm
by HsiaLin
Thanks for the info, i think i`ll just use minimal C code for
what i need, the good old delphi days are gone.

Re: Another old delphi issue

PostPosted: Thu Jan 05, 2017 12:19 pm
by rlebeau
HsiaLin wrote:the good old delphi days are gone.


Not really. You just have to pay attention to what you are doing with it.

Re: Another old delphi issue

PostPosted: Thu Jan 05, 2017 7:00 pm
by HsiaLin
rlebeau wrote:Not really. You just have to pay attention to what you are doing with it.


With your help it makes it alot easier, thanks.

This one works.
For getting TWAIN images via Native, File or Memory.