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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using the GetSystemInfo API call

Status
Not open for further replies.

SmileeTiger

Programmer
Mar 13, 2000
200
US
HI,<br><br>I am trying to use the GetSystemInfo API call. When I run the following code I get a 'memory cannot be written' error. I know it is something simple but can anyone see what is wrong with this code?<br><br>#include &lt;windows.h&gt;<br><br>void main()<br>{<br>LPSYSTEM_INFO temp;<br>GetSystemInfo(temp );<br>}<br><br>That's it. It shoudl be a simple call but I am missing something here.<br><br>Cory
 
just by looking at it, temp should now have parameters, or groups to check like lets say for example<br><br>temp.harddrivesize<br>etc, LPSYSTEM_INFO i assumeis some kind of structure, so check the MSDN on that type. <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in , or have messed with : VC++, Borland C++ Builder, VJ++6(starting),VB-Dos, VB1 thru VB6, Delphi 3 pro, Borland C++ 3(DOS), Borland C++ 4.5, HTML, ASP(somewhat), QBasic(least i didnt start with COBOL)
 
I agree that shoudl be exactly what it would have but I am getting a memory write error when I make the call for some reason or other. I can also use the following code:<br><br>void main ()<br>{<br>SYSTEM_INFO *temp;<br><br>GetSystemInfo(temp );<br>}<br><br>But the same thing happens
 
<FONT FACE=monospace>Parameters<br>lpSystemInfo <br>[out] Pointer to a SYSTEM_INFO structure that receives the information</font><br><br>ok , so we know you have to use *temp now, since it returns a pointer to that address where the information is.<br><br><br>this is the structyre of System_Info<br><br><FONT FACE=monospace><br>typedef struct _SYSTEM_INFO { <br>&nbsp;&nbsp;union { <br>&nbsp;&nbsp;&nbsp;&nbsp;DWORD&nbsp;&nbsp;dwOemId; <br>&nbsp;&nbsp;&nbsp;&nbsp;struct { <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WORD wProcessorArchitecture; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WORD wReserved; <br>&nbsp;&nbsp;&nbsp;&nbsp;}; <br>&nbsp;&nbsp;}; <br>&nbsp;&nbsp;DWORD&nbsp;&nbsp;dwPageSize; <br>&nbsp;&nbsp;LPVOID lpMinimumApplicationAddress; <br>&nbsp;&nbsp;LPVOID lpMaximumApplicationAddress; <br>&nbsp;&nbsp;DWORD_PTR dwActiveProcessorMask; <br>&nbsp;&nbsp;DWORD dwNumberOfProcessors; <br>&nbsp;&nbsp;DWORD dwProcessorType; <br>&nbsp;&nbsp;DWORD dwAllocationGranularity; <br>&nbsp;&nbsp;WORD wProcessorLevel; <br>&nbsp;&nbsp;WORD wProcessorRevision; <br>} SYSTEM_INFO; <br></font><br><br><br>it would seem to get access to one of these, it would be somehting like.<br><br>temp-&gt;dwProcessorType<br><i>details can be seen at <A HREF=" TARGET="_new"> would also seem, that most of thisinformation may not be the one you are looking for.<br><br>also this might help<br><br><i><A HREF=" TARGET="_new"> list all the WIN32 API functions by catagory.<br><br> <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in , or have messed with : VC++, Borland C++ Builder, VJ++6(starting),VB-Dos, VB1 thru VB6, Delphi 3 pro, Borland C++ 3(DOS), Borland C++ 4.5, HTML, ASP(somewhat), QBasic(least i didnt start with COBOL)
 
Dear SmileeTiger,<br><br>Pointers must be allocated before using them! C/C++ 101<br><br>LPSYSTEM_INFO pSI = (LPSYSTEM_INFO)malloc(sizeof(SYSTEM_INFO));<br><br>but in your case what you really want to do is this<br><br>SYSTEM_INOF si;<br>memset( &si, 0, sizeof(si));<br>GetSystemInfo( &si);<br><br>Hope this helps<br>-pete
 
you can do it that way if you want to allocate it via C, but in C++ you use the new keyword, but this is irrelavent, since the GetSystemInformation returns the address to a pointer, therefore creating a new instance of System_Info is useless only to throw away that same instance when you reassign it to another address (C/C++ 101 :}) <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in , or have messed with : VC++, Borland C++ Builder, VJ++6(starting),VB-Dos, VB1 thru VB6, Delphi 3 pro, Borland C++ 3(DOS), Borland C++ 4.5, HTML, ASP(somewhat), QBasic(least i didnt start with COBOL)
 
