Solved

cast to int + return a LPSIZE

Posted on 2000-03-21
21
617 Views
Last Modified: 2008-02-20
There are 2 errors in the following function.  I am attempting to cast Text.length to an integer - how?  Also I want to correctly return a LPSIZE - how?

LPSIZE CNodeView1::TextExt(HDC dc, string Text)
{
  LPSIZE res;
  GetTextExtentPoint32(dc, Text.c_str(), (integer) Text.length, res);
  return res;
}

Note that this is the official definition of the GetTextExtentPoint32 function:

BOOL GetTextExtentPoint32(
  HDC hdc,          // handle to device context
  LPCTSTR lpString,  // pointer to text string
  int cbString,      // number of characters in string
  LPSIZE lpSize      // pointer to structure for string size
);

Finally - should I be passing in char * to make my life easier?

thanks
0
Comment
Question by:abulka
  • 8
  • 7
  • 3
  • +2
21 Comments
 
LVL 5

Expert Comment

by:Wyn
ID: 2643454
Try:

LPSIZE CNodeView1::TextExt(HDC dc, string Text)
{
  SIZE res;
  GetTextExtentPoint32(dc, Text.c_str(),Text.length, &res);
  return &res;
}

0
 
LVL 4

Expert Comment

by:wylliker
ID: 2643459
First, casting the length should be - (int)

Second, your variable - res - needs to be defined as:

SIZE res;

Your function call becomes ...

 GetTextExtentPoint32(dc, Text.c_str(), (int) Text.length, &res);
 

Give this a try.

0
 
LVL 5

Expert Comment

by:Wyn
ID: 2643465
or

SIZE size;
LPSIZE res;

LPSIZE CNodeView1::TextExt(HDC dc, string Text)
{
 GetTextExtentPoint32(dc, Text.c_str (),Text.length, &size);
  return &res;
}
0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 5

Expert Comment

by:Wyn
ID: 2643471
Ignore last comment,I miswrite.
I mean:

private:
SIZE size;
LPSIZE res;
.............

LPSIZE CNodeView1::TextExt(HDC dc, string Text)
{
 GetTextExtentPoint32(dc, Text.c_str (),Text.length, &size);
  return &size;
}

...............

res=TextExt(...);
0
 

Author Comment

by:abulka
ID: 2643509
Oh yeah - I want to use the above function like this:

  long LineHeight = TextExt(mydc, "Aj").cy;
0
 
LVL 11

Accepted Solution

by:
alexo earned 50 total points
ID: 2643532
Wyn, don't EVER return an address of an automatic (local on the stack) variable!

You probably meant:

    SIZE CNodeView1::TextExt(HDC dc, string Text)
    {
        SIZE res;
        GetTextExtentPoint32(dc, Text.c_str(), (integer) Text.length, &res);
        return res;
    }
0
 

Author Comment

by:abulka
ID: 2643541
Wyn - looks like I actually don't need to declare
LPSIZE res;
since the function is not using it?

Also, when I typed in
LPSIZE CNodeView1::TextExt(HDC dc, string Text)
{
 GetTextExtentPoint32(dc, Text.c_str(), (int) Text.length, &size);
  return &size;
}

and compiled it I found that I had to includ an integer cast - cos I got a compile error.  Even so, I am still getting an error re that attempted cast!!

   CNodeView1.cpp(553) : error C2440: 'type cast' : cannot convert from 'unsigned int (__thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::*' to 'int'
        Conversion is a valid standard conversion, which can be performed implicitly or by use of static_cast, C-style cast or function-style cast

0
 

Author Comment

by:abulka
ID: 2643550
alexo - your code also fails on the line with the integer cast thus:
D:\My Documents\aa Vc\ts1\CNodeView1.cpp(561) : error C2065: 'integer' : undeclared identifier
D:\My Documents\aa Vc\ts1\CNodeView1.cpp(561) : error C2146: syntax error : missing ')' before identifier 'Text'
D:\My Documents\aa Vc\ts1\CNodeView1.cpp(561) : error C2059: syntax error : ')'
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2643574
->Wyn, don't EVER return an address of an automatic (local on the stack) variable!
==================================
Yes,I mean res and I rewrite one later just for this. But you'v got to admit at the very point the function returns,the value is still there on stack,although become tragedy later.
0
 
