Modifying Constants

[prev] [thread] [next] [lurker] [Date index for 2004/05/10]

From: Phil!Gregory
Subject: Modifying Constants
Date: 22:28 on 10 May 2004
A minor thing, really, but I've just wasted several hours on this bug:

I've been working on convincing a Delphi program to call procedures in a
DLL.  This is new to me, so I figure I'll do things wrong.  Thus, when
things go wrong, I blame myself, sometimes, as I found, needlessly:

procedure SimLibLogon(pszDBName: PChar) external "EKDWS.DLL";

procedure DoStuff;
  var
    System : String;
begin
  System := 'VADER';
  SimLibLogon(PChar(System));
end;

and I get an access violation.  The problem?  Delphi is stricter about
types than C.  So, with the prototype I wrote for the procedure, Delphi
decides that the string is a constant, and just calls it with a pointer to
wherever the program stores its string constants.

The DLL was written in C.  It gets a character pointer which, logically,
ought to be constant, but isn't strictly so.  It tries to modify it.
(Consensus here is that it's probably trying to upper-case the string.)
The string is in non-modifiable memory, so I get an error.  I blame
myself, try to fix it, and run in circles for a couple of hours.

Solution?

  System := Format('VADER', []);

Compiler has been fooled, and the string is now in modifiable memory.
Sigh.

-- 
...computer contrarian of the first order... / http://aperiodic.net/phil/
PGP: 026A27F2  print: D200 5BDB FC4B B24A 9248  9F7A 4322 2D22 026A 27F2
--- --
Symbol table full - fatal heap error; please go buy a RAM upgrade from
your local Apple dealer
                       -- Apple MPW C Compiler error message
---- --- --

Generated at 14:02 on 01 Jul 2004 by mariachi 0.52