API call not working: insufficient buffer size on String parameter
I am trying to use the GetUserName function declared in advapi32.dll. I have got it so that it runs without raising an exception, but something is going wrong in the function call. Here is the code I am using:
When I run this testcase, the string is returned unchanged (whatever it's initial contents were, I have tried that); the buffersize variable is set to 6 and the returnedvalue is 0. The zero indicates some kind of error condition (it returns a positive number if everything went well); the buffersize being 6 indicates that it was in some sense trying to return a 5-character string making up a 6-character null-terminated string. The name I was logged in as was "vince", so this is what I was expecting.
Using another api call GetLastError, it turns out that the error was number 122, ERROR_INSUFFICIENT_BUFFER ("The data area passed to a system call is too small.")
I have tried padding my username string to more than six characters before submitting it to the GetUserName function, but this doesn't make any difference.
Is it just not possible to call a function where one of the parameters has to be a "pointer to a buffer"? Or am I doing something wrong here?
Re: API call not working: insufficient buffer size on String parameter
<BLOCKQUOTE><font size="1" face="Verdana, Arial, Helvetica">quote:</font><HR>I have tried padding my username string to more than six characters before submitting it to the GetUserName function, but this doesn't make any difference.<HR></BLOCKQUOTE>
You're halfway there. You need to set buffersize to the size of the username string that you have preloaded with spaces. Something like:
LONG buffersize = 1000
STRING username = Replicate(" ", buffersize)
Sr. Software Test Automation Engineer