0

I have program compiled with FoxPro that calls d1.dll functions. d1.dll was developed with Delphi 2007. I need to develop d1.dll that will replace existing one using c++. After several tests FoxPro application crashes in random places with random messages like:

Fatal error: Exception code=C0000005 @ 2012.12.12 11:33. Error log file: C:\Program Files\Common Files\Microsoft Shared\VFP\vfp9rerr.log

or

No enaugh memory execute some_function

I have removed all code from c++ dll and left just some test values that original dll usually returns. But this does't helps. Probably something is wrong with declaration and variable usage.

I have build Fox Pro small test program according to manner of declaring d1.dll function in main Fox Pro program. Unfortunately it doesn't crash. I run it from IDE while mine program runs from executable, but I don't think this is reason. Probably there is som issuersregarding variable usage.

What might be problem?

Function declaration in Delphi and C++:

C++

extern "C" int _stdcall f1(unsigned char *aError)

extern "C" int_stdcall f2(char *txt,unsigned char *aError)

extern "C" int _stdcall f3(unsigned char *aError, char *aAnswer)

extern "C" int _stdcall f4(unsigned char *aError)

extern "C" int _stdcall f5( char* descriptor, char x, double pr, char aError, double qtity, char kd, char* pd ) extern "C" int _stdcall f6(char *acomment_string, unsigned char* aError)

extern "C" int _stdcall f7(unsigned char tender_number, double amount, unsigned char *aError)

extern "C" int _stdcall f8(unsigned char *aError)

DELPHI 2007

function f1(var aError: byte):bool; stdcall; function f2(txt: pchar; var aError: byte):bool; stdcall;

function f3(var aError: byte; adata: pchar):bool; stdcall;

function f4(var aError: byte):bool; stdcall;

function f5(descriptor: pchar;x: byte; pr: double; var aError: byte ; qtity: double; kd : pchar ; pd: pchar ):bool; stdcall;

function f6(non_fiscal_string: pchar; var aError: byte):bool; stdcall;

function f7(tender_number:byte; amount:double; var aError: byte):bool; stdcall;

function f8 (var aError: byte):bool; stdcall;

FoxPro test program

FoxPro FoxPro

declare integer f1 in c:\d1.dll string err

declare integer f2 in c:\d1.dll string txt,string err

declare integer f3 in c:\d1.dll string err, string rec_nr

declare integer f4 in c:\d1.dll string err

declare integer f5 in c:\d1.dll string descr, integer x, double pr ,string err, double qty, string kd, string pd

declare integer f6 in c:\d1.dll string non_fiscal_string,string err

declare integer f7 in c:\d1.dll integer tender, double amount ,string err

declare integer f8 in c:\d1.dll string err

aLength=2048

aCardinal=4

ff_log=space(aLength)

rec_nr=SPACE(aCardinal)

fiscal_rec_nr=SPACE(aCardinal)

serial_nr = SPACE(aLength)

status_bytes=SPACE(aCardinal)

descr = "descr"

x =1 pr = 123 t=0 t= f2(descr,@kl)

t= f1(@kl)

  Thisform.text1.Value=ff_log

t= f2(descr,@kl)

t= f4(@kl)

  t=f3(@kl,@ff_log)

  t=GetStatus(@kl,rec_nr,fiscal_rec_nr,serial_nr,status_bytes)

t= f5(descr,x,pr,@kl, 1, "","")

t= f2(descr,@kl)

t= f2(descr,@kl)

t=f6(descr,@kl)

t=f8(@kl)

t=f7(1,100,@kl)

Arioch 'The
  • 15,799
  • 35
  • 62
vico
  • 17,051
  • 45
  • 159
  • 315
  • 1
    When quoting error messages always paste the literal text, don't rewrite from memory. You never know what you may be leaving behind (and it also saves you from embarrassing typos.) – Leonardo Herrera Dec 12 '12 at 13:19
  • -1 You must fix the formatting. Use code blocks rather than quotes. Note the italics in the question at the moment. Those are not what you intend I am sure. Also, how do you expect us to debug this? We can't see any of the code!! – David Heffernan Dec 12 '12 at 16:34

1 Answers1

1

1) edit question - add the TAG with your delphi version. it is not only polite but might be critical here.

2) you see "string" references in FoxPro code - that probably means your char* hear is C-string (aka ASCIIZ string, aka zero-terminated strings)

They need kinda special treatment. Try passing them as PAnsiChar - like - function f1(const aError: PAnsiChar):bool; stdcall;

Read help about Delphi PChar type - but always use PAnsiChar in fixed DLL API: PChar is ambiguous in different Delphi versions - mapped to either PAnsiChar or PWideChar

3) return correct datatype. Not bool.

declare integer f2 in c:\d1.dll string txt, string err
extern "C" int _stdcall f2(char *txt,unsigned char *aError)
function f2(const txt, aError: PAnsiChar): integer; stdcall;

Arioch 'The
  • 15,799
  • 35
  • 62
  • There is no need to change something in Delphi code. Crashes dll that is done with Visual c++ – vico Dec 12 '12 at 14:44
  • well, then do the reverse conversion! `var x: byte` would stand for by-ref `unsigned char& x`; Bool is some windows boolean type - search it on docwiki.embarcadero.com and use proper windows type. And your FoxPro code seems inconsistent with Delphi code - there are strings where Delphi code does not have them. Only PChar aka PAnsiChar is string. not char& type – Arioch 'The Dec 12 '12 at 14:53
  • You should also configure VC++ to make non-Unicode library, so it would have 1-byte char, not 2-bytes char – Arioch 'The Dec 12 '12 at 14:55
  • @Arioch'The No, you've got that wrong in your latest comment. On Windows, `char` is always just a single 8 bit byte. The 16 bit character type is `wchar_t`. – David Heffernan Dec 12 '12 at 17:29
  • @David there should be some correspondence to _T macro - the type that would be either single byte or two bytes depending on project settings – Arioch 'The Dec 13 '12 at 10:58
  • 1
    @Arioch'The Yes, there is the `TCHAR` stuff, but the code here is `char`. – David Heffernan Dec 13 '12 at 11:02