你是直接在VB中加載登錄窗口進(jìn)行登錄,還是在瀏覽器中做插件進(jìn)行登錄?
創(chuàng)新互聯(lián)建站基于成都重慶香港及美國等地區(qū)分布式IDC機(jī)房數(shù)據(jù)中心構(gòu)建的電信大帶寬,聯(lián)通大帶寬,移動大帶寬,多線BGP大帶寬租用,是為眾多客戶提供專業(yè)服務(wù)器托管報(bào)價,主機(jī)托管價格性價比高,為金融證券行業(yè)內(nèi)蒙古服務(wù)器托管,ai人工智能服務(wù)器托管提供bgp線路100M獨(dú)享,G口帶寬及機(jī)柜租用的專業(yè)成都idc公司。
在VB中進(jìn)行登錄:
'函數(shù)功能:?????登陸網(wǎng)頁
'輸入?yún)?shù):?????WB:webbrower控件,sURL:網(wǎng)址,OverTime:超時時間,UserName:用戶名,UserPWD:用戶密碼,InPutName:用戶名輸入框,InPutPWD:密碼輸入框,LoginFrom:登陸窗體,BtnName:登陸按鈕名字
'輸出參數(shù):?????無
'返回值:???????True:成功,false:失敗
'******************************************************************
Public?Function?LoginUrl(WB?As?WebBrowser,?sURL?As?String,?OverTime?As?Long,?UserName?As?String,?UserPWD?As?String,?InputName?As?String,?InputPWD?As?String,?LoginFrom?As?String,?BtnName?As?String)?As?Boolean
On?Error?GoTo?errorHandle
bHtmlFlag?=?False
Set?sDocument?=?WB.Document
sDocument.getElementById(InputName).Value?=?UserName
sDocument.getElementById(InputPWD).Value?=?UserPWD
If?BtnName?=?""?Then
sDocument.Forms(LoginFrom).submit
Else
sDocument.getElementById(BtnName).Click
End?If
If?HtmlOverTime(OverTime,?sURL)?=?1?Then????????'判斷超時
LoginUrl?=?False
InsertLog?"登陸網(wǎng)址:"??sURL??"失??!",?1
Exit?Function
End?If
LoginUrl?=?True
Exit?Function
errorHandle:
InsertLog?"錯誤號:"??Err.Number??"錯誤描述:"??Err.Description,?1
Debug.Print?Err.Description
Err.Clear
End?Function
'******************************************************************
'創(chuàng)建日期:?????2008-10-28
'函數(shù)功能:?????判斷網(wǎng)頁打開是否超時
'輸入?yún)?shù):?????sTime:超時時間
'輸出參數(shù):?????無
'返回值:???????0:成功,1:失敗
'******************************************************************
Public?Function?HtmlOverTime(sTime?As?Long,?sURL?As?String)?As?Long
t?=?GetTickCount
Do?Until?bHtmlFlag?=?True
DoEvents
If?GetTickCount?-?t??sTime?Then
InsertLog?"打開網(wǎng)址:"??sURL??"失敗!",?1
HtmlOverTime?=?1
Exit?Function
End?If
Loop
HtmlOverTime?=?0
End?Function
如果用插件的 復(fù)雜一些,可以查找olelib的知識
'引用olelib實(shí)現(xiàn)IObjectWithSite接口來獲得IE對象
Implements?olelib.IObjectWithSite
'
Private?WithEvents?m_IEObj?As?InternetExplorer
Attribute?m_IEObj.VB_VarHelpID?=?-1
Private?m_site?As?olelib.IUnknown
Private?sDoc????As?New?MSHTML.HTMLDocument
Private?Sub?IObjectWithSite_GetSite(riid?As?olelib.UUID,?ppvSite?As?stdole.IUnknown)
m_site.QueryInterface?riid,?ppvSite
End?Sub
Private?Sub?IObjectWithSite_SetSite(ByVal?pUnkSite?As?stdole.IUnknown)
Set?m_site?=?pUnkSite
Set?m_IEObj?=?pUnkSite
End?Sub
'========================================================
'網(wǎng)頁加載完成后要處理
'
'
'========================================================
Private?Sub?m_IEObj_DocumentComplete(ByVal?pDisp?As?Object,?URL?As?Variant)
On?Error?Resume?Next
Dim?strHTML?????As?String
Dim?strScript???????As?String
Dim?scDoc???????????As?New?MSHTML.HTMLDocument
Dim?sHeads??????????As?IHTMLElementCollection
Dim?sHead???????????As?IHTMLHeadElement
Dim?sElement????????As?IHTMLElement
'獲取Script腳本內(nèi)容
strScript?=?getHTMLScript
'MsgBox?URL
If?InStr(URL,?"zs/browseShenbao.do?formId=")??0?Then
'獲取添加按鈕的html語句
strHTML?=?GetIni("HTML",?"String1",?App.Path??"\HtmlMark.ini")
Set?sDoc?=?m_IEObj.Document
Set?scDoc?=?sDoc.parentWindow.Document.frames("workspace").Document.frames("top_Frame").Document
Set?scDoc?=?scDoc.frames("mainFrame").Document
'獲取HEAD頭的對象
Set?sHeads?=?scDoc.getElementsByTagName("HEAD")
Set?sHead?=?sHeads(0)
'創(chuàng)建script的節(jié)點(diǎn)
Set?sElement?=?scDoc.createElement("script")
sElement.setAttribute?"language",?"JavaScript"
sElement.setAttribute?"text",?strScript
'把script的節(jié)點(diǎn)插入到head中
Call?sHead.insertBefore(sElement,?Null)
'把按鈕的html語句插入到頁面中
scDoc.body.innerHTML?=?scDoc.body.innerHTML??strHTML
End?If
'
If?InStr(URL,?"/initArchive.do")??0?Then
Set?sDoc?=?m_IEObj.Document
'獲取添加按鈕的html語句
strHTML?=?GetIni("HTML",?"String2",?App.Path??"\HtmlMark.ini")
Set?scDoc?=?sDoc.parentWindow.Document.frames("workspace").Document.frames("top_Frame").Document
Set?scDoc?=?scDoc.frames("mainFrame").Document
'獲取HEAD頭的對象
Set?sHeads?=?scDoc.getElementsByTagName("HEAD")
Set?sHead?=?sHeads(0)
'創(chuàng)建script的節(jié)點(diǎn)
Set?sElement?=?scDoc.createElement("script")
sElement.setAttribute?"language",?"JavaScript"
sElement.setAttribute?"text",?strScript
'把script的節(jié)點(diǎn)插入到head中
Call?sHead.insertBefore(sElement,?Null)
'把按鈕的html語句插入到頁面中
scDoc.body.innerHTML?=?scDoc.body.innerHTML??strHTML
End?If
'檔案查詢頁面
If?InStr(URL,?"/showDangAn.do?")??0?Then
Set?sDoc?=?m_IEObj.Document
'獲取添加按鈕的html語句
strHTML?=?GetIni("HTML",?"String3",?App.Path??"\HtmlMark.ini")
'MsgBox?"strHTML---------"??strHTML
'MsgBox?sDoc.body.innerHTML
Set?scDoc?=?sDoc.parentWindow.Document.frames("workspace").Document.frames("top_Frame").Document
Set?scDoc?=?scDoc.frames("mainFrame").Document
'MsgBox?scDoc.body.innerHTML
'獲取HEAD頭的對象
Set?sHeads?=?scDoc.getElementsByTagName("HEAD")
Set?sHead?=?sHeads(0)
'創(chuàng)建script的節(jié)點(diǎn)
Set?sElement?=?scDoc.createElement("script")
sElement.setAttribute?"language",?"JavaScript"
sElement.setAttribute?"text",?strScript
'把script的節(jié)點(diǎn)插入到head中
Call?sHead.insertBefore(sElement,?Null)
'把按鈕的html語句插入到頁面中
scDoc.body.innerHTML?=?scDoc.body.innerHTML??strHTML
End?If
End?Sub
通用的GUID的結(jié)構(gòu)如下
typedef struct _GUID {
DWORD Data1;
WORD Data2;
WORD Data3;
BYTE Data4[8];
} GUID;
舉一個例子:
假設(shè)一個GUID的格式是這樣的 6B29FC40-CA47-1067-B31D-00DD010662DA
其中Data1 是32位,可以看做8個四位十六進(jìn)制數(shù),對應(yīng)于上面的6B29FC40
其中Data2 是16位,可以看做4個四位十六進(jìn)制數(shù),對應(yīng)于上面的CA47
其中Data3 是16位,可以看做4個四位十六進(jìn)制數(shù),對應(yīng)于上面的1067
其中Data4 比較特殊,是8個字節(jié)也就可以看做16個四位十六進(jìn)制數(shù)
取其Data4[0],Data4[1]來組成4個四位十六進(jìn)制數(shù),對應(yīng)于上面的B31D
取其Data4[2],Data4[3]來組成4個四位十六進(jìn)制數(shù),對應(yīng)于上面的00DD
取其Data4[4],Data4[5]來組成4個四位十六進(jìn)制數(shù),對應(yīng)于上面的0106
取其Data4[6],Data4[7]來組成4個四位十六進(jìn)制數(shù),對應(yīng)于上面的62DA
*注意:四位十六進(jìn)制數(shù)對應(yīng)一個GUID字符。
在Windows下提供了一個函數(shù)可以生成一個GUID。需要使用的頭文件 "objbase.h",需要鏈接的庫ole32.lib。
HRESULT CoCreateGuid(GUID * pguid);
在Linux下首先需要下載到相應(yīng)的庫文件和頭文件
如果是Debian用戶可以方便的通過apt命令來獲取相關(guān)資源。
apt-get install uuid-dev
安裝完成后會發(fā)現(xiàn)
在/usr/include/ 下有一個uuid文件夾,其中則包含了uuid.h頭文件。
在/usr/lib/ 下有幾個libuuid*的鏈接庫文件,包括靜態(tài)和動態(tài)鏈接庫。
打開uuid.h你會發(fā)現(xiàn)有一個uuid_generate(uuid_t out) 的函數(shù)聲明。我們可以通過調(diào)用這個函數(shù)來生成UUID。
uuid_generate(reinterpret_castunsigned char *(guid));
生成GUID是不是很簡單!是的,因?yàn)樯傻乃惴ú挥梦覀內(nèi)?shí)現(xiàn),我們是站在前人的肩膀所以我們要感謝他們。
下面給出一個WINDOWS和LINUX下通用的程序。
// uuid_test.cpp
#include string
#include stdio.h
#include iostream
#include "uuid_test.hpp"
#ifdef WIN32
#include objbase.h
#else
#include uuid/uuid.h
#endif
using namespace std;
namespace ChinuxTeam
{
GUID CreateGuid()
{
GUID guid;
#ifdef WIN32
CoCreateGuid(guid);
#else
uuid_generate(reinterpret_castunsigned char *(guid));
#endif
return guid;
}
std::string GuidToString(const GUID guid)
{
char buf[64] = {0};
#ifdef __GNUC__
snprintf(
#else // MSVC
_snprintf_s(
#endif
buf,
sizeof(buf),
"{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
guid.Data1, guid.Data2, guid.Data3,
guid.Data4[0], guid.Data4[1],
guid.Data4[2], guid.Data4[3],
guid.Data4[4], guid.Data4[5],
guid.Data4[6], guid.Data4[7]);
return std::string(buf);
}
}
對應(yīng)的頭文件
// uuid_test.hpp"
#include string
#include stdio.h
#include iostream
typedef struct _GUID
{
unsigned long Data1;
unsigned short Data2;
unsigned short Data3;
unsigned char Data4[8];
} GUID, UUID;
namespace ChinuxTeam
{
GUID CreateGuid();
std::string GuidToString(const GUID guid);
}// namespace ChinuxTeam
最后一個 GuidToString()稍微解釋一下,作用就是把GUID以固定的格式輸出到標(biāo)準(zhǔn)string字符串中,
根據(jù)編譯器的不同調(diào)用分別調(diào)用了不同的字符串打印函數(shù)。
另外附一個簡單的測試函數(shù)
#include string
#include stdio.h
#include iostream
#ifdef WIN32
#include "objbase.h"
#else
typedef struct _GUID
{
unsigned long Data1;
unsigned short Data2;
unsigned short Data3;
unsigned char Data4[8];
} GUID, UUID;
#endif
namespace ChinuxTeam{
GUID CreateGuid();
std::string GuidToString(const GUID guid);
}
運(yùn)行后會生成一個GUID并打印到標(biāo)準(zhǔn)輸出的控制臺上。
把下列內(nèi)容拷貝到“記事本”中,然后保存到上述文件夾,文件名為Objsafe.odl:
[
uuid(C67830E0-D11D-11cf-BD80-00AA00575603),
helpstring("VB IObjectSafety Interface"),
version(1.0)
]
library IObjectSafetyTLB
{
importlib("stdole2.tlb");
[
uuid(CB5BDC81-93C1-11cf-8F20-00805F2CD064),
helpstring("IObjectSafety Interface"),
odl
]
interface IObjectSafety:IUnknown {
[helpstring("GetInterfaceSafetyOptions")]
HRESULT GetInterfaceSafetyOptions(
[in] long riid,
[in] long *pdwSupportedOptions,
[in] long *pdwEnabledOptions);
[helpstring("SetInterfaceSafetyOptions")]
HRESULT SetInterfaceSafetyOptions(
[in] long riid,
[in] long dwOptionsSetMask,
[in] long dwEnabledOptions);
}
}
在命令行提示符下切換到項(xiàng)目文件夾,輸入下列命令創(chuàng)建一個.tlb 文件:
MKTYPLIB objsafe.odl /tlb objsafe.tlb
在VB中新建一個ActiveX Control 項(xiàng)目。修改屬性,把項(xiàng)目命名為IobjSafety,控件命名為DemoCtl。在控件上放置一個按鈕,命名為cmdTest,在它的Click事件中加入一句代碼 MsgBox "Test" 。
打開菜單“工程-引用”,點(diǎn)“瀏覽”,找到剛剛建立的Objsafe.tlb,把它加入到引用中。
增加一個新module名為basSafeCtl,并在其中加入下列代碼:
Option Explicit
Public Const IID_IDispatch = "{00020400-0000-0000-C000-000000000046}"
Public Const IID_IPersistStorage = _
"{0000010A-0000-0000-C000-000000000046}"
Public Const IID_IPersistStream = _
"{00000109-0000-0000-C000-000000000046}"
Public Const IID_IPersistPropertyBag = _
"{37D84F60-42CB-11CE-8135-00AA004BB851}"
Public Const INTERFACESAFE_FOR_UNTRUSTED_CALLER = H1
Public Const INTERFACESAFE_FOR_UNTRUSTED_DATA = H2
Public Const E_NOINTERFACE = H80004002
Public Const E_FAIL = H80004005
Public Const MAX_GUIDLEN = 40
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
(pDest As Any, pSource As Any, ByVal ByteLen As Long)
Public Declare Function StringFromGUID2 Lib "ole32.dll" (rguid As _
Any, ByVal lpstrClsId As Long, ByVal cbMax As Integer) As Long
Public Type udtGUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(7) As Byte
End Type
Public m_fSafeForScripting As Boolean
Public m_fSafeForInitializing As Boolean
Sub Main()
m_fSafeForScripting = True
m_fSafeForInitializing = True
End Sub
在工程屬性中把啟動對象改成Sub Main確保上述代碼會被執(zhí)行。m_fSafeForScripting 和m_fSafeForInitializing兩件變量的值分別指定了腳本安全和初始化安全取值。
打開控件代碼窗口,在聲明部分加入如下代碼(如果有Option Explicit語句,當(dāng)然要保證代碼放在其后):
Implements IObjectSafety
把下面兩個過程代碼拷貝到控件代碼中:
Private Sub IObjectSafety_GetInterfaceSafetyOptions(ByVal riid As _
Long, pdwSupportedOptions As Long, pdwEnabledOptions As Long)
Dim Rc As Long
Dim rClsId As udtGUID
Dim IID As String
Dim bIID() As Byte
pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER Or _
INTERFACESAFE_FOR_UNTRUSTED_DATA
If (riid 0) Then
CopyMemory rClsId, ByVal riid, Len(rClsId)
bIID = String$(MAX_GUIDLEN, 0)
Rc = StringFromGUID2(rClsId, VarPtr(bIID(0)), MAX_GUIDLEN)
Rc = InStr(1, bIID, vbNullChar) - 1
IID = Left$(UCase(bIID), Rc)
Select Case IID
Case IID_IDispatch
pdwEnabledOptions = IIf(m_fSafeForScripting, _
INTERFACESAFE_FOR_UNTRUSTED_CALLER, 0)
Exit Sub
Case IID_IPersistStorage, IID_IPersistStream, _
IID_IPersistPropertyBag
pdwEnabledOptions = IIf(m_fSafeForInitializing, _
INTERFACESAFE_FOR_UNTRUSTED_DATA, 0)
Exit Sub
Case Else
Err.Raise E_NOINTERFACE
Exit Sub
End Select
End If
End Sub
Private Sub IObjectSafety_SetInterfaceSafetyOptions(ByVal riid As _
Long, ByVal dwOptionsSetMask As Long, ByVal dwEnabledOptions As Long)
Dim Rc As Long
Dim rClsId As udtGUID
Dim IID As String
Dim bIID() As Byte
If (riid 0) Then
CopyMemory rClsId, ByVal riid, Len(rClsId)
bIID = String$(MAX_GUIDLEN, 0)
Rc = StringFromGUID2(rClsId, VarPtr(bIID(0)), MAX_GUIDLEN)
Rc = InStr(1, bIID, vbNullChar) - 1
IID = Left$(UCase(bIID), Rc)
Select Case IID
Case IID_IDispatch
If ((dwEnabledOptions And dwOptionsSetMask) _
INTERFACESAFE_FOR_UNTRUSTED_CALLER) Then
Err.Raise E_FAIL
Exit Sub
Else
If Not m_fSafeForScripting Then
Err.Raise E_FAIL
End If
Exit Sub
End If
Case IID_IPersistStorage, IID_IPersistStream, _
IID_IPersistPropertyBag
If ((dwEnabledOptions And dwOptionsSetMask) _
INTERFACESAFE_FOR_UNTRUSTED_DATA) Then
Err.Raise E_FAIL
Exit Sub
Else
If Not m_fSafeForInitializing Then
Err.Raise E_FAIL
End If
Exit Sub
End If
Case Else
Err.Raise E_NOINTERFACE
Exit Sub
End Select
End If
End Sub
用CreateObject方法創(chuàng)建控件,或者把對應(yīng)的文件打包在同一目錄,或者打包進(jìn)程序里面待使用的時候再動態(tài)釋放出來,還有兩種動態(tài)調(diào)用的方法可供參考:
(1)VB使用未注冊的ActiveX代碼
''IDE下可以引用那個dll使用。編譯后可以在未注冊dll的計(jì)算機(jī)上正常工作了。
''使用方法:
''Dim?pDll?As?Long?''記錄Dll,用來最后完美釋放
''Dim?Update?As?Update.Handle?''要實(shí)例化的對象
''Set?Update?=?LoadObjectByName(App.Path??"Update.dll",?"Handle",?pDll)?''?New?Update.Handle
''If?Update?Is?Nothing?Then?Exit?Sub
''Update.Test?''--類中的方法
''Set?Update?=?Nothing?''-----這句不能少,否則會出現(xiàn)意外錯誤
''UnLoadDll?pDll?''----釋放
''模塊中:
Option?Explicit
Private?Declare?Function?LoadLibrary?Lib?"kernel32"?Alias?"LoadLibraryW"?(ByVal?lpLibFileName?As?Long)?As?Long
Private?Declare?Function?GetProcAddress?Lib?"kernel32"?(ByVal?hModule?As?Long,?ByVal?lpProcName?As?String)?As?Long
Private?Declare?Function?FreeLibrary?Lib?"kernel32"?(ByVal?hLibModule?As?Long)?As?Long
Private?Declare?Function?CallAsmCode?Lib?"user32"?Alias?"CallWindowProcW"?(FirstAsmCode?As?Long,?ByVal?pA?As?Long,?ByVal?pB?As?Long,?ByVal?pC?As?Long,?lpD?As?Long)?As?Long
Private?Declare?Sub?CopyMemory?Lib?"kernel32"?Alias?"RtlMoveMemory"?(Destination?As?Any,?Source?As?Any,?ByVal?Length?As?Long)
Private?AsmCode(94)?As?Long
Function?LoadObjectByName(ByVal?DllPath?As?String,?ByVal?ClsName?As?String,?pDll?As?Long)?As?Object
Dim?pObj?As?Long,?TLIAPP?As?Object,?TLI?As?Object
Dim?CLSID?As?String,?IID?As?String,?IIDName?As?String
On?Error?GoTo?Err
Set?TLIAPP?=?CreateObject("TLI.TLIApplication")
Set?TLI?=?TLIAPP.TypeLibInfoFromFile(DllPath)
IIDName?=?"_"??Trim(ClsName)
CLSID?=?TLI.GetTypeInfo(Trim(ClsName)).GUID
IID?=?TLI.GetTypeInfo(Trim(IIDName)).GUID
Set?LoadObjectByName?=?LoadObjectByID(DllPath,?CLSID,?IID,?pDll)
Set?TLI?=?Nothing
Err:
End?Function
Function?LoadObjectByID(ByVal?DllPath?As?String,?ByVal?CLSID?As?String,?ByVal?IID?As?String,?pDll?As?Long)?As?Object
Dim?pObj?As?Long
Call?InitAsmCode
pObj?=?CallAsmCode(AsmCode(20),?StrPtr(DllPath),?StrPtr(CLSID),?StrPtr(IID),?pDll)
CopyMemory?LoadObjectByID,?pObj,?4
End?Function
Function?UnLoadDll(pDll?As?Long)?As?Long
Call?InitAsmCode
UnLoadDll?=?CallAsmCode(AsmCode(79),?pDll,?0,?0,?0)
End?Function
Private?Sub?InitAsmCode()
If?AsmCode(4)?Then?Exit?Sub
Dim?pDll?As?Long
pDll?=?LoadLibrary(StrPtr("kernel32"))
AsmCode(0)?=?GetProcAddress(pDll,?"LoadLibraryW")
AsmCode(1)?=?GetProcAddress(pDll,?"GetProcAddress")
AsmCode(2)?=?GetProcAddress(pDll,?"FreeLibrary")
Call?FreeLibrary(pDll)
AsmCode(4)?=?H476C6C44
AsmCode(5)?=?H6C437465
AsmCode(6)?=?H4F737361
AsmCode(7)?=?H63656A62
AsmCode(8)?=?H4C430074
AsmCode(9)?=?H46444953
AsmCode(10)?=?H536D6F72
AsmCode(11)?=?H6E697274
AsmCode(12)?=?H10067
AsmCode(13)?=?H0
AsmCode(14)?=?HC00000
AsmCode(15)?=?H0
AsmCode(16)?=?H6F4600
AsmCode(17)?=?H65006C
AsmCode(18)?=?H320033
AsmCode(19)?=?H0
AsmCode(20)?=?H83EC8B55?''創(chuàng)建對象函數(shù)入口
AsmCode(21)?=?HE853D8C4
AsmCode(22)?=?H0
AsmCode(23)?=?H6CEB815B
AsmCode(24)?=?H8D100010
AsmCode(25)?=?H105293
AsmCode(26)?=?H93FF5210
AsmCode(27)?=?H10001010
AsmCode(28)?=?H32938D50
AsmCode(29)?=?H52100010
AsmCode(30)?=?H1493FF50
AsmCode(31)?=?H8D100010
AsmCode(32)?=?H101C93
AsmCode(33)?=?HFF028910
AsmCode(34)?=?H101893
AsmCode(35)?=?H875FF10
AsmCode(36)?=?H101093FF
AsmCode(37)?=?HC00B1000
AsmCode(38)?=?H86840F
AsmCode(39)?=?H45890000
AsmCode(40)?=?H20938DFC
AsmCode(41)?=?H52100010
AsmCode(42)?=?H1493FF50
AsmCode(43)?=?HB100010
AsmCode(44)?=?H506674C0
AsmCode(45)?=?H52EC558D
AsmCode(46)?=?HFF0C75FF
AsmCode(47)?=?H101C93
AsmCode(48)?=?H558D5810
AsmCode(49)?=?H938D52D8
AsmCode(50)?=?H10001042
AsmCode(51)?=?HEC558D52
AsmCode(52)?=?HBD0FF52
AsmCode(53)?=?H8D3E75C0
AsmCode(54)?=?HFF52DC55
AsmCode(55)?=?H93FF1075
AsmCode(56)?=?H1000101C
AsmCode(57)?=?HD8558B50
AsmCode(58)?=?H8D54128B
AsmCode(59)?=?H6A50DC45
AsmCode(60)?=?HD875FF00
AsmCode(61)?=?HB0C52FF
AsmCode(62)?=?H8B1575C0
AsmCode(63)?=?H4D8BFC45
AsmCode(64)?=?H59018914
AsmCode(65)?=?H8BD18B51
AsmCode(66)?=?H52FF5112
AsmCode(67)?=?H14EB5804
AsmCode(68)?=?HEB06EB58
AsmCode(69)?=?HEB02EB0F
AsmCode(70)?=?HFC75FF0B
AsmCode(71)?=?H101893FF
AsmCode(72)?=?HC0331000
AsmCode(73)?=?H10C2C95B
AsmCode(74)?=?H6C6C4400
AsmCode(75)?=?H556E6143
AsmCode(76)?=?H616F6C6E
AsmCode(77)?=?H776F4E64
AsmCode(78)?=?H0
AsmCode(79)?=?H53EC8B55?''嘗試卸載DLL函數(shù)入口
AsmCode(80)?=?HE8
AsmCode(81)?=?HEB815B00
AsmCode(82)?=?H10001155
AsmCode(83)?=?H1139938D
AsmCode(84)?=?HFF521000
AsmCode(85)?=?H93FF0875
AsmCode(86)?=?H10001014
AsmCode(87)?=?H1374C00B
AsmCode(88)?=?HC00BD0FF
AsmCode(89)?=?H75FF0E74
AsmCode(90)?=?H1893FF08
AsmCode(91)?=?H33100010
AsmCode(92)?=?H4801EBC0
AsmCode(93)?=?H10C2C95B
AsmCode(94)?=?H90909000
End?Sub
(2)vb.net 通過類廠創(chuàng)建com對象的方法
vb.net中要創(chuàng)立com對象可以用new或者CreateObject,就像C++中的CoCreateInstance,一步調(diào)用就直接創(chuàng)建了com對象。在它們之中包括了對CoGetClassObject和CreateInstance的調(diào)用。默認(rèn)使用的類廠是IClassFactory。?
一般情況下,new或CreateObject已經(jīng)夠用了。但是如果我們的com組件增加了授權(quán),也就是使用了IClassFactory2接口。那么在vb中繼續(xù)用這種方法就不能創(chuàng)建我們所需要的組件了。我們必須使用IClassFactory2。
在C++中實(shí)現(xiàn)很簡單。只需要通過CoGetClassObject獲得IClassFactory2接口,然后調(diào)用它的CreateInstanceLic來創(chuàng)建組件。
因此,在vb.net中,我們就可以仿照c++中的方法。下面是相關(guān)代碼:
首先聲明IClassFactory2的接口。IClassFactory2.idl如下:
[
uuid(6ED6AF97-F279-4d57-A392-0B8ACF89426C),
version(1.0),
helpstring("INVENTOROCIDL?Type?Library?1.0")
]
library?INVENTOROCIDL
{
interface?IClassFactory2;
typedef?enum?enuCLSCTX
{
enuCLSCTX_INPROC_SERVER????=?1,
enuCLSCTX_INPROC_HANDLER???=?2,
enuCLSCTX_LOCAL_SERVER?????=?4,
enuCLSCTX_REMOTE_SERVER????=?16,
enuCLSCTX_NO_CODE_DOWNLOAD?=?400,
enuCLSCTX_NO_FAILURE_LOG?=?4000,
enuCLSCTX_SERVER????=?(1?|?4?|?16),
enuCLSCTX_ALL???????=?(2?|?1)
}?CLSCTX;
[
object,
uuid(B196B28F-BAB4-101A-B69C-00AA00341D07),
pointer_default(unique)
]
interface?IClassFactory2?:?IClassFactory
{
typedef?IClassFactory2?*?LPCLASSFACTORY2;
typedef?struct?tagLICINFO?{
LONG?cbLicInfo;
BOOL?fRuntimeKeyAvail;
BOOL?fLicVerified;
}?LICINFO;
typedef?struct?tagLICINFO?*?LPLICINFO;
HRESULT?GetLicInfo(
[out,?retval]?LICINFO?*?pLicInfo
);
HRESULT?RequestLicKey(
[in]?LONG?dwReserved,
[out,?retval]?BSTR?*?pBstrKey
);
[local]
HRESULT?CreateInstanceLic(
[in]?IUnknown?*?pUnkOuter,
[in]?IUnknown?*?pUnkReserved,
[in]?GUID*?riid,
[in]?BSTR?bstrKey,
[out,?retval,?iid_is(riid)]?PVOID?*?ppvObj
);
}
}
用midl編譯成IClassFactory2.tlb,并導(dǎo)入到vb.net的工程當(dāng)中。
聲明如下api用于得到IClassFactory2接口:
Declare?Function?CoGetClassObject?Lib?"ole32.dll"?(ByRef?rclsid?As?Guid,?ByVal?context?As?Short,?ByRef?serverInfo?As?IntPtr,?ByRef?riid?As?Guid,?ByRef?ppv?As?IntPtr)?As?Integer
'添加如下代碼:
Const?bstrInventorApplication?As?String?=?"{B6B5DC40-96E3-11d2-B774-0060B0F159EF}"
Const?bstrIClassFactory2?As?String?=?"{B196B28F-BAB4-101A-B69C-00AA00341D07}"
Const?bstrIDispatch?As?String?=?"{00020400-0000-0000-C000-000000000046}"
Private?IClsFry2?As?INVENTOROCIDL.IClassFactory2?
Dim?bstrLicence?As?String?=?"12345678"???'licence?key
Dim?guidInventorApplication?As?Guid?=?New?Guid(bstrInventorApplication)
Dim?guidIClassFactory2?As?Guid?=?New?Guid(bstrIClassFactory2)
Dim?guidIDispatch?As?Guid?=?New?Guid(bstrIDispatch)
Dim?InventorGuid?As?INVENTOROCIDL.GUID??'used?by?CreateInstanceLic,defined?in?INVENTOROCIDL
'transform?Guid?of?IDispatch?to?INVENTOROCIDL.GUID
Dim?byteArry()?As?Byte?=?guidIDispatch.ToByteArray()
Dim?MyGC?As?GCHandle?=?GCHandle.Alloc(byteArry,?GCHandleType.Pinned)
InventorGuid?=?CType(Marshal.PtrToStructure(MyGC.AddrOfPinnedObject,?InventorGuid.GetType()),?INVENTOROCIDL.GUID)
'get?the?IClassFactory2?Interface
Dim?obj?As?IntPtr
CoGetClassObject(guidInventorApplication,?CInt(INVENTOROCIDL.enuCLSCTX.enuCLSCTX_LOCAL_SERVER),?Nothing,?guidIClassFactory2,?obj)
IClsFry2?=?CType(Marshal.GetTypedObjectForIUnknown(obj,?System.Type.GetTypeFromCLSID(guidIClassFactory2)),?INVENTOROCIDL.IClassFactory2)
'create?Inventor?Instance?by?using?Licence
obj?=?IClsFry2.CreateInstanceLic(Nothing,?Nothing,?InventorGuid,?bstrLicence)
InvApp?=?CType(Marshal.GetTypedObjectForIUnknown(obj,?System.Type.GetTypeFromCLSID(guidInventorApplication)),?Inventor.Application)
致此創(chuàng)建成功!
由于各種原因,開發(fā)人員使用全局唯一標(biāo)識符(GUID),例如,在類中給全局標(biāo)識符賦值使用GUID或者是在處理數(shù)據(jù)庫的時候使用GUID。GUID是微軟分布計(jì)算環(huán)境(DCE)全局唯一標(biāo)識符(UUID)的實(shí)現(xiàn)。GUID是128位全局唯一標(biāo)識符,它是根據(jù)不斷頻繁變化的因素自動產(chǎn)生的。有一個相當(dāng)小的可能性,那就是GUID的值可能都是零,或者是它可能等于其它的GUID。你能使用GUID通過所有需要唯一標(biāo)識的計(jì)算機(jī)或者網(wǎng)絡(luò)。GUID可以標(biāo)識界面或者類對象。一個 GUID由一組8位的十六進(jìn)制數(shù)和三組4位十六進(jìn)制數(shù)(也就是一組12位十六進(jìn)制數(shù))組成。在下面的腳本中,我手動生成了GUID,我利用VB.NET在后面的應(yīng)用中使用了它:Private Sub GenerateGUID() Dim sGUID As String sGUID = System.GUID.NewGUID.ToString() MessageBox.Show(sGUID) End Sub在這個例子中,我定義了一個字符串變量,sGUID,這個變量是用來保存將生成的GUID。然后我再將System.GUID.NewGUID(使用System.GUID名字空間)這個方法的返回值賦給sGUID,再使用ToString()將這個結(jié)果轉(zhuǎn)換成字符串類型。這樣就可以在消息對話框中顯示這個結(jié)果了。注意:Visual Studio .NET允許你通過運(yùn)行Visual Studio .NET Command Prompt交互生成GUID??梢栽L問MSDN網(wǎng)站獲得更詳細(xì)的信息。你也可以參考TechRepublic的另一篇文章“Generating and working with GUIDs in .NET。