🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.windows.ms.programmer
Article #3056

Re: Problem with the WSPrintF function

#3056
From: sampson david 58163
Date: Wed, 05 Jun 1991 19:24
80 lines
3713 bytes
In article <91156.084930F0O@psuvm.psu.edu> F0O@psuvm.psu.edu writes:

      I'm new to Windows programming and I was trying to modify the
   STEP3.PAS example program that comes with TPW.  I was changing the
   WMLButtonDown method.  The original routine looks like this:

   Procedure TMyWindow.WMLButtonDown(Var Msg:TMessage);
   VAR
     DC: HDC;
     S: array[0..9] of char;
   BEGIN
     WVSPrintF(S, '(%d,%d)', Msg.LParam);
     DC := GetDC(HWindow);
     TextOut(DC, Msg.LParamLo, Msg.LParamHi, S, StrLen(S));
     ReleaseDC(HWindow, DC);
   END;

      This of course works fine, but I modified the code in the following
   way so I could print some text instead of a number:

   Procedure TMyWindow.WMLButtonDown(Var TextString: PChar);
   VAR
     DC: HDC;
     S: PChar;
   BEGIN
     WVSPrintF(S, '%s', TextString);
     DC := GetDC(HWindow);
     TextOut(DC, Msg.LParamLo, Msg.LParamHi, S, StrLen(S));
     ReleaseDC(HWindow, DC);
   END;

      When Windows hits the WVSPrintF statement, I get a UAE.  By running
   in the debugger, the UAE is reported as exception code 13, a memory
   addressing error.


Hey -- I'll bet it crashes real good.  The problem is with your
variable S.  Notice in the Borland example they've declared it as an
array of characters.  You declared it as a pointer to a character.
The difference between these two declarations is that in Borland's the
ARRAY statement also ALLOCATES the space that will hold the string.

All you have done is declared a pointer.  You haven't allocated any
space to actually hold the string contents.

Look at it this way. If you declare a pointer to char, the compiler
allocates enough space to just hold the address that the pointer
points to (i.e. where the string is located).  If you were to add a
line just after that declaring an integer X, then the compiler will
(more than likely) take the very next 2 bytes to use for the integer
X.  If you thought that the string started in the byte after your
pointer, you're wrong.

What you need to do is ALLOCATE the storage space for the string.  You
can do this with the NEW operator in TP. 

S := New (Char ....      I forget the exact syntax..... you have to
                         tell it how many bytes to allocate 

This statement (in proper form) will allocated the space for the
string contents, return the starting address of that space, and
assign it to your variable S.

David

--

                                          A new world record
                                          in the javalin throw
                                                
    /                                          /
   /                                          I
-------------------------------------------------


David Sampson                                         Harris Corporation
dsampson@x102a.ess.harris.com                   Gov't Aerospace Systems Divison
uunet!x102a!dsampson                                  Melbourne, Florida

-------------------------------------------------------------------------------
Technical Details
Message-ID: <DSAMPSON.91Jun5152408@x102a.harris-atd.com>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!samsung!sol.ctr.columbia.edu!trantor.harris-atd.com!trantor!dsampson
References: <91156.084930F0O@psuvm.psu.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!samsung!sol.ctr.columbia.edu!trantor.harris-atd.com!trantor!dsampson
From: dsampson@x102a.harris-atd.com (sampson david 58163)
Newsgroups: comp.windows.ms.programmer
Subject: Re: Problem with the WSPrintF function
Message-ID: <DSAMPSON.91Jun5152408@x102a.harris-atd.com>
Date: 5 Jun 91 19:24:08 GMT
References: <91156.084930F0O@psuvm.psu.edu>
Sender: news@trantor.harris-atd.com
Organization: Harris Gov't Aerospace Systems Division
Lines: 80
In-reply-to: F0O@psuvm.psu.edu's message of 5 Jun 91 12:49:30 GMT