I am trying to 'port' code created in an MFC (Microsoft Foundation Class) sample to compile with DEV C++ 4.9.9.2.
First --- The original code used CString.
I figured I wasn't able to use CString so I decided to use string instead from #include <string>. Am I correct that CString is somehow exclusive to MFC and I can't access it from Dev (i don't have their IDE). MSDN says "You can freely substitute CString objects for const char* and LPCTSTR function arguments."
Second --- I have a function that takes the parameters LPTSTR
//ApplicationName and ApplicationKey are both type CString//SetApplicationInfo(LPTSTR, LPTSTR);m_SSptr->SetApplicationInfo(ApplicationName.GetBuffer(ApplicationName.GetLength()),ApplicationKey.GetBuffer(ApplicationKey.GetLength()));
Is there a way for me to convert the string to LPTSTR
LPTSTR is defined as seen below
CString::GetBuffer
LPTSTR GetBuffer( int nMinBufLength );
throw( CMemoryException );
Return Value
An LPTSTR pointer to the object’s (null-terminated) character buffer.
Parameters
nMinBufLength
The minimum size of the character buffer in characters. This value does not include space for a null terminator.
Remarks
Returns a pointer to the internal character buffer for the CString object. The returned LPTSTR is not const and thus allows direct modification of CString contents.
If you use the pointer returned by GetBuffer to change the string contents, you must call ReleaseBuffer before using any other CString member functions.
The address returned by GetBuffer may not be valid after the call to ReleaseBuffer since additional CString operations may cause the CString buffer to be reallocated. The buffer will not be reallocated if you do not change the length of the CString.
The buffer memory will be freed automatically when the CString object is destroyed
by the way in this situation, I'm using the serialshield license key generation software form ionworks.
I think I'm close to getting it to work. There is just a crash that I'm unsure of and I'm sending the code to their customer support. I'm trying to port the code from their MFC sample to DEV C++ - which I do not believe has access to MFC objects like CString.
But I think by using string instead of CString - it is causing some major crashes. But it might be other issues in the code.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
> MSDN says "You can freely substitute CString objects
> for const char* and LPCTSTR function arguments."
Does it? You should provide a link. I think however you misunderstood what it was saying. You can do that only because I presume the CString class provides conversions, so you still need MFC to do that. It does not mean you can replace one with the other.
You cannot use MFC with Dev-C++, If CString is the only MFC dependency it is likley that porting the code to use std::string is trivial.
> Is there a way for me to convert the string to LPTSTR
That depends on the build LPTSTR is a type that changes definition depending on whether you are doing a Unicode or ANSI build. For an ANSI build LPTSTR is merely a char* so std::string::c_str() can be used.
Clifford.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi
I am trying to 'port' code created in an MFC (Microsoft Foundation Class) sample to compile with DEV C++ 4.9.9.2.
First --- The original code used CString.
I figured I wasn't able to use CString so I decided to use string instead from #include <string>. Am I correct that CString is somehow exclusive to MFC and I can't access it from Dev (i don't have their IDE). MSDN says "You can freely substitute CString objects for const char* and LPCTSTR function arguments."
Second --- I have a function that takes the parameters LPTSTR
Is there a way for me to convert the string to LPTSTR
LPTSTR is defined as seen below
CString::GetBuffer
LPTSTR GetBuffer( int nMinBufLength );
throw( CMemoryException );
Return Value
An LPTSTR pointer to the object’s (null-terminated) character buffer.
Parameters
nMinBufLength
The minimum size of the character buffer in characters. This value does not include space for a null terminator.
Remarks
Returns a pointer to the internal character buffer for the CString object. The returned LPTSTR is not const and thus allows direct modification of CString contents.
If you use the pointer returned by GetBuffer to change the string contents, you must call ReleaseBuffer before using any other CString member functions.
The address returned by GetBuffer may not be valid after the call to ReleaseBuffer since additional CString operations may cause the CString buffer to be reallocated. The buffer will not be reallocated if you do not change the length of the CString.
The buffer memory will be freed automatically when the CString object is destroyed
===================================================
Any thoughts ?
Thanks
Stephen
by the way in this situation, I'm using the serialshield license key generation software form ionworks.
I think I'm close to getting it to work. There is just a crash that I'm unsure of and I'm sending the code to their customer support. I'm trying to port the code from their MFC sample to DEV C++ - which I do not believe has access to MFC objects like CString.
But I think by using string instead of CString - it is causing some major crashes. But it might be other issues in the code.
if anyone wants to take a look at the code I'll be glad to send the whole project to you. it's very simple.
> MSDN says "You can freely substitute CString objects
> for const char* and LPCTSTR function arguments."
Does it? You should provide a link. I think however you misunderstood what it was saying. You can do that only because I presume the CString class provides conversions, so you still need MFC to do that. It does not mean you can replace one with the other.
You cannot use MFC with Dev-C++, If CString is the only MFC dependency it is likley that porting the code to use std::string is trivial.
> Is there a way for me to convert the string to LPTSTR
That depends on the build LPTSTR is a type that changes definition depending on whether you are doing a Unicode or ANSI build. For an ANSI build LPTSTR is merely a char* so std::string::c_str() can be used.
Clifford.