Solved

Problem related to CString usage.

Posted on 2013-01-21
3
515 Views
Last Modified: 2013-01-21
This should be easy, but I can't figure it out.  I'm trying to call a function from MFC based C++ code.  The function signature is:
Add(HZIP hz, const TCHAR *dstzn, void *src, unsigned int len, DWORD flags);

The 3rd parameter is what is causing my problem.  It is supposed to be the full path and file name of a file to be taken into a process.  In my case that full path is contained in a CString object.  I've tried several ways to get the CStrinng content into an acceptable format, but I cannot get anything to work.  For example, if I use the CString directly, I get the compiler complaint:
C2664: Add: cannot convert parameter 3 from 'CString' to 'void *'

if I try (LPCSTR)CString I get:
C2664: 'Add' : cannot convert parameter 3 from 'LPCSTR' to 'void *'      

Using VS2005

Any help would be appreciated.  Thanks
0
Comment
Question by:Onager
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 8

Expert Comment

by:virtuadept
ID: 38801271
Cast you CString as (void *). The third parameter is expecting a void pointer.

More specifically, lets say you have a variable "path" which is a Cstring,

parm3 should be:

... , (void *) &path, ...


& means the address of, so that is casting address of "path" as void pointer.
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 150 total points
ID: 38802083
I think it is not correct to cast CString directly to void *
Regarding LPCTSTR, it returns a const pointer, which cannot be casted automatically to non-const void pointer
I would use CString::GetBuffer() instead (http://msdn.microsoft.com/en-us/library/aa314880(v=vs.60).aspx), which returns a LPTSTR, which is non-const
0
 

Author Closing Comment

by:Onager
ID: 38802379
Both solutions got me past the compiler complaint but the 1st solution failed inside of the called function.  The getbuffer approach worked like a charm.

Thanks to both.  Mike
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
A short article about problems I had with the new location API and permissions in Marshmallow
Introduction to Processes
Starting up a Project

705 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