0
    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

4 回答 4

4

IPM_GETADDRESS返回地址为 a 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在上面的示例中包含返回的四边形的数量),因此您需要在现实生活中考虑这一点。

于 2012-09-02T23:15:57.310 回答
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

于 2012-09-02T23:19:50.713 回答
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 的 oder 更改为 FOURTH - 不确切知道:-) 请参见此处:http: //msdn.microsoft.com/en-us/library/windows/desktop/bb761378%28v=vs.85% 29.aspx

于 2012-09-02T23:31:49.330 回答
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
    }
}
于 2016-04-24T15:08:12.793 回答