Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to convert char* or CString to fixed char[] using VC++ 1

Status
Not open for further replies.

hvytas

Programmer
Aug 10, 2005
23
LT
Hello,

I got a problem, I can't convert char* or CString to fixed char[]
using VC++, I try many books, but find only this, for example:

#define MAX_ME 100
char szString[MAX_ME];

I want to move from string or char to fixed char[] and add fixed lenght
for char, but not successed, somebody know how to fix this problem.

I want to use like this example:
CString sDEMO;
sDEMO="only for demonstration";
char szString[]=sDEMO; //<- But this method not working
//char szString[]="only for demonstration"; //<- This method working, but I want
get from other location like from file string and add to fixed char[] and add
fixed lenght.

Why I want to use this? Becouse in ISAPI filter, that only supported
some functions fixed char[] and not supported like char* or something else.

P.S. Sorry for my English
 
char[] IS a char*

Do you get an error when you pass a char* to the ISAPI function?
 
Hi cpjust,

Thank's for answer.

>Do you get an error when you pass a char* to the ISAPI function?
Yes.

If I use char *szString, in ISAPI filter then compilling VC++, show this:

error C2065: 'szString' : undeclared identifier

and this:

warning C4311: 'type cast' : pointer truncation from 'char *' to 'DWORD'

Somebody know how to fix it?
 
Something is fishy here.

DWORD != char [] ISAPI or not.

When going from CString to char[] (or char*) you need to take into account that CString is based on TCHAR, which only corresponds to the char type in non-unicode builds.

If you're sure youre not in unicode then
Code:
char sz[MAX_ME];
CString s("Foo");
// Perhaps make sure s.GetLength()<MAX_ME?
strcpy(sz,s);
should work.


Becouse in ISAPI filter, that only supported
some functions fixed char[]
What functions would that be?


/Per
[sub]
www.perfnurt.se[/sub]
 
Actually, if you want to avoid automatic casting, it should be
Code:
strcpy (sz, (LPCTSTR) s);
The LPCTSTR operator returns a const char*
 
error C2065: 'szString' : undeclared identifier

That makes me think the problem is entirely different and has nothing to do with passing a char[] to an ISAPI function. The compiler doesn't recognize that variable.
 
Thank's for all for replays,

I use this function in ISAPI filter:
pfc->WriteClient (pfc, szString, &dwString, 0);

In this function:
WriteClient(.., LPVOID, LPDWORD, DWORD)

I using this before this function:
char szString[MAX_ME];
DWORD dwString=sizeof(szString);
strcpy(szString,"Test");

And this is not good, becouse I want to add like sting
or somthing information with not fixed lenght. Somebody
know how to solve this problem?
 
I find this way, but I want to add not like text like string from other place, how to solve this problem, any ideas?

This Example work, but I want add string from other way:
LPCTSTR lpsz;
DWORD dwBytesWritten;

lpsz=TEXT("DEMO");
dwBytesWritten = lstrlen (lpsz);

pfc->WriteClient (pfc, (PVOID) lpsz, &dwBytesWritten, 0);
 
Not sure I understand the question, but this should work
Code:
CString s("DEMO");
//...
LPCTSTR lpsz = s;
DWORD dwBytesWritten = s.GetLength();
pfc->WriteClient (pfc, (PVOID) lpsz, &dwBytesWritten, 0);

or even briefer
Code:
CString s("DEMO");
//...
DWORD dwBytesWritten = s.GetLength();
pfc->WriteClient (pfc, (PVOID)(LPCTSTR)s, &dwBytesWritten, 0);




/Per
[sub]
www.perfnurt.se[/sub]
 
Not sure if this is what you want, but here goes.

Code:
	// Convert from CString to fixed char[]
	// I will try.
	CString ThisString = "Hello, there!";
	char NewString[] = "This could be any string";
	unsigned int StringLen;
	unsigned int upper_limit;

	StringLen = ThisString.GetLength();
	upper_limit = (StringLen < strlen(NewString)) ? StringLen : strlen(NewString);
	m_Text1.Format("Len = %ud", upper_limit);
	unsigned int i;
	for (i = 0; i < upper_limit; i++)
	{
		NewString[i] = ThisString.GetAt(i);
	}
	NewString[i] = '\0';

Make sure the initial size of NewString is bigger than what you expect the CString to be.

The conditional if statement is used to ensure you don't accidentally go passed the end of your array, which could crash your program.

Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top