FengK 发表于 2009-4-13 17:19:29

VBA 等效于 (vlax-product-key)?

有没有办法使用VBA获取注册表项?谢谢。
**** Hidden Message *****

Jeff_M 发表于 2009-4-13 18:12:41

给你,凯莉。
' Returns the AutoCAD Product Key
''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Function VBA_Acad_Product_Key() As String
On Error Resume Next
' Code suggested by Tony Zanzillo & Autodesk as posted by Laurie Comerford.
' Note that the code posted in 2005 does not appear to work in the 2009 products, edited to work
' in 2009 and also to output similar results as the Lisp function (vla-product-key) by Jeff Mishler
Dim oReg As Object
Dim sVer1 As String
Dim sVer2 As String
Dim sver3 As String
Dim sProduct As String
Set oReg = CreateObject("WScript.Shell")
sVer1 = oReg.RegRead("HKEY_CURRENT_USER\SOFTWARE\Autodesk\Autocad\curver")
sVer2 = oReg.RegRead("HKEY_CURRENT_USER\SOFTWARE\Autodesk\Autocad\" & sVer1 & "\curver")
'sver3 = "HKEY_LOCAL_MACHINE\SOFTWARE\Autodesk\Autocad\" & sVer1 & "\" & sVer2 & "\ProductName"
' ' As well as product name, there are a whole series of other aspects of AutoCAD which can be recovered with this code
'' In 2009 the 2 CurVer keys are found under Current_User NOT Local_Machine as was posted. However, the Key "ProductName" IS in HKLM.
''sProduct = oReg.RegRead(sver3)
''Edited to return the key instead of the Product Name.
sver3 = "SOFTWARE\Autodesk\Autocad\" & sVer1 & "\" & sVer2
sProduct = sver3
Set oReg = Nothing
If Err0 Or sProduct = "" Then
Err.Clear
GoTo CantFindAutoCAD
End If
VBA_Acad_Product_Key = sProduct
Exit Function
CantFindAutoCAD:
sProduct = "Unable to find AutoCAD in the computer registry." & vbCrLf
MsgBox sProduct, vbCritical
End Function ' VBA_Acad_Product_Key

FengK 发表于 2009-4-13 18:30:57

谢谢杰夫!我不知道这两个曲线。

Jeff_M 发表于 2009-4-13 18:39:14

等一下,别这么快.....我刚刚做了一点测试,这得到了最新的版本开始。换句话说,启动一个2009年的会话,它会返回正确的版本。在2009年运行时开始2008年的一个会话,曲线返回2008年,直到2009年的另一个会话开始。在会话之间切换不会更新曲线,关闭2008会话也不会。(vlax-product-key)能够识别这种变化。
如果这会导致问题,也许有人有更好的主意。

Jeff_M 发表于 2009-4-13 19:58:12

我刚刚做了一个快速的ATL/ARX项目...
也许如果你知道如何在VBA使用COM,你可能想试试这个ARX(如果它有用,我可以把它改成Release并再次上传)。
由于我对VBA了解不多,这里是用visual lisp的解释,加载注册COM服务器后:
我只是添加了对acrxProductKey()的访问;功能-它与visual lisp中的vlax-product-key相同

Matt__W 发表于 2009-4-13 20:21:02

谢谢路易斯。这很有效:
Function ProductKey() As String
Dim oCom As Object
Dim progID As String
progID = "TheSwampCOM.CTheSwamp.1"
Application.LoadArx "TheSwampCOM.arx"
Set oCom = Application.GetInterfaceObject(progID)
Dim path As String
oCom.ProductKey path
Application.UnloadArx "TheSwampCOM.arx"
ProductKey = path
End Function
Sub arxtest()
Debug.Print ProductKey
End Sub

Jeff_M 发表于 2009-4-13 20:27:02


那很好,杰夫。

Jeff_M 发表于 2009-4-13 20:38:49

它不需要加载arx了——试一试,看看是否可行。

Jeff_M 发表于 2009-4-13 22:15:57

如果您想避免COM(如果C#不提供此函数,并且您还希望它使用此语言),您可以使用以下内容:

static extern string ProductKey();

public void regPathVerCurrentlyLoaded()
{
    string path = ProductKey();
    Document doc = acadApp.DocumentManager.MdiActiveDocument;
    Editor ed = doc.Editor;
    ed.WriteMessage(path);
}

HTH

Jeff_M 发表于 2009-4-14 08:59:32

另一个....
页: [1] 2
查看完整版本: VBA 等效于 (vlax-product-key)?