问大家一个在windows上使用什么API可以修改电脑的DNS

砍柴君 2013-10-29 04:23:20
我使用的是ShellExecuteEx函数在cmd上执行netsh命令来修改,但是这样做有个问题 就是每次执行这个命令 360会弹窗口,而且在cmd命令上执行命令,就会打开cmd界面(不管界面是显示还是不显示),如果设置成不显示状态,且不关闭,则会在任务管理器中看到这个进程,如果执行完就关闭的话得等命令执行完了关闭命令才有效,而这期间到底需要多少时间无法判断,这里主要是想问下还有什么windows API可以修改DNS,是用c++调用的,我在MSDN上找了很久还是没找到
...全文
354 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2013-10-30
  • 打赏
  • 举报
回复
Summary of Name Resolution Functions The name resolution functions can be grouped into three categories: Service installation, client queries, and helper functions (and macros). The sections that follow identify the functions in each category and briefly describe their intended use. Key data structures are also described. Service Installation WSAInstallServiceClass WSARemoveServiceClass WSASetService When the required service class does not already exist, an application uses WSAInstallServiceClass to install a new service class by supplying a service class name, a GUID for the service class ID, and a series of WSANSCLASSINFO structures. These structures are each specific to a particular name space, and supply common values such as recommended TCP port numbers or Netware SAP Identifiers. A service class can be removed by calling WSARemoveServiceClass and supplying the GUID corresponding to the class ID. Once a service class exists, specific instances of a service can be installed or removed via WSASetService. This function takes a WSAQUERYSET structure as an input parameter along with an operation code and operation flags. The operation code indicates whether the service is being installed or removed. The WSAQUERYSET structure provides all of the relevant information about the service including service class ID, service name (for this instance), applicable name space identifier and protocol information, and a set of transport addresses at which the service listens. Services should invoke WSASetService when they initialize in order to advertise their presence in dynamic name spaces. Client Query WSAEnumNameSpaceProviders WSALookupServiceBegin WSALookupServiceNext WSALookupServiceEnd The WSAEnumNameSpaceProviders function allows an application to discover which name spaces are accessible via Windows Sockets's name resolution facilities. It also allows an application to determine whether a given name space is supported by more than one name space provider, and to discover the provider ID for any particular name space provider. Using a provider ID, the application can restrict a query operation to a specified name space provider. Windows Sockets' name space query operations involves a series of calls: WSALookupServiceBegin, followed by one or more calls to WSALookupServiceNext and ending with a call to WSALookupServiceEnd. WSALookupServiceBegin takes a WSAQUERYSET structure as input in order to define the query parameters along with a set of flags to provide additional control over the search operation. It returns a query handle which is used in the subsequent calls to WSALookupServiceNext and WSALookupServiceEnd. The application invokes WSALookupServiceNext to obtain query results, with results supplied in an application-supplied WSAQUERYSET buffer. The application continues to call WSALookupServiceNext until the error code WSA_E_NO_MORE is returned indicating that all results have been retrieved. The search is then terminated by a call to WSALookupServiceEnd. The WSALookupServiceEnd function can also be used to cancel a currently pending WSALookupServiceNext when called from another thread. In Windows Socket 2, conflicting error codes are defined for WSAENOMORE (10102) and WSA_E_NO_MORE (10110). The error code WSAENOMORE will be removed in a future version and only WSA_E_NO_MORE will remain. For Windows Socket 2, however, applications should check for both WSAENOMORE and WSA_E_NO_MORE for the widest possible compatibility with Name Space Providers that use either one. Helper Functions WSAGetServiceClassNameByClassId WSAAddressToString WSAStringToAddress WSAGetServiceClassInfo The name resolution helper functions include a function to retrieve a service class name given a service class ID, a pair of functions used to translate a transport address between a sockaddr struct and an ASCII string representation, a function to retrieve the service class schema information for a given service class, and a set of macros for mapping well known services to pre-allocated GUIDs. The following macros from winsock2.h aid in mapping between well known service classes and these name spaces. SVCID_TCP(Port) SVCID_UDP(Port) SVCID_NETWARE(Object Type) Given a port for TCP/IP or UDP/IP or the object type in the case of Netware, return the GUID (port number in host order) IS_SVCID_TCP(GUID) IS_SVCID_UDP(GUID) IS_SVCID_NETWARE(GUID) Returns TRUE if the GUID is within the allowable range SET_TCP_SVCID(GUID, port) SET_UDP_SVCID(GUID, port) Initializes a GUID structure with the GUID equivalent for a TCP or UDP port number (port number must be in host order) PORT_FROM_SVCID_TCP(GUID) PORT_FROM_SVCID_UDP(GUID) SAPID_FROM_SVCID_NETWARE(GUID) Returns the port or object type associated with the GUID (port number in host order)
赵4老师 2013-10-30
  • 打赏
  • 举报
