开发者

C basic datatype problem - const char * to LPCTSTR

开发者 https://www.devze.com 2023-01-08 05:15 出处:网络
#include \"stdafx.h\" #include \"string.h\" #include \"windows.h\" bool SCS_GetAgentInfo(char name[32],char version[32], char description[256], const char * dwAppVersion)
#include "stdafx.h"
#include "string.h"
#include "windows.h"
bool SCS_GetAgentInfo(char name[32],char version[32], char description[256], const char * dwAppVersion)
{
    strcpy(name,gName);
    strcpy(version,gVersion);
    strcpy(description,gDescription);
    notify(dwAppVersion);
    return true;
}

void notify(const char * msg)
{
    MessageBox(NULL, TEXT(msg), NULL, NUL开发者_JAVA技巧L);
}

I have managed to work with the first three fields fine, but I am running into issues with the const char *. I have tried passing and casting in alot of different ways, but can't get it to work. I googled around, but couldn't find much on Lmsg. I am new to alot of this. I have read around and I think it may have to do with encoding. What really confuses me is LPCTSTR is defined as a const char *, but straight typecasting doesn't give me anything from the field.

I get an error that Lmsg is undeclared which I am guessing means that the Macro expansion of TEXT is causing this. How can I get this working?

Doing MessageBox(NULL, (LPCTSTR)msg, NULL, NULL); instead gives me a bunch of boxes indicating it probably is referencing the wrong characters, but copying the dwAppsVersion parameter into the description shows the correct information.


The problem is that you're building you application to use UNICODE Win32 API's, but you're passing around non-UNICODE strings. You have two options:

  1. convert the msg string to Unicode using something like MultiByteToWideChar(). This is probably the 'right' way to do it, if a bit more complex because you need to deal with codepages and managing the buffers used for the conversion.

  2. you can force the ANSI version of the API to be used:

    MessageBoxA(NULL, msg, NULL, NULL); 
    

That's a simple workaround, if not elegant.

Other options include only building the application to use Win32 ANSI APIs instead the Unicode APIs or changing the strings you pass around as LPTSTR and using the TEXT() or _T() macros for your literals. However, if you're reading non-Unicode data from files or elseswhere, then you still have to deal with the conversion at some point...


An LPCTSTR is an alias for const TCHAR *, and TCHAR is a type used in Windows programming to ease the transition between the ANSI (Windows-1252, very similar to the internationally standardized ISO 8859-1) and Unicode text encodings.

If your project is set up to build your app using ANSI, TCHAR is really char, and you would be able to pass msg to MessageBox without a cast.

If your app is set up to build using Unicode (which is what it sounds like), TCHAR is really wchar_t, and you would have to convert the string from ANSI to Unicode using a function like MultiByteToWideChar().

Simply casting just forces the compiler interpret the type differently without changing the data; in this case, that's not enough because the actual data must be converted from one format to another.


It's hard to tell exactly what's going on in your question since you appear to have left some relevant context out. For example LPCTSTR isn't mentioned anywhere, so I can only guess at what you're talking about, or what "the first three fields" are.

One thing to note is that LPCTSTR is not always const char*, it is in an ANSI build, but it is const wchar_t* in a Unicode build. This most likely the issue you're running into.

Also, the TEXT() macro is only for defining string constants. You can't use it to perform a conversion on a variable, this is why you're getting 'Lmsg undeclared'.

If you aren't intentionally using a Unicode build, you may want to change your project settings to an ANSI build as a work-around. Otherwise, you may want to read a tutorial on working with Unicode, which you really should be familiar with if you are writing software for Windows these days.

0

精彩评论

暂无评论...
验证码 换一张
取 消