您的位置:程序门 -> .net技术 -> c#



用c#编写的读取客户端mac地址的activex控件,在ie中运行时出现权限错误,无从下手,求教


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


用c#编写的读取客户端mac地址的activex控件,在ie中运行时出现权限错误,无从下手,求教
发表于:2007-08-31 18:05:29 楼主
我用c#写了一个读取客户端mac地址的控件,在winform下运行正常,搬到ie里就出现权限错误:

请求“system.security.permissions.registrypermission,   mscorlib,   version=2.0.0.0,   culture=neutral,   publickeytoken=b77a5c561934e089”类型的权限已失败。

窗体上只有一个文本框,用来显示mac地址,源代码如下:
using   system;
using   system.net;  
using   system.collections.generic;
using   system.componentmodel;
using   system.drawing;
using   system.data;
using   system.text;
using   system.windows.forms;
using   system.runtime.interopservices;
using   microsoft.win32;
using   system.management;
using   system.diagnostics;
using   system.text.regularexpressions;

namespace   wmactivex
{
        [guid( "d3722706-9591-4449-b8aa-f35ee65fb9b0 ")]
        public   partial   class   hardware   :   usercontrol
        {
                public   hardware()
                {
                        initializecomponent();
                        textbox1.text   =   getlocalmac();
                }

                public   static   string   getlocalmac()
                {
                        list <string>   netcardlist   =   getnetcardlist();
                        list <string> .enumerator   enumnetcard   =   netcardlist.getenumerator();

                        string   macaddr   =   string.empty;
                        while   (enumnetcard.movenext())
                        {
                                macaddr   =   getphysicaladdr(enumnetcard.current);
                                if   (macaddr   !=   string.empty)
                                {
                                        break;
                                }
                        }
                        return   macaddr;
                }
                public   static   list <string>   getnetcardlist()
                {
                        list <string>   cardlist   =   new   list <string> ();
                        try
                        {
                                //请求“system.security.permissions.registrypermission,   mscorlib,   version=2.0.0.0,   culture=neutral,   publickeytoken=b77a5c561934e089”类型的权限已失败。
                                registrykey   regnetcards   =   registry.localmachine.opensubkey(win32utils.reg_net_cards_key);
                                if   (regnetcards   !=   null)
                                {
                                        string[]   names   =   regnetcards.getsubkeynames();
                                        registrykey   subkey   =   null;
                                        foreach   (string   name   in   names)
                                        {
                                                subkey   =   regnetcards.opensubkey(name);
                                                if   (subkey   !=   null)
                                                {
                                                        object   o   =   subkey.getvalue( "servicename ");
                                                        if   (o   !=   null)
                                                        {
                                                                cardlist.add(o.tostring());
                                                        }
                                                }
                                        }
                                }
                        }
                        catch   (exception   ex)
                        {
                        }

                        return   cardlist;
                }
                private   static   string   getphysicaladdr(string   cardid)
                {
                        string   macaddress   =   string.empty;
                        uint   device   =   0;
                        try
                        {
                                string   drivename   =   "\\\\.\\ "   +   cardid;
                                device   =   win32utils.createfile(drivename,
                                win32utils.generic_read   ¦   win32utils.generic_write,
                                win32utils.file_share_read   ¦   win32utils.file_share_write,
                                0,   win32utils.open_existing,   0,   0);
                                if   (device   !=   win32utils.invalid_handle_value)
                                {
                                        byte[]   outbuff   =   new   byte[6];
                                        uint   bytrv   =   0;
                                        int   intbuff   =   win32utils.permanent_address;

                                        if   (0   !=   win32utils.deviceiocontrol(device,   win32utils.ioctl_ndis_query_global_stats,
                                        ref   intbuff,   4,   outbuff,   6,   ref   bytrv,   0))
                                        {
                                                string   temp   =   string.empty;
                                                foreach   (byte   b   in   outbuff)
                                                {
                                                        temp   =   convert.tostring(b,   16).padleft(2,   '0 ');
                                                        macaddress   +=   temp;
                                                        temp   =   string.empty;
                                                }
                                        }
                                }
                        }
                        finally
                        {
                                if   (device   !=   0)
                                {
                                        win32utils.closehandle(device);
                                }
                        }

                        return   macaddress;
                }
        }
        #region   win32utils
        public   class   win32utils
        {
                public   const   string   reg_net_cards_key   =   @ "software\microsoft\windows   nt\currentversion\networkcards ";
                public   const   uint   generic_read   =   0x80000000;
                public   const   uint   generic_write   =   0x40000000;
                public   const   uint   file_share_read   =   0x00000001;
                public   const   uint   file_share_write   =   0x00000002;
                public   const   uint   open_existing   =   3;
                public   const   uint   invalid_handle_value   =   0xffffffff;
                public   const   uint   ioctl_ndis_query_global_stats   =   0x00170002;
                public   const   int   permanent_address   =   0x01010101;