回复
不一定对,仅供参考: WSASetService The Windows Sockets WSASetService function registers or deregisters a service instance within one or more name spaces. This function can be used to affect a specific name space provider, all providers associated with a specific name space, or all providers across all name spaces. INT WSASetService( LPWSAQUERYSET lpqsRegInfo, WSAESETSERVICEOP essOperation, DWORD dwControlFlags ); Parameters lpqsRegInfo [in] A pointer to the service information for registration or deregistration. essOperation [in] An enumeration whose values include: RNRSERVICE_REGISTER Register the service. For SAP, this means sending out a periodic broadcast. This is a NOP for the DNS name space. For persistent data stores, this means updating the address information. RNRSERVICE_DEREGISTER Deregister the service. For SAP, this means stop sending out the periodic broadcast. This is a NOP for the DNS name space. For persistent data stores this means deleting address information. RNRSERVICE_DELETE Delete the service from dynamic name and persistent spaces. For services represented by multiple CSADDR_INFO structures (using the SERVICE_MULTIPLE flag), only the supplied address will be deleted, and this much match exactly the corresponding CSADD_INFO structure that was supplied when the service was registered. dwControlFlags [in] The meaning of dwControlFlags is dependent on the following values: Flag Meaning SERVICE MULTIPLE Controls scope of operation. When clear, service addresses are managed as a group. A register or deregister invalidates all existing addresses before adding the given address set. When set, the action is only performed on the given address set. A register does not invalidate existing addresses and a deregister only invalidates the given set of addresses. The available values for essOperation and dwControlFlags combine to give meanings as shown in the following table: Operation Flags Service already exists Service does not exist RNRSERVICE _REGISTER none Overwrite the object. Use only addresses specified. Object is REGISTERED. Create a new object. Use only addresses specified. Object is REGISTERED. RNRSERVICE _REGISTER SERVICE _MULTIPLE Update object. Add new addresses to existing set. Object is REGISTERED. Create a new object. Use all addresses specified. Object is REGISTERED. RNRSERVICE _DEREGISTER none Remove all addresses, but do not remove object from name space. Object is DEREGISTERED. WSASERVICE _NOT_FOUND RNRSERVICE _DEREGISTER SERVICE _MULTIPLE Update object. Remove only addresses that are specified. Only mark object as DEREGISTERED if no addresses present. Do not remove from the name space. WSASERVICE _NOT_FOUND RNRSERVICE _DELETE none Remove object from the name space. WSASERVICE _NOT_FOUND RNRSERVICE _DELETE SERVICE _MULTIPLE Remove only addresses that are specified. Only remove object from the name space if no addresses remain. WSASERVICE _NOT_FOUND Remarks SERVICE_MULTIPLE lets an application manage its addresses independently. This is useful when the application wants to manage its protocols individually or when the service resides on more than one machine. For instance, when a service uses more than one protocol, it may find that one listening socket aborts but the others remain operational. In this case, the service could deregister the aborted address without affecting the other addresses. When using SERVICE_MULTIPLE, an application must not let stale addresses remain in the object. This can happen if the application aborts without issuing a DEREGISTER request. When a service registers, it should store its addresses. On its next invocation, the service should explicitly deregister these old stale addresses before registering new addresses. Service Properties The following table describes how service property data is represented in a WSAQUERYSET structure. Fields labeled as (Optional) can be supplied with a NULL pointer. WSAQUERYSET Field Name Service Property Description Field Name Service Property Description dwSize Must be set to sizeof(WSAQUERYSET). This is a versioning mechanism. DwOutputFlags Not applicable and ignored. LpszServiceInstanceName Referenced string contains the service instance name. LpServiceClassId The GUID corresponding to this service class. lpVersion (Optional) Supplies service instance version number. LpszComment (Optional) An optional comment string. DwNameSpace See table below. LpNSProviderId See table below. LpszContext (Optional) Specifies the starting point of the query in a hierarchical name space. DwNumberOfProtocols Ignored. LpafpProtocols Ignored. LpszQueryString Ignored. DwNumberOfCsAddrs The number of elements in the array of CSADDRO_INFO structures referenced by lpcsaBuffer. LpcsaBuffer A pointer to an array of CSADDRO_INFO structures that contain the address(es) that the service is listening on. lpBlob (Optional) This is a pointer to a provider-specific entity. As illustrated below, the combination of the dwNameSpace and lpNSProviderId parameters determine that name space providers are affected by this function. dwNameSpace lpNSProviderId Scope of Impact Ignored Non-NULL The specified name space provider a valid name space ID NULL All name space providers that support the indicated name space NS_ALL NULL All name space providers Return Values The return value for WSASetService is zero if the operation was successful. Otherwise, the value SOCKET_ERROR is returned, and a specific error number can be retrieved by calling WSAGetLastError. Error Codes WSAEACCES The calling routine does not have sufficient privileges to install the Service. WSAEINVAL One or more required parameters were invalid or missing. WSANOTINITIALIZED The WiS2_32.DLL has not been initialized. The application must first call WSAStartup before calling any Windows Sockets functions WSA NOT ENOUGH MEMORY There was insufficient memory to perform the operation. WSASERVICE NOT FOUND No such service is known. The service cannot be found in the specified name space. QuickInfo Windows NT: Yes Windows: Yes Windows CE: Unsupported. Header: Declared in winsock2.h. Import Library: Link with ws2_32.lib.
砍柴君 2013-10-30
  • 打赏
  • 举报
