从c中的IP地址控件中检索文本

时间:2012-09-02 22:44:07

标签: c winapi ip-address

    SendMessage(hIpControl, IPM_GETADDRESS, 0, (LPARAM)lpIPAddress);
    TextOut(hdc, 10, 10, (LPCWSTR)lpIPAddress , 20);
//where static LPDWORD lpIPAddress = (LPDWORD)malloc(sizeof(LPDWORD));

和 hIpControl是IP控件的句柄。当我运行它时,Text Out只显示随机的中文符号和内容。如何安全地检索IP地址并将其存储在字符串中。我需要在一个字符串中输入它:“192.168.1.4”,以便我的应用程序正常工作。

4 个答案:

答案 0 :(得分:4)

IPM_GETADDRESS将地址作为DWORD返回,而不是字符串。

DWORD dwAddr;
int iCount = (int)SendMessage(hwndCtl, IPM_GETADDRESS, 0, (LPARAM)&dwAddr);
wchar_t wchAddr[20];
StringCchPrintf(wchAddr, _countof(wchAddr), L"%ld.%ld.%ld.%ld",
    FIRST_IPADDRESS(dwAddr),
    SECOND_IPADDRESS(dwAddr),
    THIRD_IPADDRESS(dwAddr),
    FOURTH_IPADDRESS(dwAddr));

请注意,用户可以输入少于四个四边形(上例中的iCount包含返回的四边形数量),因此您需要在现实生活中考虑这一点。

答案 1 :(得分:3)

这会将所有4个字段转换为字符串。

DWORD lpIPAddress;

LRESULT SM = SendMessage(hWndIPAddress, IPM_GETADDRESS, 0, (LPARAM)(LPDWORD)&lpIPAddress);

BYTE IPPart1 = FIRST_IPADDRESS((LPARAM)lpIPAddress);
BYTE IPPart2 = SECOND_IPADDRESS((LPARAM)lpIPAddress);
BYTE IPPart3 = THIRD_IPADDRESS((LPARAM)lpIPAddress);
BYTE IPPart4 = FOURTH_IPADDRESS((LPARAM)lpIPAddress);


AnsiString ip = AnsiString(IPPart1) + AnsiString(".") +
                AnsiString(IPPart2) + AnsiString(".") +
                AnsiString(IPPart3) + AnsiString(".") +
                AnsiString(IPPart4);

ShowMessage("IP Address " + ip);

参考:http://www.functionx.com/cppbuilder/controls/ipaddress.htm

答案 2 :(得分:2)

DWORD IPAddress;
SendMessage(hIpControl, IPM_GETADDRESS, 0, (LPARAM)&IPAddress);
WCHAR Buff[500];
sprintf(Buff, "%d.%d.%d.%d", (int)FIRST_IPADDRESS(IPAddress), (int)SECOND_IPADDRESS(IPAddress), (int)THIRD_IPADDRESS(IPAddress), (int)FOURTH_IPADDRESS(IPAddress));
TextOut(hdc, 10, 10, Buff , 20);

或许你必须将FIRST的顺序改为第四 - 不确切地知道:-) 看这里: http://msdn.microsoft.com/en-us/library/windows/desktop/bb761378%28v=vs.85%29.aspx

答案 3 :(得分:0)

这里的一个简单函数应该可以正常工作(就像它对我一样):

void getIpAddress(HWND hDlg)
{
    int len = GetWindowTextLength(GetDlgItem(hDlg, IDC_IPADDRESS_INPUT));
    if (len > 0)
    {
        char *buffer;
        buffer = (char*)GlobalAlloc(GPTR, len + 1);
        GetDlgItemTextA(hDlg, IDC_IPADDRESS_INPUT, buffer, len + 1);
        MessageBoxA(0, buffer, "Ip Address", MB_OK);
    }
    else
    {
        // Error handling here
    }
}