By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,168 Members | 2,599 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,168 IT Pros & Developers. It's quick & easy.

Calling API

P: n/a
Hi
In VB I used to call a Windows API from VB to get the User's Name and
Workstation Name

I have just started to try and pick up some Dot Net by rewriting one of my
small Applications and I can't see how to invoke these calls in Dot Net.

Declare Function GetComputerName Lib "kernel32" Alias _
"GetComputerNameA" (ByVal lpBuffer As String, nSize As Long)
As Long

Declare Function GetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal pBuffer As String, nSize As
Long) As Long
Function dacUser() As String
If Len(gstrUserName) = 0 Then
Dim stUser As String
Dim lngCnt As Long, lngDL As Long
Dim sngPos As Single
lngCnt = 199
stUser = String(200, 0)
lngDL = GetUserName(stUser, lngCnt)
stUser = Left(stUser, lngCnt) & lngCnt
sngPos = InStr(1, stUser, Chr(0))
If sngPos > 0 Then
gstrUserName = Left(stUser, sngPos - 1)
End If
End If
dacUser = gstrUserName
End Function

I tried replacing String with StrDup but can't get it to work

Can someone help?
Nov 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hi,

No need for an api call anymore. Take a look at the environment and
systeminformation classes.

Trace.WriteLine(Environment.UserName)

Trace.WriteLine(Environment.MachineName)

Trace.WriteLine(SystemInformation.UserName)

Trace.WriteLine(SystemInformation.ComputerName)

Ken

-------------------

"Doug Bell" <dug@bigpond> wrote in message
news:uj**************@TK2MSFTNGP09.phx.gbl...
Hi
In VB I used to call a Windows API from VB to get the User's Name and
Workstation Name

I have just started to try and pick up some Dot Net by rewriting one of my
small Applications and I can't see how to invoke these calls in Dot Net.

Declare Function GetComputerName Lib "kernel32" Alias _
"GetComputerNameA" (ByVal lpBuffer As String, nSize As Long)
As Long

Declare Function GetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal pBuffer As String, nSize As
Long) As Long
Function dacUser() As String
If Len(gstrUserName) = 0 Then
Dim stUser As String
Dim lngCnt As Long, lngDL As Long
Dim sngPos As Single
lngCnt = 199
stUser = String(200, 0)
lngDL = GetUserName(stUser, lngCnt)
stUser = Left(stUser, lngCnt) & lngCnt
sngPos = InStr(1, stUser, Chr(0))
If sngPos > 0 Then
gstrUserName = Left(stUser, sngPos - 1)
End If
End If
dacUser = gstrUserName
End Function

I tried replacing String with StrDup but can't get it to work

Can someone help?

Nov 20 '05 #2

P: n/a
Thanks Ken,

That is too easy!

What about a File Open/ File Save Box Dialogue Box?

I used always call that as an API, is there a Namespace solution for that?

Also can you recommend best way to really get up to speed with all these
changes?

Doug
"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:u5*************@TK2MSFTNGP10.phx.gbl...
Hi,

No need for an api call anymore. Take a look at the environment and systeminformation classes.

Trace.WriteLine(Environment.UserName)

Trace.WriteLine(Environment.MachineName)

Trace.WriteLine(SystemInformation.UserName)

Trace.WriteLine(SystemInformation.ComputerName)

Ken

-------------------

"Doug Bell" <dug@bigpond> wrote in message
news:uj**************@TK2MSFTNGP09.phx.gbl...
Hi
In VB I used to call a Windows API from VB to get the User's Name and
Workstation Name

I have just started to try and pick up some Dot Net by rewriting one of my
small Applications and I can't see how to invoke these calls in Dot Net.

Declare Function GetComputerName Lib "kernel32" Alias _
"GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Declare Function GetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal pBuffer As String, nSize As
Long) As Long
Function dacUser() As String
If Len(gstrUserName) = 0 Then
Dim stUser As String
Dim lngCnt As Long, lngDL As Long
Dim sngPos As Single
lngCnt = 199
stUser = String(200, 0)
lngDL = GetUserName(stUser, lngCnt)
stUser = Left(stUser, lngCnt) & lngCnt
sngPos = InStr(1, stUser, Chr(0))
If sngPos > 0 Then
gstrUserName = Left(stUser, sngPos - 1)
End If
End If
dacUser = gstrUserName
End Function

I tried replacing String with StrDup but can't get it to work

Can someone help?

Nov 20 '05 #3

P: n/a
Hi David,

As Ken did, are we trying to give the right dotNet program parts in this
newsgroup, which will have all the benefits of the framework.

What is the sense when there are good framework classes to show the API
code, when this is done people are easy converting API's which is not that
hell of a job when you know the tricks, however can have mabye give problems
in short or long future?

I am curious about your thought about this?

Cor
Nov 20 '05 #4

P: n/a
* "Doug Bell" <dug@bigpond> scripsit:
What about a File Open/ File Save Box Dialogue Box?

I used always call that as an API, is there a Namespace solution for that?


'System.Windows.Forms.OpenFileDialog'/'System.Windows.Forms.SaveFileDialog'.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
<URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.