回复
引用 7 楼 u012624807 的回复:
可以考虑修改注册表试试
不错 写注册表是可以达到效果
lifeisshortyouneed 2013-10-30
  • 打赏
  • 举报
回复
API不太了解 以前干过类似的事,是个笨办法,就是用 vbs脚本来隐藏执行cmd命令的。 界面隐藏的,cmd进程肯定有的。不过那会就完全可以达到我的要求了。
set objshell=createobject("wscript.shell")
objshell.run("%comspec% /k netsh int ip set dns 本地连接 static xxx.xxx.xxx.xxx primary"),0,true
u012624807 2013-10-30
  • 打赏
  • 举报
回复
可以考虑修改注册表试试
砍柴君 2013-10-30
  • 打赏
  • 举报
回复
引用 5 楼 rocktyt2 的回复:
lpFile 改为netsh, 参数里去掉netsh试试 不知道是不是你要的效果……
我现在要的是不需要用这种方法去改 是要别的方法 c#里面可以用API改 C++里面肯定也有 只不过我不知道这个API而已 所以来问下
rocktyt 2013-10-30
  • 打赏
  • 举报
回复
引用 6 楼 jyckder 的回复:
[quote=引用 5 楼 rocktyt2 的回复:] lpFile 改为netsh, 参数里去掉netsh试试 不知道是不是你要的效果……
我现在要的是不需要用这种方法去改 是要别的方法 c#里面可以用API改 C++里面肯定也有 只不过我不知道这个API而已 所以来问下[/quote]DnsReplaceRecordSet试过吗
rocktyt 2013-10-29
  • 打赏
  • 举报
回复
lpFile 改为netsh, 参数里去掉netsh试试 不知道是不是你要的效果……
砍柴君 2013-10-29
  • 打赏
  • 举报
回复
引用 3 楼 rocktyt2 的回复:
[quote=引用 2 楼 jyckder 的回复:] [quote=引用 1 楼 rocktyt2 的回复:] netsh不需要打开cmd界面啊,是会有画面一闪倒是没错…… 就算你调API,360该弹还是会弹 你的ShellExecuteEx是怎么调的?
就是打开cmd命令行界面 然后传入修改DNS的netsh命令参数, 我看别人用c#开发的用的是c#里面一个API 360都不会弹窗[/quote]我其实想让你发ShellExecuteEx调用的代码 查查DnsReplaceRecordSet试试[/quote]
CString strShellCmd = _T("netsh interface ip set dnsservers "无线网络连接" static 208.67.222.222");
SHELLEXECUTEINFO   ExeInfo;   
	ZeroMemory(&ExeInfo, sizeof(SHELLEXECUTEINFO));   
	ExeInfo.cbSize = sizeof(SHELLEXECUTEINFO);   
	ExeInfo.lpFile = _T("cmd.exe"); 
	ExeInfo.lpParameters = strShellCmd;
	ExeInfo.fMask = SEE_MASK_NOCLOSEPROCESS;   
	ExeInfo.nShow = SW_HIDE;   
	ExeInfo.hwnd = NULL;
	ExeInfo.lpVerb = _T("runas");
	ExeInfo.lpDirectory = NULL;  
	ExeInfo.hInstApp = NULL;
	//执行命令
	BOOL nret = ShellExecuteEx(&ExeInfo);
	CString strExit(_T("/c exit"));
	ExeInfo.lpParameters = strExit;
	nret = ShellExecuteEx(&ExeInfo);
这就是那个修改DNS的代码
rocktyt 2013-10-29
  • 打赏
  • 举报
回复
引用 2 楼 jyckder 的回复:
[quote=引用 1 楼 rocktyt2 的回复:] netsh不需要打开cmd界面啊,是会有画面一闪倒是没错…… 就算你调API,360该弹还是会弹 你的ShellExecuteEx是怎么调的?
就是打开cmd命令行界面 然后传入修改DNS的netsh命令参数, 我看别人用c#开发的用的是c#里面一个API 360都不会弹窗[/quote]我其实想让你发ShellExecuteEx调用的代码 查查DnsReplaceRecordSet试试
砍柴君 2013-10-29
  • 打赏
  • 举报
回复
引用 1 楼 rocktyt2 的回复:
netsh不需要打开cmd界面啊,是会有画面一闪倒是没错…… 就算你调API,360该弹还是会弹 你的ShellExecuteEx是怎么调的?
就是打开cmd命令行界面 然后传入修改DNS的netsh命令参数, 我看别人用c#开发的用的是c#里面一个API 360都不会弹窗
rocktyt 2013-10-29
  • 打赏
  • 举报
回复
netsh不需要打开cmd界面啊,是会有画面一闪倒是没错…… 就算你调API,360该弹还是会弹 你的ShellExecuteEx是怎么调的?

64,680

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