Calling API

D

Doug Bell

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?
 
K

Ken Tucker [MVP]

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

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

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?
 
D

Doug Bell

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
 
C

Cor Ligthert

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
 
H

Herfried K. Wagner [MVP]

* "Doug Bell said:
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'.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top