CString Convert to LPCTSTR
i write DLL file,it is static dll
i want CString type Convert to LPCTSTR
but error
Code:
<<<<<<<<<<<Dll File>>>>>>>>>
<<<<<<<A.H>>>>>>>
class AFX_NOVTABLE dll
{
public:
__declspec(dllexport) LPCTSTR Fun();
};
<<<<<<<A.Cpp>>>>>>
LPCTSTR dll::Fun()
{
CString Rxg=_T("rxg");
LPCTSTR lpszStr=Rxg.GetBuffer();
return lpszStr;
}
<<<<<<<<Exe>>>>>>>>>
#pragma comment(lib,"dll.lib")
#include "A.h"
..................
dll d;
LPCTSTR b=d.Fun();

error,why
thank:)

Dani AI

Generated

Short answer: the LPCTSTR you return points into a local CString (the CString object is destroyed when the function returns), so the pointer becomes dangling. ’s pointer-access tips (c_str/cast) show how to get a pointer from a CString, and correctly raised export/import concerns — but neither fixes the lifetime or cross-module memory/ABI issues.

Recommended approaches (pick one that fits your API):

  • Caller-supplied buffer (robust, simple): let the caller allocate the buffer and pass it in so the DLL copies the text into memory the caller owns. This avoids any lifetime problems and is the usual C-style pattern for stable DLL APIs.

    extern "C" __declspec(dllexport) void GetText(TCHAR* outBuf, size_t outBufChars)
    {
      const TCHAR* src = _T("rxg");
      _tcsncpy_s(outBuf, outBufChars, src, _TRUNCATE);
    }
  • DLL allocates, caller frees (COM-style): allocate with CoTaskMemAlloc (or provide a matching free function) and document that the caller must call CoTaskMemFree. This is useful when the caller cannot easily predict buffer size.

extern "C" __declspec(dllexport) LPTSTR AllocText()
{
    const TCHAR* src = _T("rxg");
    size_t len = _tcslen(src) + 1;
    LPTSTR out = (LPTSTR)CoTaskMemAlloc(len * sizeof(TCHAR));
    if (out) _tcscpy_s(out, len, src);
    return out; // caller must CoTaskMemFree()
}

Notes and cautions:

  • Returning a pointer into a local CString is undefined behavior. Do not return pointers to temporaries or stack/local objects.
  • Returning MFC types (CString) or exporting C++ classes across DLL boundaries requires identical compiler/CRT/MFC settings for caller and DLL — fragile unless both are built consistently. For maximum compatibility, export a C-style API (extern "C").
  • If the string is a literal, returning the literal pointer is safe; for mutable strings or runtime values, use one of the patterns above.

Recommended Answers

All 3 Replies

Look up c_str() function.

try this

LPCTSTR lpszStr=(LPCTSTR)Rxg;

>>LPCTSTR b=d.Fun();

post the complete error message, I don't think you exported/imported the function correctly. This is a macro I use. When compiling the DLL, define the macro MYEXPORT, when compiling the application then do not define it.

#ifdef MYEXPORT
#define DllExport  __declspec( dllexport )
#else
#define DllExport  __declspec( dllimport )
#endif

class AFX_NOVTABLE dll 
{
public:
DllExport LPCTSTR Fun();
};

thank you:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.