473,394 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Using API function in VB.NET

I'm trying to use the GetUserName API function in the advapi32.dll library.
I declare my function as follows

Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA"
(ByVal lpBuffer As String, ByVal nsize As Long) As Lon

But when I call the function like this
lngRetVal = GetUserName(strName, lngSize

I get the following error
'Object reference not set to an instance of an object.

Does anyone know how I can fix this

Thanks
Parvee

Nov 20 '05 #1
3 9796
There is no need to use this API use the System.Environment.UserName object
from the framework...
"Parveen" <an*******@discussions.microsoft.com> wrote in message
news:4B**********************************@microsof t.com...
I'm trying to use the GetUserName API function in the advapi32.dll library. I declare my function as follows:

Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA"
(ByVal lpBuffer As String, ByVal nsize As Long) As Long

But when I call the function like this:
lngRetVal = GetUserName(strName, lngSize)

I get the following error:
'Object reference not set to an instance of an object.'

Does anyone know how I can fix this?

Thanks,
Parveen

Nov 20 '05 #2
In article <4B**********************************@microsoft.co m>, Parveen wrote:
I'm trying to use the GetUserName API function in the advapi32.dll library.
I declare my function as follows:

Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA"
(ByVal lpBuffer As String, ByVal nsize As Long) As Long

But when I call the function like this:
lngRetVal = GetUserName(strName, lngSize)

I get the following error:
'Object reference not set to an instance of an object.'

Does anyone know how I can fix this?

Thanks,
Parveen

As mentioned, you don't need this API call since this functionality is
implemented in the framework - System.Environment.UserName.

But, I am going to mention some problems with your above declare anyway
:)

1. Long in VB.NET is 64-Bit. Integer is 32-Bit. So, you'll want to
replace your Longs with Integers.

2. Drop the Alias. It is much more effiecient to declare the function
as Auto and let the runtime call the appropriate function. This way,
when running on NT based systems you avoid a lot of unnecessary string
conversions.

3. Don't use String for writeable buffers. For buffers that are going
to be filled with by the API call you should use
System.Text.StringBuilder. While it will work in VB.NET to use string
(the marshaller is smart enough to deal with it), it is really for
backwards compatability. Using the String type will not work in C#.
This is because the String data type is meant to be a immutable buffer.
Further, because of the extra work on the part of the marshaller, performance
will suffer. Of course, performance may not be an issue in cases
where the function is only called one time - but it still violates the
intent of the string data type.

4. The reason your getting your error: the API call expects the nSize
parameter to be passed ByRef :)

So, given the above - your declare:
Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA"
(ByVal lpBuffer As String, ByVal nsize As Long) As Long

Should really be:

Declare Auto Function GetUserName Lib "advapi32.dll" _
(ByVal lpBuffer As System.Text.StringBuilder, _
ByRef nSize As Integer) As Boolean
Then you would call it like this:

Dim buffer As New System.Text.StringBuilder(256)
Dim nSize As Integer = buffer.Capacity

If GetUserName(buffer, nSize) Then
Return buffer.ToString()
Else
Throw New Win32Exception(Marshal.GetLastWin32Error())
End If

HTH
--
Tom Shelton
MVP [Visual Basic]
Nov 20 '05 #3
That was very helpful!! Thank you both for the advice!! :)
Nov 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
1
by: Mike | last post by:
When trying to compile (using Visual Web Developer 2005 Express Beta; frameworkv2.0.50215 ) the source code below I get errors (listed below due to the use of ICallBackEventHandler. Ultimately I...
3
by: Random Person | last post by:
Does anyone know how to use VBA to relink tables between two MS Access databases? We have two databases, one with VBA code and the other with data tables. The tables are referenced by linked...
11
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on...
1
by: Daveyk0 | last post by:
Hello there, I have a front end database that I have recently made very many changes to to allow off-line use. I keep copies of the databases on my hard drive and link to them rather than the...
3
by: Rob | last post by:
Hi all, I am having trouble converting the code below (found on http://vbnet.mvps.org/index.html?code/core/sendmessage.htm) into a format that will work using vb .NET. Can anyone have a look...
9
by: Ron | last post by:
Hello, Is it required to use Imports System.Runtime.InteropServices to run C++ API code? I ask because I thought I read somewhere that this was required. If it is not required would it...
14
by: B Williams | last post by:
I am stuck on an assignment that uses classes and functions. I am receiving numerous errors when I try to run a test program to see if I wrote it correctly. Can someone please point me in the right...
2
by: Tugrul HELVACI | last post by:
I'm using Delphi 2006 and I have a class defination like this: TPerson = class fPersonName : String; fPersonSurName : String; fPersonAge : Integer; published property PersonName : String...
6
by: =?Utf-8?B?U2hhd24gU2VzbmE=?= | last post by:
Greetings! I was researching AJAX to provide a solution to displaying status messages while a long process executed. I found several examples online and was able to use their code to get a quick...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.