将文本框的文本设置为MAC地址. [英] Setting the text of a TextBox to a MAC Address.

查看:145
本文介绍了将文本框的文本设置为MAC地址.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在用纯非托管C/C ++编写应用程序.

我正在寻找一种检索第一个本地MAC地址并将其用作文本框以及配置文件/数据库中的文本的方法.

我可以使用 GetAdaptersInfo()函数在控制台应用程序中检索MAC地址.一切都很好,但是我无法找到一种在Win32 GUI应用程序中使用此功能来设置TextBox文本的方法.

我想找到一种方法来设置变量或将MAC地址设置为 00BF67A25E88 00-BF-67-A2-5E-88 格式,这样然后可以使用以下功能显示它:

Hi All,

I am writing an application in pure unmanaged C/C++.

I am looking for a way to retrieve the first local MAC Address and use it both as text within a TextBox as well as within a configuration file/Database.

I am able to retrieve the MAC Address within a console application using the GetAdaptersInfo() function. That''s all fine and well, but i am unable to find a way to set the text of a TextBox with the use of this function inside a Win32 GUI Application.

I would like to find a way to set a variable or such with the MAC Address as either 00BF67A25E88 or 00-BF-67-A2-5E-88 formats, so that it can then be displayed with a function of the likes of:

?type? MACAddr = GetMACAddress();
SendMessage(hwndMACLabel, WM_SETTEXT, 0, (LPARAM)(?type?)MACAddr);



GetMACAddress()应该以上述格式之一返回地址,并与SendMessage()类型兼容.

请不要发布有关使用MFC,ATL,WTL,STL,VC ++.NET等的建议,因为我仅限于纯非托管C/C ++.

非常感谢您的帮助:)



GetMACAddress() should return the address in one of the above formats and be type compatible with SendMessage().

Please do not post suggestions about using MFC, ATL, WTL, STL, VC++.NET, etc. as i limited to pure unmanaged C/C++.

Your help is very much appreciated :)

推荐答案

尝试类似的方法;

Try something like this;

char total[100] = "";
char part[4];
for(int i=0;i<padapter->AddressLength;i++){
    sprintf(part,"%02x-",padapter->Address[i]);
    strcat(total,part);
}
// Remove the last ''-'', we added one too many.
total[strlen(total)-1] = 0;



变量total现在具有请求的字符串,您可以将其传递给SendMessage.

由于某种原因,我的前置块使用了所有错误的颜色,但是代码似乎并没有错.也许其他人可以帮助解决此问题.



The variable total now has the requested string, you can pass that to SendMessage.

edit: For some reason my pre-block uses all the wrong colours, but the code doesn''t seem to be wrong. Maybe someone else can help fix this.


pFirstAddr指向的字符串是一个局部变量,在函数退出时不会保留.

为了解决这个问题,您可以将total变量设为静态,在这种情况下,它将在函数退出后保留.
The string pointed to by pFirstAddr is a local variable, it won''t be preserved when your function exits.

To solve this, you could make the total variable static, in which case it will be preserved after the function exits.


您对非托管C/C ++的含义感到困惑.
MFC,ATL,WTL和STL都是不受管理的C ++.

您可能只想使用C运行时函数,在这种情况下将需要更多工作.

要获取MAC地址,您需要-
You''re confused with the meaning of unmanaged C/C++.
MFC, ATL, WTL, and STL are all unmanaged C++.

You probably only want to use the C runtime functions in which case it would be a little more work.

To get the MAC address you would need to do -
BYTE MACAddr[MAX_ADAPTER_ADDRESS_LENGTH];


该函数的签名必须为-


The signature of the function must be -

void GetMACAddress(BYTE* MAXAddr);


并且该函数必须调用-


And the function must be called -

GetMACAddress(MACAddr);


现在,您可以在通过WM_SETTEXT调用SendMesage的过程中使用BYTE数组.


Now you can use the BYTE array in a call to SendMesage with WM_SETTEXT.


这篇关于将文本框的文本设置为MAC地址.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