How to use WMI with Excel VBA - Engineer of NiagaraFramework

Tridium, NiagaraFramework, SCADA, HMI, PLC, Automation, SmartFAM etc.. About controls.
나이아가라 프레임워크 QnA : neverlikekami@gmail.com

2019년 4월 17일 수요일

How to use WMI with Excel VBA

개요
엑셀의 VBA로 프로그램을 작성할때 WMI를 이용하여 원하는 기능을 도출할 수 있다.
인터프리터 언어의 한계상.. 완벽한 보안은 될 수 없지만 본 내용을 응용하여 심플한 License 코드를 작성할 수 있다.



<VBA : Visual Basic for Applications>



Get Serial Number of Mainboard

메인보드의 시리얼 번호를 추출하는 소스코드.
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Private Function SystemSerialNumber() As String
Dim mother_boards As Variant
Dim board As Variant
Dim wmi As Variant
Dim serial_numbers As String

' Get the Windows Management Instrumentation object.
Set wmi = GetObject("WinMgmts:")

' Get the "base boards" (mother boards).
Set mother_boards = wmi.InstancesOf("Win32_BaseBoard")
For Each board In mother_boards
serial_numbers = serial_numbers & ", " & _
board.SerialNumber
Next board
If Len(serial_numbers) > 0 Then serial_numbers = _
Mid$(serial_numbers, 3)

SystemSerialNumber = serial_numbers
End Function


Get CPU ID

CPU의 고유 ID 추출하는 소스코드.
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Private Function CpuId() As String
Dim computer As String
Dim wmi As Variant
Dim processors As Variant
Dim cpu As Variant
Dim cpu_ids As String

computer = "."
Set wmi = GetObject("winmgmts:" & _
"{impersonationLevel=impersonate}!\\" & _
computer & "\root\cimv2")
Set processors = wmi.ExecQuery("Select * from " & _
"Win32_Processor")

For Each cpu In processors
cpu_ids = cpu_ids & ", " & cpu.ProcessorId
Next cpu
If Len(cpu_ids) > 0 Then cpu_ids = Mid$(cpu_ids, 3)

CpuId = cpu_ids
End Function



Get MAC Address of LAN Card

LanCard의 MAC Address 추출하는 소스코드.
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Private Function getMacAddress() As String
Try
Dim adapters As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()
Dim adapter As NetworkInterface
Dim myMac As String = String.Empty

For Each adapter In adapters
Select Case adapter.NetworkInterfaceType
'Exclude Tunnels, Loopbacks and PPP
Case NetworkInterfaceType.Tunnel, NetworkInterfaceType.Loopback, NetworkInterfaceType.Ppp
Case Else
If Not adapter.GetPhysicalAddress.ToString = String.Empty And Not adapter.GetPhysicalAddress.ToString = "00000000000000E0" Then
myMac = adapter.GetPhysicalAddress.ToString
Exit For ' Got a mac so exit for
End If

End Select
Next adapter

Return myMac
Catch ex As Exception
Return String.Empty
End Try
End Function


위의 코드들로 얻은 결과값을 조합하여 특정 키를 생성해낼 수 있다.



댓글 없음:

댓글 쓰기

Post list