|
If you know you have a multibyte string, you can simply use strlen() to obtain the number of bytes.
void SomeFunction(LPSTR pString)
{
int nChars = _mbslen(pString);
int nBytes = strlen(pString);
[...]
}
If you are using the TCHAR macros you can do it like this:
void SomFunction(LPTSTR pString)
{
int nChars = _tcsclen(pString);
int nBytes = _tcslen(pString) * sizeof(TCHAR);
}
Note _tcslen is different than _tcsclen .
If _UNICODE is not defined, _tcslen resolves to strlen , and sizeof(TCHAR)==1 .
If _UNICODE is defined, _tcslen resolves to wcslen , and sizeof(TCHAR)==2 .
So, _tcslen(pString) * sizeof(TCHAR) will give you the number of bytes in any case.
Hope that helps,
--
jlr
http://jlamas.blogspot.com/[^]
|
|
|
|
|
Jose Lamas Rios wrote:
If you know you have a multibyte string, you can simply use strlen() to obtain the number of bytes.
can you not have a NULL in the middle of a multi-byte string?
Jose Lamas Rios wrote:
If _UNICODE is not defined, _tcslen resolves to strlen, and sizeof(TCHAR)==1.
If _UNICODE is defined, _tcslen resolves to wcslen, and sizeof(TCHAR)==2.
So, _tcslen(pString) * sizeof(TCHAR) will give you the number of bytes in any case.
If _UNICODE is not defined, and _MBCS is defined, _tcslen resolves to _mbslen. And you know, _MBCS is defined by default in the project settings. So, _tcslen(pString) * sizeof(TCHAR) will not always give you the number of bytes all the time...
|
|
|
|
|
|
Two above posts are correct, except only the one thing. As MSDN says :
"these functions returns the number of characters in string, excluding the terminal NULL."
So, I suppose, the correct answer on the question will be
(_tcslen(str) + 1) * sizeof(TCHAR)
Andrew
|
|
|
|
|
|
Hi all,
Can I make a variable argrument function like foofunc(char*, const char *,...); as a virtual function in C++ class.?.
Thanks in Advance
Abhi Lahare
|
|
|
|
|
|
I dont have a compiler to test this right now, but c++ allow the following two to be present simultaneously? My guess is No. How about the first one in the base class and the second on in the derived class? Myy guess again is no, but this time I am less confident. Can someone clarify with reasons?
foofunc(char*, const char *,...);
foofunc(char*, const char *, int);
|
|
|
|
|
insert a 'does' there - read it as "...,but does c++ allow..."
|
|
|
|
|
Flace wrote:
My guess is No
why?
this works exactly as expected:
class foo
{
public:
virtual void doo(int i, ...)
{
TRACE("foodoo\n");
}
} ;
class voo : public foo
{
public:
virtual void doo(int i, int x)
{
TRACE("voodoo\n");
}
};
...
foo f;
f.doo(0, 0);
voo v;
v.doo(0, 0);
foodoo
voodoo
Cleek | Image Toolkits | Thumbnail maker
|
|
|
|
|
I see. How about this:
foo *f = new doo;
f->doo();
|
|
|
|
|
eeek...I meant foo *f= new voo;
|
|
|
|
|
|
The first one is no surprise as the compiler will pick the method of class on which you are calling the function. The second one is interesting, but it too is not a surprise to me as you have defined doo as virtual in the base class. Can you do a couple more tests:
1. Do not define doo as virtual in foo, and try this:
<br />
foo *f = new voo;<br />
f->doo(3,3); <br />
2. what if both the prototypes are in the same class, which one does the compiler pick:
<br />
class foo {<br />
void doo(int i, ...) {<br />
TRACE ("int i, ...");<br />
}<br />
void doo(int i, int j) {<br />
TRACE("int i, int j");<br />
} <br />
}<br />
foo f;<br />
f.doo(3, 3);<br />
|
|
|
|
|
1. foodoo
2. int i, int j
why do you think this would behave any differently than a function that doesn't have a vararg parameter ?
Cleek | Image Toolkits | Thumbnail maker
|
|
|
|
|
I was basically expecting an error thinking that C++ compiler would get confused in resolving the function call. It looks like the function with explicit arguments takes precedence over the function with varargs.
I wonder how this extends to function pointers...let me experiment this myself tomorrow.
|
|
|
|
|
I have recently tried incorporating someone's codes into my codes in visual c++. However their codes are in C. Anyway I have just added their .C and .H files into my project workspace and called one of their functions in my main program. I am unable to cmpile due to the error below:
regis.obj : error LNK2001: unresolved external symbol "struct Coordinates __cdecl findRotationMatrix(struct Coordinates)" (?findRotationMatrix@@YA?AUCoordinates@@U1@@Z)
Debug/regis.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
I understand that the compiler is looking for a library file but the problem is the codes i downloaded only came with the .C and .H file. If i am not wrong, the original codes were compiled successfuly in gcc. Is this a compatability problem?
|
|
|
|
|
findRotationMatrix is this declared in .h ?
|
|
|
|
|
It must be - he didn't encounter any compilation errors. Looks like he's missing a library.
/ravi
My new year's resolution: 2048 x 1536
Home | Articles | Freeware | Music
ravib@ravib.com
|
|
|
|
|
Ravi Bhavnani wrote:
Looks like he's missing a library
Oops, you are very FAST
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
|
|
|
|
|
Cillieacc0rd5o4 wrote:
findRotationMatrix is this declared in .h ?
Yeap,It's declared in .h File as there is no compiler error i.e. Function was found in Header file while compiling but since linker throw the error, it's seem that it enable to get DEFINATION of findRotationMatix function
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
|
|
|
|
|
I believe your problem is that the function you're trying to use is in the .c file, and you're calling it from the .cpp file.
The compiler doesn't decorate names from the .c file, but it does decorate them from the .cpp. That why it appears to be calling:
glycemia wrote:
findRotationMatrix(struct Coordinates)" (?findRotationMatrix@@YA?AUCoordinates@@U1@@Z)
In the header file for the function (the header that goes with the .c file) you should place the following:
At the top of the header file
#if defined(__cplusplus)<br />
extern "C"<br />
{<br />
#endif<br />
Then put the rest of the header file in here.
and then at the bottom of the same header file:
#if defined(__cplusplus)<br />
}<br />
#endif<br />
That should resolve your problem.
Good luck.
Karl - WK5M
PP-ASEL-IA (N43CS)
<kmedcalf@ev1.net>
PGP Key: 0xDB02E193
PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193
|
|
|
|
|
Thank you so much for your help....your suggestion has solved my problem....
Does this mean I should convert the .c file into .cpp file in the future?
Thanks for all your help guys!!
|
|
|
|
|
Not necessarily. It is your choice, but you may run into other problems by converting it to .cpp.
If you're using mfc, you'll need to add the stdafx.h header, a wrapper to prevent multiple inclusions would be a good idea, etc.
Unless there's a compelling reason to change it to .cpp, then I'd just leave it like it is.
The software I work on (a suite of 38 applications and 18 dlls) has one module that is shared with a unix-based hardware component, and that has to be in c, so we just have the one module in c.
Glad I could be of help.
Karl - WK5M
PP-ASEL-IA (N43CS)
<kmedcalf@ev1.net>
PGP Key: 0xDB02E193
PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193
|
|
|
|
|
but did you think to #include the header whenever you imported them into your project ?
TOXCCT >>> GEII power [toxcct][VisualCalc]
|
|
|
|