Another old delphi issue

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

Another old delphi issue

Postby HsiaLin » Wed Jan 04, 2017 10:54 am

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 :-)
Attachments
OopsTwain_Delphi.zip
Twain Delphi
(13.24 KiB) Downloaded 170 times
HsiaLin
BCBJ Master
BCBJ Master
 
Posts: 280
Joined: Sun Jul 08, 2007 6:29 pm

Re: Another old delphi issue

Postby rlebeau » Wed Jan 04, 2017 2:23 pm

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.
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1401
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: Another old delphi issue

Postby HsiaLin » Wed Jan 04, 2017 2:52 pm

Thanks for the info, i think i`ll just use minimal C code for
what i need, the good old delphi days are gone.
HsiaLin
BCBJ Master
BCBJ Master
 
Posts: 280
Joined: Sun Jul 08, 2007 6:29 pm

Re: Another old delphi issue

Postby rlebeau » Thu Jan 05, 2017 12:19 pm

HsiaLin wrote:the good old delphi days are gone.


Not really. You just have to pay attention to what you are doing with it.
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1401
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: Another old delphi issue

Postby HsiaLin » Thu Jan 05, 2017 7:00 pm

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.
Attachments
OopsDemo.zip
(48.39 KiB) Downloaded 69 times
OopstwainComp.zip
(23.3 KiB) Downloaded 67 times
HsiaLin
BCBJ Master
BCBJ Master
 
Posts: 280
Joined: Sun Jul 08, 2007 6:29 pm


Return to Technical

Who is online

Users browsing this forum: Bing [Bot] and 18 guests

cron