2010-10-16 110 views
2

我想在Delphi 2010中使用TNetSharingManager來編譯這個project。我已經導入類型庫,並試圖編譯它,但不幸的是我在這個函數得到一個訪問衝突:TNetSharingManager訪問衝突問題

 
function TNetSharingManager.GetDefaultInterface: INetSharingManager; 
begin 
    if FIntf = nil then 
    Connect; 
    Assert(FIntf nil, 'DefaultInterface is NULL. Component is not connected to Server. You must call "Connect" or "ConnectTo" before this operation'); 
    Result := FIntf; 
end; 

(NETCONLib_TLB的一部分) 的錯誤是:if FIntf = nil then一些奇怪的原因..

這是調用它的代碼:

 
procedure TForm1.GetConnectionList(Strings,IdList: TStrings); 
var 
    pEnum: IEnumVariant; 
    vNetCon: OleVARIANT; 
    dwRetrieved: Cardinal; 
    pUser: NETCONLib_TLB.PUserType1; 
    NetCon : INetConnection; 
begin 
    Strings.Clear; 
    IdList.Clear; 
    pEnum := (NetSharingManager.EnumEveryConnection._NewEnum as IEnumVariant); 
    while (pEnum.Next(1, vNetCon, dwRetrieved) = S_OK) do 
    begin 
    (IUnknown(vNetCon) as INetConnection).GetProperties(pUser); 
    NetCon := (IUnknown(vNetCon) as INetConnection); 

    if (pUser.Status in [NCS_CONNECTED,NCS_CONNECTING])//remove if you want disabled NIC cards also 
    and (pUser.MediaType in [NCM_LAN,NCM_SHAREDACCESSHOST_LAN,NCM_ISDN]) 
    and (GetMacAddress(GuidToString(pUser.guidId))'') then 
    begin 
     //we only want valid network cards that are enabled 
     Strings.Add(pUser.pszwName); 
     IdList.Add(GuidToString(pUser.guidId)); 
    end; 
    end; 
end; 

我不明白爲什麼我不能比較。有任何想法嗎?

回答

2

當觸發該錯誤時,很可能TNetSharingManager對象本身已經死亡(或者不是首先創建的)。表達式是對類的實際字段的第一次引用,即它將指向無效的地址空間。

[編輯]我下載了源代碼並按照步驟導入了TLB(Delphi 2010)。爲了執行應用程序,我必須(a)以管理員身份運行Delphi,因爲默認情況下我不是高級用戶,(b)必須添加支票pUser <>無,因爲最終的getProperties返回nil結構,但除此之外,代碼運行良好。很不幸,我似乎無法重現您的問題。

重讀你的問題,你得到一個AV,而編譯

+0

感謝您的回答Paul-Jan,我嘗試了很多方法通過自己創建對象(和成員對象)來解決問題。 – 2010-10-17 22:09:29

+0

[按錯誤輸入] ..但沒有運氣。我應該提到:FIntf:INetSharingManager;真正令我驚訝的是,這個代碼在Delphi 7中編譯(作爲原始項目)。這意味着包裝有問題。在我看來,沒有人對這些單位給予任何關注,可惜,考慮到這是互聯網管理網絡連接的極少數例子之一。再次感謝! – 2010-10-17 22:16:37

+0

剛剛注意到編輯。非常感謝你的努力!很高興知道它仍然有效!不幸的是,我仍然無法運行它(即使編譯沒有錯誤)。我會繼續努力,希望能儘快完成。 – 2010-10-19 00:55:10