Re: LPCTSTR and CString
On Wed, 25 Jun 2008 00:45:01 -0700, mido1971
<mido1971@discussions.microsoft.com> wrote:
hi,
i have a extension dll with this functuon
extern "C" AFX_EXT_API LPCTSTR WINAPI GetString()
{
LPCTSTR lpcstr ;
CString str = _T("xxx");
lpcstr =str;
return lpcstr ;
}
That function returns a pointer to memory that is freed when the function
returns. IOW, it returns a dangling pointer, which is a fatal flaw.
and i call this function in my program
extern "C" LPCTSTR WINAPI TestString();
void CMainFrame::OnGetString()
{
/......
/.....
LPCTSTR lpc;
lpc = GetString();
}
but i dont get the string i always get the ansi chars, any help thanks in
advance
another thing why i cant use CString in post of LPCTSTR if i use it i got
this warning
warning C4190: '<Unknown>' has C-linkage specified, but returns UDT
'CString' which is incompatible with C
To return CString, get rid of the extern "C". Why do you think you need to
use it? Your CMainFrame::OnGetString certainly doesn't need it. Note that
to share C++ objects between modules like this, the modules should all link
to the same CRT DLL. This is true for extension DLLs and their clients
anyway.
Also get rid of AFX_EXT_API and replace it with something like:
#if COMPILING_X_DLL
#define X_EXPORT __declspec(dllexport)
#else
#define X_EXPORT __declspec(dllimport)
#endif
Then replace "X" with a suitably unique identifier, which may be as simple
as the name of your DLL, and add COMPILING_X_DLL to your preprocessor
macros /D section. If you look in your extension DLL's preprocessor
options, you will probably find a "COMPILING_X_DLL" macro already defined
for you. You need to do this so that one extension DLL can use another; if
they all use AFX_EXT_API, there's no way it can work.
--
Doug Harrison
Visual C++ MVP