Skip to main content
gamedev.net
🔒 Locked

strcpy problems

Started by CloudNine 8 replies 600+ views
Original Post
CloudNine
CloudNine
Hi Still working on my Win32 wrapper, but I seem to have problems just copying a char array. It''s just this line
  
strcpy(title,wtitle);
  
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
morebeer
morebeer
hard to tell without more code. the only suggestion i can make is using strncpy() instead of strcpy(), which will copy only an given number of characters (maybe the target buffer is too small)
CloudNine
CloudNine
Here''s some more code (title, width and height are part of a function):


  
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?
cgoat
cgoat
Did you actually allocate space for the title? You can''t just copy to an uninitialized pointer. Or make is a set size: "char title[256]" or use an std::string instead.
PrincessDaisy
PrincessDaisy
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
________________________
Pretty In Pink
CloudNine
CloudNine
Got it! Thanks

[edited by - CloudNine on May 9, 2003 11:08:38 AM]
CloudNine
CloudNine
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.


      
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]
Oluseyi
Oluseyi
quote:
Original post by CloudNine
Should I really use a bool for the main loop?
So long as the user of the wrapper can change the state of the boolean explicitly, there''s nothing wrong with it.

quote:
I''m having problems unregistering the class at the moment.
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.

quote:
Does WinMain never go out of scope or something?
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.
CloudNine
CloudNine
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:


    
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]
CloudNine
CloudNine
There seems to be a problem with the class names. It seems to create the window name different to the class name, and so the class doesn''t get unregistered properly, which is strange.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.