2015-02-06 86 views
0

我有一個使用TNetsharingmanager工具啓用和禁用通過NIC卡通信的delphi porgram。它在Windows XP和7上運行得非常完美,但不會在Windows 8和10上運行。當我的程序在啓動時嘗試連接第一個可用NIC卡時,它不斷提高「拒絕訪問」錯誤。我似乎無法弄清楚這一點。我想也許是因爲當前用戶沒有權限進行連接,但事實並非如此。我甚至以管理員身份運行我的程序,但仍然引發錯誤。一旦你確定錯誤框,我的程序繼續沒有問題。TNetSharingManager和Windows 8&10:拒絕訪問

這裏是我使用的代碼:

procedure TDXCommdlg.GetConnectionList(Strings,IdList: TStrings); 
var 
    pEnum: IEnumVariant; 
    vNetCon: OleVARIANT; 
    dwRetrieved: Cardinal; 
    pUser: NETCONLib_TLB.PUserType1; 
    NetCon : INetConnection; 
begin 
    Strings.Clear; 
    IdList.Clear; 
    pEnum := (NetSharingManager1.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]) 
     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; 

function TDXCommdlg.GetMacAddress(CardID: string): String; 
var 
    Reg: TRegistry; 
    KeyValues: TSTringList; 
    i: integer; 
    CardInstanceID,CardAddress: string; 
begin 
    Result := ''; 
    Reg := TRegistry.Create; 
    KeyValues := TStringList.Create; 
    try 
     Reg.RootKey:=HKEY_LOCAL_MACHINE; 
     if Reg.OpenKey(MacLocation,false) then 
     begin 
      Reg.GetKeyNames(KeyValues); 
      Reg.CloseKey; 

      for i := 0 to KeyValues.Count-1 do 
      if reg.OpenKey(MacLocation+'\'+KeyValues[i],false) then 
      begin 
       CardInstanceID := Reg.ReadString('NetCfgInstanceId'); 
       CardAddress := Reg.ReadString('NetworkAddress'); 
       Reg.CloseKey; 

       if CardInstanceID = CardId then 
       begin 
        if CardAddress='' then CardAddress := 'Hardware'; 
         Result := CardAddress; 
        break; 
       end; 
       end; 
      end; 
     finally 
     Reg.Free; 
     KeyValues.Free; 
    end; 
end; 

procedure TDXCommdlg.ResetNIC(const aConnection: string); 
var 
    pEnum: IEnumVariant; 
    vNetCon: OleVARIANT; 
    dwRetrieved: Cardinal; 
    pUser: NETCONLib_TLB.PUserType1; 
begin 
    enabled := false; 
    try 
     pEnum := (NetSharingManager1.EnumEveryConnection._NewEnum as IEnumVariant); 
    while (pEnum.Next(1, vNetCon, dwRetrieved) = S_OK) do 
    begin 
     (IUnknown(vNetCon) as INetConnection).GetProperties(pUser); 
     if pUser.pszwName = aConnection then 
     begin 
      (IUnknown(vNetCon) as INetConnection).Disconnect; 
      (IUnknown(vNetCon) as INetConnection).Connect; 
      sleep(2000); 
      break; 
     end; 
    end; 
    finally 
    enabled := true; 
end; 
end; 

回答

0

我原本以爲我的安裝程序以管理員身份運行,但顯然我沒有這樣做是正確的。一旦我做了以下,該訪問被拒絕的消息去 了。

要使用完全管理員訪問令牌

  1. 找到程序圖標或Windows資源管理器的快捷方式運行應用程序一次。

  2. 右鍵單擊程序圖標或快捷方式,然後單擊「作爲 管理員運行」。

    當顯示UAC消息,請執行下列操作之一:

    • 如果您登錄作爲標準用戶,或者如果UAC配置爲 總是需要憑據,請輸入相應的管理 憑據,然後單擊確定。
    • 如果以管理員身份登錄並且UAC未配置爲始終需要憑據 ,請單擊是以啓動該應用程序。