怎样通过编程实现"网络连接"中的"自动获得IP地址"和"自动获得DNS域名服务器"???请不吝赐教
希望能给出相关函数或一些文档
希望能给出相关函数或一些文档
解决方案 »
- Domal后窗口不显示的问题有哪些
- ccombobox::deletestring异常
- vc编译链接时总出现这样的情况是怎么回事啊?50分
- 看原版书太费劲了,没想到看中文版还不如看原版,一生气还是自己翻译大家看看?
- 怎样在对话框中贴上多个非模态对话框?
- VC++处理消息的机制与Win32API有何不同
- 关于ADO通过CRecordset读取数据的问题
- 关于tooltip的问题,急!
- 如何把1个DOS程序放到对话框中
- 各位,你们怎样查MFC的原码,查到某个函数的声明,怎么查它在CPP里的定义?
- windows.h和STL冲突?
- 请教各位opengl同仁:有谁写过nurbs曲面拟合中利用曲面上测得的形值点反求曲面控制点的程序,拿出来给兄弟参看参考!
The following examples illustrate two uses of the DHCP API: Example 1 illustrates how to use the DhcpRequestParams function to retrieve a host name.
Example 2 shows how the DhcpRegisterParamChange function can be used to keep track of host name changes.
Example 1: Using the DhcpRequestParams function
The following example illustrates how to retrieve the host name using the DhcpRequestParams function call. The name of the adapter can be retrieved using the GetInterfaceInfo structure, which is part of the Internet Protocol Helper API:BOOL
RetrieveHostName(
IN LPCWSTR pszAdapterName,
IN OUT CHAR[] pszHostNameBuf, // must be large enough buffer
IN DWORD dwHostNameBufSize
)
/*++Routine returns TRUE on success and FALSE on failure.--*/
{
DWORD dwError, dwSize;
CHAR TmpBuffer[1000]; // host name won't be larger than this
DHCPCAPI_PARAMS DhcpApiHostNameParams = {
0, // Flags
OPTION_HOST_NAME, // OptionId
FALSE, // vendor specific?
NULL, // data filled in on return
0 // nBytes
};
DHCPCAPI_PARAMS_ARRAY DhcpApiParamsArray = {
1, // only one option to request
&DhcpApiHostNameParams
}; dwSize = sizeof(TmpBuffer);
dwError = DhcpRequestParams(
DHCPCAPI_REQUEST_SYNCHRONOUS, // Flags
NULL, // Reserved
pszAdapterName, // Adapter Name
NULL, // not using class id
NULL, // nothing to send
&RequestParams, // requesting params
(PBYTE) TmpBuf, // buffer
&dwSize, // buffer size
NULL // Request ID
); if( ERROR_MORE_DATA == dwError )
{
//
// dwSize is not large enough.
//
} if( NO_ERROR == dwError )
{ // Check if the requested option was obtained. if( DhcpApiHostNameParams.nBytesData )
{ // Check size with dwHostNameBufSize. CopyMemory(
pszHostNameBuf, DhcpApiHostNameParams.Data,
DhcpApiHostNameParams.nBytesData
);
pszHostNameBuf[DhcpApiHostNameParams.nBytesData] = '\0';
return TRUE;
}
} return FALSE;
}
Example 2: Using the DhcpRegisterParamChange function
The following code illustrates how the DhcpRegisterParamChange function can be used to keep track of host name changes:ULONG
UpdateHostNameLoop(
IN LPCWSTR pszAdapterName,
IN CHAR pszHostNameBuf[],
IN ULONG dwHostBufSize
)
{
DWORD dwError;
HANDLE hEvent;
DHCPCAPI_PARAMS DhcpApiHostNameParams = {
0, // Flags
OPTION_HOST_NAME, // OptionId
FALSE, // vendor specific?
NULL, // data filled in on return
0 // nBytes
};
DHCPCAPI_PARAMS_ARRAY DhcpApiParamsArray = {
1, // only one option to request
&DhcpApiHostNameParams
}; dwError = DhcpRegisterParamChange(
DHCPCAPI_REGISTER_HANDLE_EVENT, // Flags
NULL, // Reserved
pszAdapterName, // adapter name
NULL, // no class ID
&DhcpApiHostNameParams, // params of interest
(LPVOID)&hEvent // event handle
); if( NO_ERROR != dwError ) return dwError; // Wait on event all the time. while( WAIT_OBJECT_0 == WaitForSingleObject(hEvent, INFINITE) )
{ // Get host name and update it. ResetEvent(hEvent);
dwError = RetrieveHostName(pszAdapterName, pszHostNameBuf, dwHostBufSize );
// Ignore this error. break;
}
// Wait failed or retrieve failed? De-register the event handle. (void)DhcpDeRegisterParamChange(
DHCPCAPI_REGISTER_HANDLE_EVENT, // Flags
NULL, // Reserved
(LPVOID) hEvent // event
); return dwError;
}
Note The event handle obtained by this routine must not be closed with the CloseHandle function. It should be released using the DhcpDeRegisterParamChange function in order to avoid resource leaks; the DhcpDeRegisterParamChange function releases internal resources allocated for this notification.