strcpy(title,wtitle);
🔒 Locked
strcpy problems
Advertisement
Original Post
Hi
Still working on my Win32 wrapper, but I seem to have problems just copying a char array.
It''s just this line
The title and wtitle are both pointers to a char array (i.e. char*) and wtitle is a parameter in a function.
With or without including string.h, I get a wierd error, an error in msvcrt.dll. What''s the problem?
CloudNine
Here''s some more code (title, width and height are part of a function):
And the whole CGOApp class:
Is that enough?
bool CGOApp::goInitWindow(char *wtitle,int wwidth,int wheight) //Initiates the window
{
strcpy(title,wtitle);
width=wwidth;
height=wheight;
And the whole CGOApp class:
class CGOApp
{
public:
CGOApp(); //Default constructor
~CGOApp(); //Default destructor
HWND goGetWindowHandle() { return hWnd; } //Returns the window handle, since hWnd is private
HINSTANCE goGetInstance() { return hInstance;} //Returns the window instance
void goSetInstance(HINSTANCE wininstance) { hInstance=wininstance; }
bool goWindowAlive() {return windowalive; } //Returns whether the window is alive or not
bool goInitWindow(char*,int,int);
bool goKillWindow();
private:
HWND hWnd; //Handle to the window
HINSTANCE hInstance; //Handle to the window instance
HINSTANCE hPrevInstance; //Handle to the previous instance
WNDCLASSEX wc; //The window class
bool windowalive; //The status of the window
char *title; //The title of the window
int width,height; //The width and height of the window
};
Is that enough?
your title variable is only a pointer char, not a pointer to an array of char. I assume you dont new this (what would be the point to be honest), so you strcpy method is copying the source string to an undefined area of memory, hense the problem
Change it to something like :
char title[32]; (or however long the title could be)
Or if you wTitle never goes out of scope, just copy the pointer address
//--------------------------------
Pretty In Pink
Change it to something like :
char title[32]; (or however long the title could be)
Or if you wTitle never goes out of scope, just copy the pointer address
//--------------------------------
Pretty In Pink
Got it! Thanks!
I've got a different problem. The window title, and class name is stored in the title string (as you know), and I've got a boolean that says if the window is alive or not. Should I really use a bool for the main loop? i.e.
Or should I scrap that, and leave the exit conditions to the user of the wrapper? I'm having problems unregistering the class at the moment.
This function is called by the destructor, but doesn't seem to work:
Does WinMain never go out of scope or something?
[edited by - CloudNine on May 9, 2003 12:41:38 PM]
[edited by - CloudNine on May 9, 2003 12:57:15 PM]
I've got a different problem. The window title, and class name is stored in the title string (as you know), and I've got a boolean that says if the window is alive or not. Should I really use a bool for the main loop? i.e.
while (cApp.goWindowAlive())
Or should I scrap that, and leave the exit conditions to the user of the wrapper? I'm having problems unregistering the class at the moment.
This function is called by the destructor, but doesn't seem to work:
void CGOApp::goKillWindow()
{
UnregisterClass(title,hInstance);
}
Does WinMain never go out of scope or something?
[edited by - CloudNine on May 9, 2003 12:41:38 PM]
[edited by - CloudNine on May 9, 2003 12:57:15 PM]
quote:So long as the user of the wrapper can change the state of the boolean explicitly, there''s nothing wrong with it.
Original post by CloudNine
Should I really use a bool for the main loop?
quote:Why are you bothering with unregistering the class? Not that there aren''t valid reasons to do so, but all windowclasses get unregistered when the application exits. Unless you feel it necessary to reuse the registered class within the application, there''s no problem.
I''m having problems unregistering the class at the moment.
quote:Functions don''t go out of scope, they terminate. WinMain terminates either when an explicit return statement is encountered or execution falls through the end (this will generate a warning since WinMain expects to return a value). What you need to look at is the conditions for your message loop. I suppose the goWindowAlive function does that. Post the internals of that function so we can see why it may not be terminating. Personally, I suspect that you''re using GetMessage or PeekMessage with the HWND of your main window. That only looks in the message queue of your own window, and the WM_QUIT message isn''t placed in that queue. You can use NULL instead.
Does WinMain never go out of scope or something?
Thanks for the function terminology, but the window runs fine, until I quit it. The window is destroyed, but the class does not unregister properly. Here's the WinMain function:
Here's the simple goWindowAlive() function
[edited by - CloudNine on May 9, 2003 2:28:52 PM]
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmd, int nShow)
{
cApp.goSetInstance(hInstance);
cApp.goInitWindow("Testing!",800,600);
MSG messages;
while (cApp.goWindowAlive())
{
if(GetMessage(&messages,NULL,0,0)>0)
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
}
return messages.wParam;
}
Here's the simple goWindowAlive() function
bool goWindowAlive() {return windowalive; } //Returns whether the window is alive or not
[edited by - CloudNine on May 9, 2003 2:28:52 PM]
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement