[英]TNetSharingManager and Windows 8 & 10: Access Denied
I have a delphi porgram that uses TNetsharingmanager tool to enable and disable communication through NIC card. 我有一个使用TNetsharingmanager工具来启用和禁用通过NIC卡进行通信的delphi porgram。 It has worked flawlessly on Windows XP and 7, but it won't on Windows 8 and 10. It keeps raising "Access Denied" error, when my program tries to connect to the first available NIC card on start up. 它在Windows XP和7上都可以正常运行,但在Windows 8和10上却不能正常运行。当我的程序尝试在启动时连接到第一个可用的NIC卡时,它会不断出现“ Access Denied”错误。 I can't seem to figure this out. 我似乎无法弄清楚这一点。 I thought maybe its because of the current user doesn't have the permission to make connection, but that isn't the case. 我认为可能是因为当前用户没有建立连接的权限,但事实并非如此。 I even ran my program as an administrator and still raises the error. 我什至以管理员身份运行程序,但仍然引发错误。 Once you okay the error box, my program continues without a problem. 一旦您确定错误框,我的程序就可以继续运行而不会出现问题。
Here is the code I use: 这是我使用的代码:
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;
I thought I had setup my program to run as administrator, but apparently I didn't do it right. 我以为我已经将程序设置为以管理员身份运行,但是显然我没有正确执行。 Once I did the following, that access denied message went away. 一旦执行以下操作,该拒绝访问消息就会消失。
To run an application one time with a full administrator access token 使用完整的管理员访问令牌一次运行应用程序
Locate the program icon or a shortcut in Windows Explorer. 在Windows资源管理器中找到程序图标或快捷方式。
Right-click the program icon or shortcut, and then click Run as administrator. 用鼠标右键单击程序图标或快捷方式,然后单击以管理员身份运行。
When the UAC message is displayed, do one of the following: 显示UAC消息时,请执行以下操作之一:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.