I am using visual studio 2010 MFC to build a C++ program. My program calls a DLL that is not apart of the project and it accepts a char*. I have a function that gets a string in a format of LPCTSTR. I have been on google for about two hours now, and no solution found. How do I convert form a MFC LPCTSTR to a char*. Everything I have found either does not work, or just does not compile.
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
This was the way i used to typecast the variables
LPCTSTR
is either defined as aconst wchar_t *
or aconst char *
depending on whether your project defined the preprocessor symbolUNICODE
(or_UNICODE
, I forget which one MFC uses).So the solution to your problem depends on whether you're using the UNICODE setting or not.
If you are using it, you'll need to convert the string to a narrow string. Use
CStringA
to do this.If you're not using UNICODE you'll need to make a copy that is mutable and pass it to the DLL, in case it wants to modify the string. You can do this by creating a copy using
CString
.In either case, once you have a copy in a
CString
object then use theGetBuffer
method to get a mutable pointer to the string, call the DLL function and then callReleaseBuffer
after the call.In MFC the easiest is to convert through
CStringA
(provided that resulting buffer will be a read-only argument):Other options are available and were discussed:
WideCharToMultiByte
,T2A
macros etc.