Sign in to follow this  
Matthew Shockley

Simple Error Help

Recommended Posts

Does anyone know how I can fix this?
	int Length = GetWindowTextLength(nWind);
	wchar_t Buffer[Length];
	GetWindowText(nWind, Buffer, sizeof(Buffer)/sizeof(Buffer[0]));
I keep getting these errors:
1>c:\users\matt\documents\visual studio 2008\projects\ustest\ustest\main.cpp(13) : error C2057: expected constant expression
1>c:\users\matt\documents\visual studio 2008\projects\ustest\ustest\main.cpp(13) : error C2466: cannot allocate an array of constant size 0
1>c:\users\matt\documents\visual studio 2008\projects\ustest\ustest\main.cpp(13) : error C2133: 'Buffer' : unknown size
1>c:\users\matt\documents\visual studio 2008\projects\ustest\ustest\main.cpp(14) : error C2070: 'wchar_t []': illegal sizeof operand
Thanks in advance!

Share this post


Link to post
Share on other sites
The array size must be a compile time constant and cannot be evaluated at run time. Use a dynamic vector instead.

int Length = GetWindowTextLength(nWind);
std::vector<wchar_t> Buffer(Length);
GetWindowText(nWind, &Buffer[0], Length);

Share this post


Link to post
Share on other sites
Now this error:

1>c:\users\matt\documents\visual studio 2008\projects\ustest\ustest\main.cpp(17) : error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::vector<_Ty>' (or there is no acceptable conversion)


With this code:

	std::wofstream File;
File.open("Test.lua");
File << Buffer;
File.close();

Share this post


Link to post
Share on other sites
int Length = GetWindowTextLength(nWind);
wchar_t Buffer = new wchar_t[Length];
GetWindowText(nWind, Buffer, Length);


Remember to delete or use a smart_ptr.

Or using Brother Bob's example you might use
File << &Buffer[0];

Share this post


Link to post
Share on other sites
I got it to work:

	int Length = GetWindowTextLength(nWind);
wchar_t *Buffer = new wchar_t[Length+1];
GetWindowText(nWind, Buffer, Length+1);
std::wofstream File;
File.open("Test.lua");
File << Buffer;
File.close();
delete Buffer;


Any suggestions for anything I am doing sloppy? Thanks!

Share this post


Link to post
Share on other sites
Stick with Bob's vector code if you can. Otherwise, remember to use delete [] Buffer (as you've allocated an array, not just a single object).

Share this post


Link to post
Share on other sites
Quote:
Any suggestions for anything I am doing sloppy? Thanks!
Here's a slightly revised version of your code (not compiled or tested):

std::vector<wchar_t> buffer(GetWindowTextLength(nWind) + 1);
GetWindowText(nWind, &buffer.front(), buffer.size());
std::wofstream file("Test.lua");
file << &buffer.front();



The main differences are:

1. Use of std::vector makes the code (more) exception safe, and obviates the need for manual cleanup and for 'remembering' the length of the buffer.

2. Opening the file using the wofstream constructor tightens the code up a bit.

3. The (most likely) unnecessary call to close() has been removed (I'm assuming that this code resides in a function or some other scoped block, and that the file object will therefore go out of scope soon after this code finishes executing).

There are probably some opportunities for error-checking there as well, but I didn't address them in my example.

[Edit: Some of these improvements were already suggested in earlier posts.]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this