×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Using the GetSystemInfo API call

Using the GetSystemInfo API call

Using the GetSystemInfo API call

(OP)
HI,

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?

#include <windows.h>

void main()
{
LPSYSTEM_INFO temp;
GetSystemInfo(temp );
}

That's it. It shoudl be a simple call but I am missing something here.

Cory

RE: Using the GetSystemInfo API call

just by looking at it, temp should now have parameters, or groups to check like lets say for example

temp.harddrivesize
etc, LPSYSTEM_INFO i assumeis some kind of structure, so check the MSDN on that type.

Karl
kb244@kb244.8m.com

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)

RE: Using the GetSystemInfo API call

(OP)
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:

void main ()
{
SYSTEM_INFO *temp;

GetSystemInfo(temp );
}

But the same thing happens

RE: Using the GetSystemInfo API call

Parameters
lpSystemInfo
[out] Pointer to a SYSTEM_INFO structure that receives the information


ok , so we know you have to use *temp now, since it returns a pointer to that address where the information is.


this is the structyre of System_Info


typedef struct _SYSTEM_INFO {
  union {
    DWORD  dwOemId;
    struct {
      WORD wProcessorArchitecture;
      WORD wReserved;
    };
  };
  DWORD  dwPageSize;
  LPVOID lpMinimumApplicationAddress;
  LPVOID lpMaximumApplicationAddress;
  DWORD_PTR dwActiveProcessorMask;
  DWORD dwNumberOfProcessors;
  DWORD dwProcessorType;
  DWORD dwAllocationGranularity;
  WORD wProcessorLevel;
  WORD wProcessorRevision;
} SYSTEM_INFO;



it would seem to get access to one of these, it would be somehting like.

temp->dwProcessorType
details can be seen at http://msdn.microsoft.com/library/default.asp?URL=/library/psdk/sysmgmt/sysinfo_5r76.htm

It would also seem, that most of thisinformation may not be the one you are looking for.

also this might help

http://msdn.microsoft.com/library/default.asp?URL=/library/psdk/psdkref/catfunc_26m1.htm
It list all the WIN32 API functions by catagory.

Karl
kb244@kb244.8m.com

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)

RE: Using the GetSystemInfo API call

Dear SmileeTiger,

Pointers must be allocated before using them! C/C++ 101

LPSYSTEM_INFO pSI = (LPSYSTEM_INFO)malloc(sizeof(SYSTEM_INFO));

but in your case what you really want to do is this

SYSTEM_INOF si;
memset( &si, 0, sizeof(si));
GetSystemInfo( &si);

Hope this helps
-pete

RE: Using the GetSystemInfo API call

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 :})

Karl
kb244@kb244.8m.com

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)

RE: Using the GetSystemInfo API call

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

int *myint;
myint = new int;
(just as easily int *myint = new int)

but in this case, you create a pointer

SYSTEM_INFO *temp;
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.

Karl
kb244@kb244.8m.com

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)

RE: Using the GetSystemInfo API call

(OP)
OK I have it now. I did this:

SYSTEM_INFO temp;

GetSystemInfo(&temp );
cout << temp.dwProcessorType;
cout << endl << temp.dwActiveProcessorMask;

Thanks for your help

Cory

RE: Using the GetSystemInfo API call

or that hehe.(& address of)

Karl
kb244@kb244.8m.com

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)

RE: Using the GetSystemInfo API call

Dear Karl,

I don't want to start a debate about this but here is the SDK for GetSystemInfo()

<-- begin msdn.microsoft.com excerpt

VOID GetSystemInfo( LPSYSTEM_INFO lpSystemInfo);

Parameters
lpSystemInfo

Pointer to a SYSTEM_INFO structure to be filled in by this function.

Return Values
None.

 END -->

Now where does it allocate and return a pointer for you?

-pete

RE: Using the GetSystemInfo API call

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?

does that answer your question?

Karl
kb244@kb244.8m.com

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)

RE: Using the GetSystemInfo API call

and if you notice the post I made above from the MSDN , the [[out]] signifies that is the output, not the input.

Karl
kb244@kb244.8m.com

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)

RE: Using the GetSystemInfo API call

Dear karl,

If you ever get that to work, more power to you.

In winbase.h LPSYSTEM_INFO is defined as a pointer to SYSTEM_INFO.

So how is the calling stack going to get access to the allocated memory when the parameter is defined as LPSYSTEM_INFO as in:

VOID GetSystemInfo( LPSYSTEM_INFO lpSystemInfo);

For the calling stack to obtain the actual pointer allocated inside 'GetSystemInfo' this would require one of these:

LPSYSTEM_INFO GetSystemInfo()
VOID GetSystemInfo( *LPSYSTEM_INFO ppSystemInfo)
VOID GetSystemInfo( LPSYSTEM_INFO& pSystemInfo)

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.

-pete

RE: Using the GetSystemInfo API call

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.

Karl
kb244@kb244.8m.com

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)

RE: Using the GetSystemInfo API call

Dear Karl,

C/C++ function parameters pass by value.

void createBuf(void* buf){

  buf = (void*)new char[10]; // assigns the address to 'buf'
  cout << buf << endl;  // displays new address allocated and stored in buf <local stack>
}

void main(){
  void* buf;  // not initailized so value is unknown
  cout << buf << endl;  //displays invalid address stored in buf
  createBuf( buf);  // unknown value passed into function
  cout << buf << endl;  //address is unchanged
}

and you have a memory leak!

-pete

RE: Using the GetSystemInfo API call

to continue...

Karl stated, "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."

The function can't "point the pointer" to the memory unless it has a 'reference' or a 'pointer' to the pointer. This is what I displayed in my earlier post:

VOID GetSystemInfo( *LPSYSTEM_INFO ppSystemInfo)
VOID GetSystemInfo( LPSYSTEM_INFO& pSystemInfo)

now you can 'point' the pointer to the memory, by example

void createBufByPtr( void** buf){
  *buf = (void*)new char[10];
  cout << *buf << endl;
}

void createBufByRef( void*& buf){
  buf = (void*)new char[10];
  cout << buf << endl;
}

void main(){
  void* buf;  // same uninitialized garbage
  cout << buf << endl;
  createBufByPtr( &buf);  // now we have the new address
  cout << buf << endl;    // print it out to verify

  createBufByRef( buf);  // another new address
  cout << buf << endl;   // print to verify
}

otherwise you can't because you only have the value of the original pointer, not the pointer itself for modification.

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.

-pete

RE: Using the GetSystemInfo API call

1) I thought you wernt going to debate this
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.

int *temp

[protype looks like : something(int *temp)]

something(temp)
{
temp = new int;
*temp = 3
}

cout<<*temp;
[output shows 3]

delete temp; //memory freed.

Karl
kb244@kb244.8m.com

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)

RE: Using the GetSystemInfo API call

Dear Karl,

1) My quote: "I don't want to start a debate about this "

As you can see i did not say I would not debate it, I stated I didn't want to.

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.

-pete

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close