to clear up my last post, a Pointer does nothing more than point to an address in memory, naturally to use a new pointer, you would have to create a location in memory, something like this<br><br>int *myint;<br>myint = new int;<br>(just as easily int *myint = new int)<br><br>but in this case, you create a pointer<br><br>SYSTEM_INFO *temp; <br>right now the temp points to nothing and would naturally generate an error if you tried to use it, or would return a junk value if you tried to retrive it's address. but since the function GetSystemInfo(...) returns a Pointer address into temp, you do not need to create a new instance of System_info, instead the function changes, or assigns a new address in memory pointing to the newly created information, which makes pete's post helpful(for C's method), but irrelavent to this situation, and does not solve the current problem. <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in , or have messed with : VC++, Borland C++ Builder, VJ++6(starting),VB-Dos, VB1 thru VB6, Delphi 3 pro, Borland C++ 3(DOS), Borland C++ 4.5, HTML, ASP(somewhat), QBasic(least i didnt start with COBOL)
 
OK I have it now. I did this:<br><br>SYSTEM_INFO temp;<br><br>GetSystemInfo(&temp );<br>cout &lt;&lt; temp.dwProcessorType;<br>cout &lt;&lt; endl &lt;&lt; temp.dwActiveProcessorMask;<br><br>Thanks for your help<br><br>Cory
 
or that hehe.(& address of) <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in , or have messed with : VC++, Borland C++ Builder, VJ++6(starting),VB-Dos, VB1 thru VB6, Delphi 3 pro, Borland C++ 3(DOS), Borland C++ 4.5, HTML, ASP(somewhat), QBasic(least i didnt start with COBOL)
 
Dear Karl,<br><br>I don't want to start a debate about this but here is the SDK for GetSystemInfo()<br><br>&lt;-- begin msdn.microsoft.com excerpt<br><br>VOID GetSystemInfo( LPSYSTEM_INFO lpSystemInfo); <br><br>Parameters<br>lpSystemInfo<br><br>Pointer to a SYSTEM_INFO structure to be filled in by this function. <br><br>Return Values<br>None.<br><br>&nbsp;END --&gt;<br><br>Now where does it allocate and return a pointer for you?<br><br>-pete<br>
 
the function assigns the Function address to the lpSystemInfo, the function does not nessarily need to return anything to assign, by passing a point through a function, the function can then assign whatever address it wants to the pointer. Like why do you think theres such a thing as pass by reference, and pass by value?<br><br>does that answer your question? <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in , or have messed with : VC++, Borland C++ Builder, VJ++6(starting),VB-Dos, VB1 thru VB6, Delphi 3 pro, Borland C++ 3(DOS), Borland C++ 4.5, HTML,Visual InterDev 6, ASP(WebProgramming), QBasic(least i didnt start with COBOL)
 
and if you notice the post I made above from the MSDN , the [[out]] signifies that is the output, not the input. <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in , or have messed with : VC++, Borland C++ Builder, VJ++6(starting),VB-Dos, VB1 thru VB6, Delphi 3 pro, Borland C++ 3(DOS), Borland C++ 4.5, HTML,Visual InterDev 6, ASP(WebProgramming), QBasic(least i didnt start with COBOL)
 
Dear karl,<br><br>If you ever get that to work, more power to you.<br><br>In winbase.h LPSYSTEM_INFO is defined as a pointer to SYSTEM_INFO.<br><br>So how is the calling stack going to get access to the allocated memory when the parameter is defined as LPSYSTEM_INFO as in:<br><br>VOID GetSystemInfo( LPSYSTEM_INFO lpSystemInfo); <br><br>For the calling stack to obtain the actual pointer allocated inside 'GetSystemInfo' this would require one of these:<br><br>LPSYSTEM_INFO GetSystemInfo()<br>VOID GetSystemInfo( *LPSYSTEM_INFO ppSystemInfo)<br>VOID GetSystemInfo( LPSYSTEM_INFO& pSystemInfo)<br><br>But it's not so therefore you must pass a pointer to an allocated block of memory to the function which is why Cory's code was breaking.<br><br>-pete<br>
 
the pointer can be just a pointer, you pass it over, the function then itself allocates and fills in the information in memory, then points your pointer to the memory. <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in , or have messed with : VC++, Borland C++ Builder, VJ++6(starting),VB-Dos, VB1 thru VB6, Delphi 3 pro, Borland C++ 3(DOS), Borland C++ 4.5, HTML,Visual InterDev 6, ASP(WebProgramming), QBasic(least i didnt start with COBOL)
 
Dear Karl,<br><br>C/C++ function parameters pass by value.<br><br>void createBuf(void* buf){<br><br>&nbsp;&nbsp;buf = (void*)new char[10]; // assigns the address to 'buf'<br>&nbsp;&nbsp;cout &lt;&lt; buf &lt;&lt; endl;&nbsp;&nbsp;// displays new address allocated and stored in buf &lt;local stack&gt;<br>}<br><br>void main(){<br>&nbsp;&nbsp;void* buf;&nbsp;&nbsp;// not initailized so value is unknown<br>&nbsp;&nbsp;cout &lt;&lt; buf &lt;&lt; endl;&nbsp;&nbsp;//displays invalid address stored in buf<br>&nbsp;&nbsp;createBuf( buf);&nbsp;&nbsp;// unknown value passed into function<br>&nbsp;&nbsp;cout &lt;&lt; buf &lt;&lt; endl;&nbsp;&nbsp;//address is unchanged<br>}<br><br>and you have a memory leak!<br><br>-pete<br>
 
to continue...<br><br>Karl stated, &quot;the pointer can be just a pointer, you pass it over, the function then itself allocates and fills in the information in memory, then points your pointer to the memory.&quot;<br><br>The function can't &quot;point the pointer&quot; to the memory unless it has a 'reference' or a 'pointer' to the pointer. This is what I displayed in my earlier post:<br><br>VOID GetSystemInfo( *LPSYSTEM_INFO ppSystemInfo)<br>VOID GetSystemInfo( LPSYSTEM_INFO& pSystemInfo)<br><br>now you can 'point' the pointer to the memory, by example<br><br>void createBufByPtr( void** buf){<br>&nbsp;&nbsp;*buf = (void*)new char[10];<br>&nbsp;&nbsp;cout &lt;&lt; *buf &lt;&lt; endl;<br>}<br><br>void createBufByRef( void*& buf){<br>&nbsp;&nbsp;buf = (void*)new char[10];<br>&nbsp;&nbsp;cout &lt;&lt; buf &lt;&lt; endl;<br>}<br><br>void main(){<br>&nbsp;&nbsp;void* buf;&nbsp;&nbsp;// same uninitialized garbage<br>&nbsp;&nbsp;cout &lt;&lt; buf &lt;&lt; endl;<br>&nbsp;&nbsp;createBufByPtr( &buf);&nbsp;&nbsp;// now we have the new address<br>&nbsp;&nbsp;cout &lt;&lt; buf &lt;&lt; endl;&nbsp;&nbsp;&nbsp;&nbsp;// print it out to verify<br><br>&nbsp;&nbsp;createBufByRef( buf);&nbsp;&nbsp;// another new address<br>&nbsp;&nbsp;cout &lt;&lt; buf &lt;&lt; endl;&nbsp;&nbsp;&nbsp;// print to verify<br>}<br><br>otherwise you can't because you only have the value of the original pointer, not the pointer itself for modification.<br><br>If you don't believe me you can read about it in basic C or C++ books or you can even run some simple code as I have posted, to verify this behavior.<br><br>-pete
 
1) I thought you wernt going to debate this<br>2) you can always give a pointer a memory address to be allocated, and this will not cause a memory leak as long as you use the delete function to deallocate it when you are done.<br><br>int *temp<br><br>[protype looks like : something(int *temp)]<br><br>something(temp)<br>{<br>temp = new int;<br>*temp = 3<br>}<br><br>cout&lt;&lt;*temp;<br>[output shows 3]<br><br>delete temp; //memory freed. <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in , or have messed with : VC++, Borland C++ Builder, VJ++6(starting),VB-Dos, VB1 thru VB6, Delphi 3 pro, Borland C++ 3(DOS), Borland C++ 4.5, HTML,Visual InterDev 6, ASP(WebProgramming), QBasic(least i didnt start with COBOL)
 
Dear Karl,<br><br>1) My quote: &quot;I don't want to start a debate about this &quot;<br><br>As you can see i did not say I would not debate it, I stated I didn't want to.<br><br>2) Your code does not produce the result you state. I don't know what more I can say. Unless you offer some evidence to support your position, I am done.<br><br>-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top