Re: Populating CString in Win32 dll interface that accepts LPCTSTR
- From: Joseph M. Newcomer <newcomer@xxxxxxxxxxxx>
- Date: Fri, 27 Jun 2008 09:47:04 -0400
See below...
On Fri, 27 Jun 2008 05:18:33 -0700 (PDT), "divya_rathore_@xxxxxxxxx"
<divyarathore@xxxxxxxxx> wrote:
I have a Win32 dll interface:****
typedef bool (__cdecl *TITLEPTR)(LPCTSTR);
and I want a string to be populated by calling this inside a UNICODE
enabled MFC App (VS2008):
CString str;
if (!(*pfn3)( str.GetBuffer(256)) )
This is totally weird syntax. If pfn3 is a pointer to a function, you can call it with
pfn3(str.GetBuffer(256);
The silly (*pfn3) notation has not been required since K&R C and is considered obsolete,
used only for backward compatibility with old programs written before ANSI/ISO C.
Therefore, there is no reason to use this obsolete and clumsy notation.
Note that in any sensible interface, you would pass in a pointer to the string AND A
LENGTH; how is it that you know for certain that 256 is the correct amount of space to
allocate (and if you "it's a file name" you're in trouble, because file names by default
are longer than 256; take a look at MAX_PATH)
{****
MessageBox(_T("Some Problemo!"));
}
..All I am getting in str is jumbled characters (ANSI?).
I see no ReleaseBuffer here. It won't work if you don't call ReleaseBuffer
****
How can I get the string str populated properly? I am clearly missing****
some Unicode concepts here.
If your app is Unicode, and your DLL returns 8-bit characters, you would declare
CStringA str;
which gives you an 8-bit string, but then you wouldn't be using LPCTSTR as the prototype
of the function, if it returned 8-bit strings; it would LPSTR. LPCTSTR says that it will
take 8-bit strings in an 8-bit app, and Unicode strings in a Unicode app, AND THAT THIS
PARAMETER REPRESENTS CONSTANT DATA AND WILL THEREFORE NOT BE MODIFIED. If the purpose is
to fill a buffer, it would have to be LPTSTR (for ANSI/Unicode), LPSTR (for ANSI) or
LPWSTR (for Unicode). Since it is a DLL, you have to declare data of the correct type
that matches the prototype of the function. Because it is a DLL, you have to use the
header file it was compiled with to determine the correct typedef for your function. You
can't just invent one out of thin air and expect it to work.
joe
****
Joseph M. Newcomer [MVP]
warm regards,
Divya Rathore
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.
- Follow-Ups:
- Re: Populating CString in Win32 dll interface that accepts LPCTSTR
- From: Tom Serface
- Re: Populating CString in Win32 dll interface that accepts LPCTSTR
- References:
- Populating CString in Win32 dll interface that accepts LPCTSTR
- From: divya_rathore_@xxxxxxxxx
- Populating CString in Win32 dll interface that accepts LPCTSTR
- Prev by Date: Populating CString in Win32 dll interface that accepts LPCTSTR
- Next by Date: Re: Populating CString in Win32 dll interface that accepts LPCTSTR
- Previous by thread: Populating CString in Win32 dll interface that accepts LPCTSTR
- Next by thread: Re: Populating CString in Win32 dll interface that accepts LPCTSTR
- Index(es):
Relevant Pages
|