API Calls

  • aasim
  • Born
  • Born
  • aasim
  • Posts: 2
  • Loc: india

Post 3+ Months Ago

The problem I am facing is that I want to access some API calls from VC++ in Windows 2000 server
I have written the following code.
Code: [ Select ]
#include<stdio.h>
#include<windows.h>
void main()
{    
    int temp;
    LPSTR ipbuffer=" ";
    DWORD sizeBuff=30;
    temp=GetUserName(ipbuffer,&sizeBuff);
    printf("%s",ipbuffer);
}
  1. #include<stdio.h>
  2. #include<windows.h>
  3. void main()
  4. {    
  5.     int temp;
  6.     LPSTR ipbuffer=" ";
  7.     DWORD sizeBuff=30;
  8.     temp=GetUserName(ipbuffer,&sizeBuff);
  9.     printf("%s",ipbuffer);
  10. }


it gives me an error saying
The instruction at "x077f8c72c" referneced memory at "0x0041f01c". The memory could not be "written".
Click OK to termintate
Clicke Debug to debug the program


Could anyone please explain me why this error occurs.
How can i proceed. The same API works fine in VB.

if I change the
DWORD sizeBuff=6
then it returns an error code122

Please help
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Lieven
  • Beginner
  • Beginner
  • User avatar
  • Posts: 57
  • Loc: Belgium

Post 3+ Months Ago

in your API call, you should use &ipbuffer since otherwiste the function can't save anything in it...

I'm not 100% sure since I've never used those from within C++, but at first sight, it should do the trick
  • alvin
  • Born
  • Born
  • alvin
  • Posts: 2

Post 3+ Months Ago

You have to prepare an enough space of buffer, so that GetUserName() could store data into the buffer.


Rewrite like this:

......

int temp;
TCHAR ipbuffer[30];
DWORD sizeBuff=30;
temp=GetUserName(ipbuffer,&sizeBuff);

......



And in your original code, the ipbuffer was pointed into a constant string.
Because it is constant data, so some compiler would put it into a non-writable page. If any instruction try to alter the data in the page, such as the function wants to put the name into the memory address, it will cause the error.

- Alvin

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 123 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.