LVL 4

Expert Comment

by:wylliker
ID: 2643578
Yeah, we all missed on the (int) cast

should be (int) Text.length()
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2643593
wylliker,I think in your first comment You already have the  code,(int)Text.length().You dont miss it:)
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2643612
->looks like I actually don't need to declare
LPSIZE res;
since the function is not using it?
=================
Doesnt matter,just sample.
0
 
LVL 4

Expert Comment

by:wylliker
ID: 2643630
Wyn,

No, I did miss the () after length. As it is a member function - every example had incorrectly ...

(int) Text.length

instead of

 (int) Text.length()


I also forgot about the return value being a pointer to a SIZE structure.


 





0
 
LVL 5

Expert Comment

by:Wyn
ID: 2643676
O,:)
I even dont realize the () after the length until I read your last comment:-)

I concentrate myself to other parts of code.:0

Yes,it's a function:)

Sorry,wylliker .....:)
0
 
LVL 3

Expert Comment

by:LucHoltkamp
ID: 2643783
Why return a LPSIZE??????
It's more logical to return a SIZE! If you return a pointer, you need a variable to hold the value, as a member in you're class or as a static...
Not neccesairy, the way to go is returning a SIZE

SIZE CNodeView1::TextExt(HDC dc, string Text)
{
  SIZE res;
  GetTextExtentPoint32(dc, Text.c_str(),Text.length, &res);
  return res;
}
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2644954
yup ,it's also my first way to go...The second I use &res.
0
 

Author Comment

by:abulka
ID: 2647107
alexo - your code also fails on the line with the integer cast thus:
D:\My Documents\aa Vc\ts1\CNodeView1.cpp(561) : error C2065: 'integer' : undeclared identifier
D:\My Documents\aa Vc\ts1\CNodeView1.cpp(561) : error C2146: syntax error : missing ')' before identifier 'Text'
D:\My Documents\aa Vc\ts1\CNodeView1.cpp(561) : error C2059: syntax error : ')'
0
 

Author Comment

by:abulka
ID: 2647279
Seems that
 
SIZE CNodeView1::TextExt(HDC dc, string Text)
{
  SIZE res;
  GetTextExtentPoint32(dc, Text.c_str(), (int) Text.length(), &res);
  return res;
}

compiles correctly.  I assume the
  return res
is not returning the actual local variable (which will disappear off the call stack as soon as the function exits), but a copy of res.  I hope so :-)


0
 

Author Comment

by:abulka
ID: 2647352
Seems that
 
SIZE CNodeView1::TextExt(HDC dc, string Text)
{
  SIZE res;
  GetTextExtentPoint32(dc, Text.c_str(), (int) Text.length(), &res);
  return res;
}

compiles correctly.  I assume the
  return res
is not returning the actual local variable (which will disappear off the call stack as soon as the function exits), but a copy of res.  I hope so :-)


0
 
LVL 11

Expert Comment

by:alexo
ID: 2648100
abulka, the (integer) cast was in the original code so I assumed it was correct (maybe a typedef to int?).  It should really be (int).

Returning a local variable is no problem since you return BY VALUE.  Tha is - a copy of the local variable.  On the other hand, returning a POINTER to a local variable (or a REFERENCE) to it usually makes your program go BOOM!
0
 

Author Comment

by:abulka
ID: 2651594
alexo seems to be the first one to get the closest to the answer.  And of course, the correct typecast is (int) rather than (integer)

SIZE CNodeView1::TextExt(HDC dc, string Text)
{
  SIZE res;
  GetTextExtentPoint32(dc, Text.c_str(), (int) Text.length(), &res);
  return res;
}
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to copy an image file into clipboard C/C++? 1 199
Exception thrown at 0x00007FFD5BC81F28 7 50
draw a Christmas tree by using a nested loop? 26 78
Need some help with mailto 16 21
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question