                [dllimport( "kernel32.dll ")]
                public   static   extern   int   closehandle(uint   hobject);

                [dllimport( "kernel32.dll ")]
                public   static   extern   int   deviceiocontrol(uint   hdevice,
                                                                                                    uint   dwiocontrolcode,
                                                                                                    ref   int   lpinbuffer,
                                                                                                    int   ninbuffersize,
                                                                                                    byte[]   lpoutbuffer,
                                                                                                    int   noutbuffersize,
                                                                                                    ref   uint   lpbytesreturned,
                                                                                                    int   lpoverlapped);

                [dllimport( "kernel32.dll ")]
                public   static   extern   uint   createfile(string   lpfilename,
                                                                                            uint   dwdesiredaccess,
                                                                                            uint   dwsharemode,
                                                                                            int   lpsecurityattributes,
                                                                                            uint   dwcreationdisposition,
                                                                                            uint   dwflagsandattributes,
                                                                                            int   htemplatefile);

        }
        #endregion
}
发表于:2007-08-31 18:07:551楼 得分:0
dll文件与网页放在同一目录下的,网页html代码:
<html>
<head>
        <title> untitled   page </title>
</head>
<body>
<object   id= "hw "   classid= "wmactivex.dll#wmactivex.hardware "   width= "400 "   height= "300 "> </object>
</body>
</html>
发表于:2007-08-31 18:30:212楼 得分:0
用c#写activex控件?太搞笑了吧...客户端要就是不装.net   framework你能怎么办?
发表于:2007-09-02 12:41:363楼 得分:0
只要实现功能就行了,客户端没有framework,装一个就行了
发表于:2007-09-02 16:28:054楼 得分:0
哈哈,如果是这样你还不入直接写net装配件了
net装配件也是可以直接再html页面里用 <object> 标签引入的

参考http://www.sifung.com/pages/879.shtm
发表于:2007-09-02 16:36:185楼 得分:0
其实如果是编写控件取得mac地址,也用不着这么麻烦的手段

反正你的控件要在客户端运行,那么就直接启动cmd命令的ipconfig   -all命令,程序接收回显的值就成

发表于:2007-09-03 17:38:396楼 得分:0
事实上我们总是讨论到别的方面上.而我问的只是个权限问题.

回vrhero(我是真小人/最烦伪君子...)
如果我们需要完全考虑所有人的客户端环境,   那还是写个c/s的,放在html上让别人下载,这样才能解决所有问题.现在各银行网站的activex在firefox下均表现不好.

回wanghui0380(放歌)
你的这个装配件,其实我在写这些代码之前就在用.   只不过最后我稍微改了一下,编译成activex而已.   我认为他仍然存在权限问题,   当然这个可以测试一下.
而用cmd去取得mac地址,我不知道你是否真的做过,   在winform下去跑后台cmd命令没有问题,并不一定在activex里能跑吧,   当然这个也需要验证.


不得已,只好去挖了个vb6出来,写了几千行代码,从获取mac,到cpuid和硬盘物理id,   写得很不爽,但也算解决问题.

还是谢谢各位


快速检索

最新资讯
热门点击