|
I have an TCHAR array,
my buf:
TCHAR szBuf[MAX_PATH] = _T("buf");
a function;
void DealFunc(char *pStr);
I wanna use DealFunc, help me how to use it!
Hope I make the question clear!
|
|
|
|
|
like this;
DealFunc( szBuf );
Love
|
|
|
|
|
But if compiled with _UNICODE, it would not pass.
Is it the only way to do like this?
char szBuf1[MAX_PATH * 2];
#if defined(_UNICODE) || defined(UNICODE)
WideCharToMultiByte(CP_ACP, 0, szBuf, -1, szBuf1, MAX_PATH * 2);
#else
sprintf(szBuf1, szBuf);
#endif
DealFunc( szBuf1 );
|
|
|
|
|
yes, I think this is the only way.
char szBuf1[MAX_PATH * 2];
#if defined(_UNICODE) || defined(UNICODE)
WideCharToMultiByte(CP_ACP, 0, szBuf, -1, szBuf1, MAX_PATH * 2);
#else
strcpy(szBuf1, szBuf);
#endif
DealFunc( szBuf1 );
Olympic Beijing 2008
|
|
|
|
|
|
First of all, I recommend you to read this article - http://www.codeproject.com/KB/string/cppstringguide1.aspx[^]
fantasy1215 wrote:
I wanna use DealFunc, help me how to use it!
Hope I make the question clear!
1) If UNICODE is not defined in your project, the TCHAR will expand to char and you can directly call DealFunc() since it expects char* .
2) Well, If your project have UNICODE defined, then your TCHAR will expand to wchar_t and in that case you cannot call DealFunc() because it signature is to accept char* .
Solutions
1) Change your function signature to TCHAR.
void DealFunc(char *pStr);
2) Change your buffer to char
char szBuf[MAX_PATH] = "buf";
3) If you don't want to change either of these, convert your unicode string to ansi and call your DealFunc() . Check this link - http://weseetips.com/2008/05/07/how-to-convert-unicode-to-ascii-and-vice-versa-more-easily/[^]
Did i explained too much details?
Regards,
Jijo.
_____________________________________________________
http://weseetips.com[ ^] Visual C++ tips and tricks. Updated daily.
|
|
|
|
|
Thanks for your all enthusiastic. I really appreciate.
people like you all make the world warm!
|
|
|
|
|
Thanks a lot for your kind words.
Regards,
Jijo.
_____________________________________________________
http://weseetips.com[ ^] Visual C++ tips and tricks. Updated daily.
|
|
|
|
|
#include <atlconv.h>
TCHAR szBuf[MAX_PATH] = _T("buf");
USES_CONVERSION;
DealFunc( T2A( szBuf ));
T2A/W2A converts from unicode to ansi and T2W/A2W converts from ansi to unicode.
Nibu babu thomas
Microsoft MVP for VC++
Code must be written to be read, not by the compiler, but by another human being.
Programming Blog: http://nibuthomas.wordpress.com
modified on Friday, August 15, 2008 2:25 AM
|
|
|
|
|
You would really recommend him to do something like that? I thought one must either stick to generic text mappings, or NOT. But that's my opinion.
Many are stubborn in pursuit of the path they have chosen, few in pursuit of the goal - Friedrich Nietzsche
.·´¯`·->Rajesh<-·´¯`·.
[Microsoft MVP - Visual C++]
|
|
|
|
|
Rajesh R Subramanian wrote: You would really recommend him to do something like that?
Reason being that it might be a external library API which he cannot modify and just in case if he can modify the prototype of this function then the parameter should be changed to TCHAR form.
Nibu babu thomas
Microsoft MVP for VC++
Code must be written to be read, not by the compiler, but by another human being.
Programming Blog: http://nibuthomas.wordpress.com
|
|
|
|
|
Nibu babu thomas wrote: just in case if he can modify the prototype of this function then the parameter should be changed to TCHAR form.
Exactly!
But I agree, it has happened to me more than once that I have to write sub standard code, only because then it would work with some existing component or framework, whose source is closed to me. I hate it.
Many are stubborn in pursuit of the path they have chosen, few in pursuit of the goal - Friedrich Nietzsche
.·´¯`·->Rajesh<-·´¯`·.
[Microsoft MVP - Visual C++]
|
|
|
|
|
The right way is to make your DealFunc(TCHAR *pStr);
so you are independent to what TCHAR expands. Everything else is a bad barter.
Believe me: I am actually changing a lot of my code because I need it to be Unicode compatible.
Greetings from Germany
|
|
|
|
|
If you are using Generic-Text Mappings, learn all the conventions and stick to the rules. Don't mix and match TCHAR with char.
Many are stubborn in pursuit of the path they have chosen, few in pursuit of the goal - Friedrich Nietzsche
.·´¯`·->Rajesh<-·´¯`·.
[Microsoft MVP - Visual C++]
|
|
|
|
|
DealFunc is thirdparty code, can't modify it!
So I ask the wired question!
|
|
|
|
|
VC++6.0
When I call function lstrlenA in kernel32.dll error.
------------------------------------------
typedef long(*Myfun)(long);
Myfun pfun;
HMODULE hMod;
BOOL bRes;
hMod = LoadLibrary("kernel32.dll");
pfun = (Myfun)GetProcAddress(hMod,"lstrlenA");
long t1 = 3434;//example
long t2 = (pfun)(t1);//this is Error !!!
getch();
bRes = FreeLibrary(hMod);
-------------------------------------------
thanks very much.
modified on Friday, August 15, 2008 12:40 AM
|
|
|
|
|
Here is the prototype of lstrlen,
int lstrlen( LPCTSTR lpString
);
Implemented as ANSI and Unicode versions
And lstrlenA can be
int lstrlenA( LPCSTR lpString
);
You should use a string ptr not a long number to use lstrlen!
try this:
CHAR buf[MAX_PATH] = "hello";
lstrlenA(buf);
|
|
|
|
|
phan xuan nguyen wrote: typedef long(*Myfun)(long);
What's this nonsense? You can't just make up you own function prototype and call a function with a different one through it. The prototype for lstrlenA looks as follows (found by searching SDK header files):
int
WINAPI
lstrlenA(
LPCSTR lpString
);
The macro WINAPI specifies the calling convention (on x86 machines this will be __stdcall ).
Code like this works fine:
typedef int (WINAPI *p_lstrlenA_t)(LPCSTR lpString);
HMODULE hMod = LoadLibrary("Kernel32.dll");
p_lstrlenA_t pFun = (p_lstrlenA_t)GetProcAddress(hMod, "lstrlenA");
int Length = (*pFun)("Measure me!");
FreeLibrary(hMod);
Steve
|
|
|
|
|
This isnt the normal way to use this function. Include the Header and lib!!!
Why arent you read the help for lstrlen and think about it instead asking these very primitive questions.
Greetings from Germany
|
|
|
|
|
Woa! Easy tiger..
How do you
(a) Insert code into a code cave, that calls functions from libraries not used by the original program?
(b) Use code from inside a .dll file that has a different file extension?
(c) Hide the loaded dll from the imports table of an exe file?
(d) Create an exe file protector?
Answer: By using the LoadLibrary & GetProcAddress functions, amongst others.
The point is: None of these are primitive subjects, nor is your post particularly helpful.
If you'd wondered, It was indeed I that gifted you that 1 vote.
|
|
|
|
|
KarstenK wrote: This isnt the normal way to use this function.
True, but that doesn't mean it can't be done. Maybe the OP was just playing around with a concept and this was his attempt at it. Sometimes our examples are not truly indicative of the actual problem.
"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
|
|
|
|
|
|
Considering that the pointer p is NULL sometimes,
which is more efficient:
1. delete p;
2. if (p) delete p;
system
|
|
|
|
|
Test it!
I'm voting for the second method - slightly more efficient in CPU,
less efficient in code size...just a guess.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Deleting null ptr is legal in C++. The delete operator already does the null checking. Check here - http://www.devx.com/tips/Tip/14443[^]
So the null checking in "if (p) delete p;" is not necessary and obviously its a few clock cycles less efficient than "delete p;" .
Regards,
Jijo.
_____________________________________________________
http://weseetips.com[ ^] Visual C++ tips and tricks. Updated daily.
|
|
|
|