Archived

This topic is now archived and is closed to further replies.

Pagan

Access Violation

Recommended Posts

,hi, you guys, i've used a SYSTEM_INFO and the code like this
    
SYTEM_INFO inf;
GetSystemInfo(&inf);
MessageBox((LPTSTR)inf.dwPageSize, "Page Size", MB_OK | MB_ICONINFORMATION);
  
it compiled okay, but when i execute it, an error occurs, 'Access Violation', how could i solve it? should i declare the variable as global? thanks in advance. Edited by - Pagan on October 29, 2001 8:19:53 PM

Share this post


Link to post
Share on other sites
Suggest you look up the definition for MessageBox ... first parameter is a HWND (which can be set null) second: Message in the box, third: title text fourth: messagebox button layout and/or an icon displayed to the left of the message

Share this post


Link to post
Share on other sites
Is this?

  
SYSTEM_INFO inf;
GetSystemInfo(&inf);
MessageBox(NULL, (LPTSTR)inf.dwPageSize, "ERROR"MB_OK | MB_ICONINFORMATION);


but it says that MessageBox only has 3 parameters
, but i see it in other documents, it has 4. what is wrong with it?

thanks

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
MFC uses a 3-parameter version of MessageBox, which does not take an HWND. This may be why it seems to be a 3-parameter function.

P.S. To call a standard windows function from within an MFC app, just add two colons :: before it.

::MessageBox(NULL,"Hello!","A Message",MB_OK);

Can''t see how it will, but certainly HOPE this helps

Share this post


Link to post
Share on other sites
I use this ::MessageBox() function very well only when

    
::MessageBox(NULL, "Hello", "Title", MB_OK);



but when i add a SYSTEM_INF variable as a parameter, the problem comes again

[source
::MessageBox(NULL, (LPTSTR)inf.dwPageSize, "Page Size", MB_OK | MB_ICONINFORMATION);
[/souce]

and i use Windows XP, is that has something wrong with OS?

thanks

Edited by - Pagan on October 29, 2001 8:48:54 PM

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I use this ::MessageBox() function very well only when

  
::MessageBox(NULL, "Hello", "Title", MB_OK);


but when i add a SYSTEM_INF variable as a parameter, the problem comes again

  
::MessageBox(NULL, (LPTSTR)inf.dwPageSize, "Page Size", MB_OK | MB_ICONINFORMATION);
[/souce]

and i use Windows XP, is that has something wrong with OS?

thanks

Share this post


Link to post
Share on other sites
Why are you trying to convert a DWORD into an LPTSTR??? I think that this is the problem. Converting a DWORD to an LPTSTR will NOT output the DWORD''s value as a string. You''ll have to use sprintf() or something to achieve that. Example:

int a = 13;
char stuff[80];
sprintf(stuff,"%d",a);
MessageBox(NULL,stuff,"Hello!",MB_OK);

This should work (only replace int a with that dwPageSize thingy).

BTW, how do you get that nice edit-box thingy to put your source code in on this forum?

I finally got it all together...
...and then forgot where I put it.

Share this post


Link to post
Share on other sites
The problem is simply that you're trying to use a DWORD variable as though it were a pointer to a string. LPTSTR is a typedef for TCHAR*, and TCHAR is a typedef for char* (or whichever type is used to represent a char, depending upon the kind of build). So, by casting dwPageSize to a LPTSTR, you're telling MessageBox to find a string located at the address represented by the numerical value in dwPageSize. Needless to say, there is no actual string located there, so you get an access violation.

Neither C nor C++ support strings as built-in types, therefore you cannot convert other types to a string. To C/C++, strings are simply arrays of characters, and that's also what Windows expects. What you need to do is to create a string to use with MessageBox. So, create a char array with enough space to hold the most characters you think your string will need + 1; the + 1 is necessary for a special character (value 0) called the terminator which is used to signal the end of the string--the terminator is usually handled automatically, so you don't need to handle it directly. Once you've done that, use the function sprintf which is defined in stdio.h--look it up in MSDN or just find a good C/C++ book.

This code should do what you need:

        
char string[100 + 1];
sprintf(string, "%u", inf.dwPageSize);
MessageBox(NULL, string, "Page Size", MB_OK | MB_ICONINFORMATION);


The "%u" bit is called a format string. The %u will be replaced with an unsigned integer, and inf.dwPageSize is the unsigned integer sprintf will place there. The result of this operation will be placed in string.

That's all there is to it.

Calling the MessageBox function with the two colons in front (::MessageBox) has nothing to do with MFC. It is a C++ thing. The two colons are called the scope resolution operator, and having nothing in front of those colons (as in "std::vector") indicates the the identifier (the thing after the colons) is part of the global namespace. The MessageBox function is always part of the global namespace, so the scope resolution operator is a redundancy, though I use them to remind myself of the fact and to make it obvious where I'm calling global functions--as opposed to functions belonging to a particular class, struct, or namespace.


Edited by - merlin9x9 on October 29, 2001 9:10:00 PM

Share this post


Link to post
Share on other sites