{"id":530,"date":"2012-06-21T23:14:39","date_gmt":"2012-06-21T15:14:39","guid":{"rendered":"http:\/\/newbiecoder.0ginr.com\/blog\/?p=530"},"modified":"2012-06-29T01:54:11","modified_gmt":"2012-06-28T17:54:11","slug":"%e6%9f%a5%e8%af%a2%e7%bd%91%e5%8d%a1%e7%9a%84%e7%89%a9%e7%90%86%e5%9c%b0%e5%9d%80","status":"publish","type":"post","link":"https:\/\/newbiecoder.0ginr.com\/blog\/?p=530","title":{"rendered":"\u67e5\u8be2\u7f51\u5361\u7684\u7269\u7406\u5730\u5740"},"content":{"rendered":"<p>\u5305\u62ec\u51fa\u5382\u65f6\u7684\u5730\u5740\uff0c\u4ee5\u53ca\u7ba1\u7406\u5458\u6307\u5b9a\u7684\u5730\u5740\uff08\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u5219\u4e0e\u51fa\u5382\u5730\u5740\u76f8\u540c\uff09\u3002<\/p>\n<p>\u7528 C++ \u7f16\u8bd1\u7684\u8bdd\u8981\u7ed9\u51fd\u6570\u539f\u578b\u58f0\u660e\u90fd\u52a0\u4e0a EXTERN_C\uff08\u6216\u8005 extern &#8220;C&#8221;\uff09\u3002\uff08\u6216\u8005\u5e72\u8106\u7528 EXTERN_C \u628a\u6240\u6709\u7684\u58f0\u660e\u90fd\u5305\u62ec\u8d77\u6765\uff08 EXTERN_C { &#8230; }\u3002\uff09\u3002\uff09<\/p>\n<p>\uff08NDMP18 \u5c55\u793a\u4e86\u5f53\u51fa\u5382 MAC \u8ddf\u7ba1\u7406\u5458\u6307\u5b9a\u7684 MAC \u4e0d\u540c\u65f6\u7684\u8f93\u51fa\u3002\uff09<\/p>\n<pre>We're checking device \\Device\\NDMP1:\r\nIts permanent physical address is: 12-34-56-78-90-12\r\nIts current physical address is: 12-34-56-78-90-12\r\n\r\nWe're checking device \\Device\\NDMP2:\r\nIts permanent physical address is: 11-22-33-44-55-66\r\nIts current physical address is: 11-22-33-44-55-66\r\n\r\nWe're checking device \\Device\\NDMP3:\r\nWe failed to open that device, status = 0xC0000001\r\n\r\nWe're checking device \\Device\\NDMP4:\r\nWe failed to query its permanent physical address, status = 0xC000000D\r\nWe failed to query its current physical address, status = 0xC000000D\r\n\r\nWe're checking device \\Device\\NDMP5:\r\nWe failed to query its permanent physical address, status = 0xC000000D\r\nWe failed to query its current physical address, status = 0x80000005\r\n\r\nWe're checking device \\Device\\NDMP6:\r\nIts permanent physical address is: AA-BB-CC-DD-EE-FF\r\nIts current physical address is: AA-BB-CC-DD-EE-FF\r\n\r\nWe're checking device \\Device\\NDMP7:\r\nIts permanent physical address is: AB-CD-EF-AB-CD-EF\r\nIts current physical address is: AB-CD-EF-AB-CD-EF\r\n\r\nWe're checking device \\Device\\NDMP8:\r\nIts permanent physical address is: 00-00-00-00-00-00\r\nIts current physical address is: 00-00-00-00-00-00\r\n\r\nWe're checking device \\Device\\NDMP9:\r\nWe failed to open that device, status = 0xC0000001\r\n\r\nWe're checking device \\Device\\NDMP10:\r\nWe failed to query its permanent physical address, status = 0xC000000D\r\nWe failed to query its current physical address, status = 0x80000005\r\n\r\nWe're checking device \\Device\\NDMP11:\r\nWe failed to query its permanent physical address, status = 0xC000000D\r\nWe failed to query its current physical address, status = 0x80000005\r\n\r\nWe're checking device \\Device\\NDMP12:\r\nWe failed to query its permanent physical address, status = 0xC000000D\r\nWe failed to query its current physical address, status = 0x80000005\r\n\r\nWe're checking device \\Device\\NDMP13:\r\nWe failed to query its permanent physical address, status = 0xC000000D\r\nWe failed to query its current physical address, status = 0x80000005\r\n\r\nWe're checking device \\Device\\NDMP14:\r\nWe failed to query its permanent physical address, status = 0xC000000D\r\nWe failed to query its current physical address, status = 0x80000005\r\n\r\nWe're checking device \\Device\\NDMP15:\r\nIts permanent physical address is: 12-34-56-78-9A-BC\r\nIts current physical address is: 12-34-56-78-9A-BC\r\n\r\nWe're checking device \\Device\\NDMP16:\r\nWe failed to open that device, status = 0xC0000001\r\n\r\nWe're checking device \\Device\\NDMP17:\r\nWe failed to open that device, status = 0xC0000001\r\n\r\nWe're checking device \\Device\\NDMP18:\r\nIts permanent physical address is: FE-DC-BA-AB-CD-EF\r\nIts current physical address is: AB-CD-EF-FE-DC-BA\r\n\r\nWe're checking device \\Device\\NDMP19:\r\nWe failed to query its permanent physical address, status = 0xC000000D\r\nWe failed to query its current physical address, status = 0xC000000D\r\n\r\n\u8bf7\u6309\u4efb\u610f\u952e\u7ee7\u7eed. . .<\/pre>\n<p><code>#include <windows.h><br \/>\n#include <stdio.h><\/p>\n<p>\/\/<br \/>\n\/\/ \u7528\u5230\u7684\u5b8f \/ \u4f8b\u7a0b\u7b49\u7684\u58f0\u660e\u3002<br \/>\n\/\/<\/p>\n<p>#define IOCTL_NDIS_QUERY_GLOBAL_STATS 0x00170002<\/p>\n<p>#define OID_802_3_PERMANENT_ADDRESS 0x01010101<br \/>\n#define OID_802_3_CURRENT_ADDRESS 0x01010102<\/p>\n<p>#define STATUS_OBJECT_NAME_NOT_FOUND ((NTSTATUS)0xC0000034L)<br \/>\n#define NT_SUCCESS(Status) (((NTSTATUS)(Status)) >= 0)<\/p>\n<p>typedef struct _UNICODE_STRING<br \/>\n{<br \/>\n    USHORT Length;<br \/>\n    USHORT MaximumLength;<br \/>\n    PWSTR Buffer;<br \/>\n} UNICODE_STRING, *PUNICODE_STRING;<\/p>\n<p>NTSYSAPI<br \/>\nVOID<br \/>\nNTAPI<br \/>\nRtlInitUnicodeString(<br \/>\n    IN OUT PUNICODE_STRING DestinationString,<br \/>\n    IN PCWSTR SourceString<br \/>\n);<\/p>\n<p>typedef struct _OBJECT_ATTRIBUTES<br \/>\n{<br \/>\n    ULONG Length;<br \/>\n    HANDLE RootDirectory;<br \/>\n    PUNICODE_STRING ObjectName;<br \/>\n    ULONG Attributes;<br \/>\n    PVOID SecurityDescriptor;<br \/>\n    PVOID SecurityQualityOfService;<br \/>\n} OBJECT_ATTRIBUTES, *POBJECT_ATTRIBUTES;<\/p>\n<p>#define OBJ_CASE_INSENSITIVE 0x00000040L<\/p>\n<p>#define InitializeObjectAttributes(p,n,a,r,s) { \\<br \/>\n    (p)->Length = sizeof(OBJECT_ATTRIBUTES);    \\<br \/>\n    (p)->RootDirectory = (r);                   \\<br \/>\n    (p)->Attributes = (a);                      \\<br \/>\n    (p)->ObjectName = (n);                      \\<br \/>\n    (p)->SecurityDescriptor = (s);              \\<br \/>\n    (p)->SecurityQualityOfService = NULL;       \\<br \/>\n}<\/p>\n<p>typedef struct _IO_STATUS_BLOCK<br \/>\n{<br \/>\n    union<br \/>\n    {<br \/>\n        NTSTATUS Status;<br \/>\n        PVOID Pointer;<br \/>\n    };<br \/>\n    ULONG_PTR Information;<br \/>\n} IO_STATUS_BLOCK, *PIO_STATUS_BLOCK;<\/p>\n<p>#define FILE_OPEN 0x00000001<\/p>\n<p>NTSYSAPI<br \/>\nNTSTATUS<br \/>\nNTAPI<br \/>\nZwCreateFile(<br \/>\n    OUT PHANDLE FileHandle,<br \/>\n    IN ACCESS_MASK DesiredAccess,<br \/>\n    IN POBJECT_ATTRIBUTES ObjectAttributes,<br \/>\n    OUT PIO_STATUS_BLOCK IoStatusBlock,<br \/>\n    IN PLARGE_INTEGER AllocationSize OPTIONAL,<br \/>\n    IN ULONG FileAttributes,<br \/>\n    IN ULONG ShareAccess,<br \/>\n    IN ULONG CreateDisposition,<br \/>\n    IN ULONG CreateOptions,<br \/>\n    IN PVOID EaBuffer OPTIONAL,<br \/>\n    IN ULONG EaLength<br \/>\n);<\/p>\n<p>NTSYSAPI<br \/>\nNTSTATUS<br \/>\nNTAPI<br \/>\nZwDeviceIoControlFile(<br \/>\n    IN HANDLE DeviceHandle,<br \/>\n    IN HANDLE Event OPTIONAL,<br \/>\n    IN PVOID UserApcRoutine OPTIONAL,<br \/>\n    IN PVOID UserApcContext OPTIONAL,<br \/>\n    OUT PIO_STATUS_BLOCK IoStatusBlock,<br \/>\n    IN ULONG IoControlCode,<br \/>\n    IN PVOID InputBuffer,<br \/>\n    IN ULONG InputBufferSize,<br \/>\n    OUT PVOID OutputBuffer,<br \/>\n    IN ULONG OutputBufferSize<br \/>\n);<\/p>\n<p>NTSYSAPI<br \/>\nNTSTATUS<br \/>\nNTAPI<br \/>\nZwClose(<br \/>\n    IN HANDLE Handle<br \/>\n);<\/p>\n<p>\/\/<br \/>\n\/\/ MAC \u5730\u5740\u957f\u5ea6\u3002\uff086\u5b57\u8282\uff09<br \/>\n\/\/<\/p>\n<p>#define PHYSICAL_ADDRESS_LENGTH 6<\/p>\n<p>\/\/<br \/>\n\/\/ \u6253\u5f00\u8bbe\u5907\u3002<br \/>\n\/\/<\/p>\n<p>NTSTATUS ObtainDeviceHandle(<br \/>\n\t__out PHANDLE DeviceHandle,<br \/>\n\t__in PUNICODE_STRING DeviceName<br \/>\n\t)<br \/>\n{<br \/>\n\tOBJECT_ATTRIBUTES ObjectAttributes;<br \/>\n\tIO_STATUS_BLOCK IoStatusBlock;<br \/>\n\tHANDLE LocalDeviceHandle;<br \/>\n\tNTSTATUS Status;<\/p>\n<p>\t\/\/<br \/>\n\t\/\/ \u521d\u59cb\u5316 OBJECT_ATTRIBUTES\u3002<br \/>\n\t\/\/<\/p>\n<p>\tInitializeObjectAttributes(&ObjectAttributes, DeviceName, OBJ_CASE_INSENSITIVE,<br \/>\n\t\tNULL, NULL);<\/p>\n<p>\t\/\/<br \/>\n\t\/\/ \u5c1d\u8bd5\u6253\u5f00\u8bbe\u5907\u3002<br \/>\n\t\/\/<\/p>\n<p>\tStatus = ZwCreateFile(&LocalDeviceHandle, FILE_READ_ACCESS, &ObjectAttributes,<br \/>\n\t\t&IoStatusBlock, NULL, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, FILE_OPEN, 0, NULL, 0);<\/p>\n<p>\tif (!NT_SUCCESS(Status)) {<br \/>\n\t\treturn Status;<br \/>\n\t}<\/p>\n<p>\t\/\/<br \/>\n\t\/\/ \u5c06\u53e5\u67c4\u8fd4\u56de\u7ed9\u8c03\u7528\u65b9\u3002<br \/>\n\t\/\/<\/p>\n<p>\t*DeviceHandle = LocalDeviceHandle;<\/p>\n<p>\treturn Status;<br \/>\n}<\/p>\n<p>\/\/<br \/>\n\/\/ \u83b7\u53d6\u7f51\u5361\u4fe1\u606f\u3002<br \/>\n\/\/<\/p>\n<p>NTSTATUS QueryNdisDeviceInformation(<br \/>\n\t__in HANDLE DeviceHandle,<br \/>\n\t__in ULONG ObjectIdentifier,<br \/>\n\t__out_bcount_opt(OutputBufferSize) PVOID OutputBuffer,<br \/>\n\t__in SIZE_T OutputBufferSize<br \/>\n\t)<br \/>\n{<br \/>\n\tIO_STATUS_BLOCK IoStatusBlock;<br \/>\n\tNTSTATUS Status;<\/p>\n<p>\t\/\/<br \/>\n\t\/\/ \u53d1\u8d77\u8bf7\u6c42\u3002<br \/>\n\t\/\/<\/p>\n<p>\tStatus = ZwDeviceIoControlFile(DeviceHandle, NULL, NULL, NULL, &IoStatusBlock,<br \/>\n\t\tIOCTL_NDIS_QUERY_GLOBAL_STATS, (PVOID)&ObjectIdentifier, sizeof(ULONG),<br \/>\n\t\tOutputBuffer, OutputBufferSize);<\/p>\n<p>\tif (!NT_SUCCESS(Status)) {<br \/>\n\t\treturn Status;<br \/>\n\t}<\/p>\n<p>\treturn Status;<br \/>\n}<\/p>\n<p>\/\/<br \/>\n\/\/ \u83b7\u53d6\u7f51\u5361 MAC \u5730\u5740\u3002<br \/>\n\/\/<br \/>\n\/\/ \u7f51\u5361 MAC \u5730\u5740\u53ef\u4ee5\u662f\u51fa\u5382\u65f6\u7684 MAC \u5730\u5740\uff0c\u4e5f\u53ef\u4ee5\u662f\u7ba1\u7406\u5458\u6307\u6d3e\u7684 MAC \u5730\u5740\u3002<br \/>\n\/\/<br \/>\n\/\/ \u8f93\u51fa\u53c2\u6570 PhysicalAddress \u81f3\u5c11\u9700\u8981 PHYSICAL_ADDRESS_LENGTH \u4e2a\u5b57\u8282\u5927\u5c0f\u3002<br \/>\n\/\/\uff08MAC \u5730\u5740\u5360 PHYSICAL_ADDRESS_LENGTH \u5b57\u8282\u3002\uff09<br \/>\n\/\/<\/p>\n<p>NTSTATUS QueryNdisDevicePhysicalAddress(<br \/>\n\t__out PUCHAR PhysicalAddress,<br \/>\n\t__in HANDLE DeviceHandle,<br \/>\n\t__in BOOLEAN PermanentPhysicalAddress<br \/>\n\t)<br \/>\n{<br \/>\n\t\/\/<br \/>\n\t\/\/ \u5411\u8bbe\u5907\u67e5\u8be2\u7f51\u5361\u5730\u5740\u3002<br \/>\n\t\/\/<\/p>\n<p>\treturn QueryNdisDeviceInformation(DeviceHandle,<br \/>\n\t\t(PermanentPhysicalAddress ? OID_802_3_PERMANENT_ADDRESS : OID_802_3_CURRENT_ADDRESS),<br \/>\n\t\t(PVOID)PhysicalAddress, PHYSICAL_ADDRESS_LENGTH);<br \/>\n}<\/p>\n<p>\/\/<br \/>\n\/\/ \u663e\u793a\u7f51\u5361\u4fe1\u606f\u3002<br \/>\n\/\/<\/p>\n<p>VOID DumpNdisDevicePhysicalAddress(<br \/>\n\t__in PUNICODE_STRING DeviceName<br \/>\n\t)<br \/>\n{<br \/>\n\tHANDLE DeviceHandle;<br \/>\n\tBOOLEAN PermanentPhysicalAddressCandidates[] = {TRUE, FALSE};<br \/>\n\tLPCSTR PhysicalAddressDescriptions[] = {\"permanent physical address\", \"current physical address\"};<br \/>\n\tULONG Index;<br \/>\n\tNTSTATUS Status;<\/p>\n<p>\t\/\/<br \/>\n\t\/\/ \u6253\u5f00\u7f51\u5361\u8bbe\u5907\u3002<br \/>\n\t\/\/<\/p>\n<p>\tStatus = ObtainDeviceHandle(&DeviceHandle, DeviceName);<\/p>\n<p>\tif (!NT_SUCCESS(Status)) {<br \/>\n\t\tprintf(\"We failed to open that device, status = 0x%08X\\n\", Status);<\/p>\n<p>\t\treturn;<br \/>\n\t}<\/p>\n<p>\t\/\/<br \/>\n\t\/\/ \u5bf9\u6240\u6709 PermanentPhysicalAddress \u53ef\u80fd\u7684\u53d6\u503c\u5206\u522b\u67e5\u8be2\u5176\u5bf9\u5e94\u7684\u7269\u7406\u5730\u5740\u3002<br \/>\n\t\/\/<\/p>\n<p>\tfor (Index = 0; Index < _countof(PermanentPhysicalAddressCandidates); Index++) {\n\t\tUCHAR PhysicalAddress[PHYSICAL_ADDRESS_LENGTH];\n\t\tULONG AddressIndex;\n\n\t\t\/\/\n\t\t\/\/ \u67e5\u8be2\u7269\u7406\u5730\u5740\u3002\n\t\t\/\/\n\n\t\tStatus = QueryNdisDevicePhysicalAddress(PhysicalAddress, DeviceHandle, \n\t\t\tPermanentPhysicalAddressCandidates[Index]);\n\n\t\tif (!NT_SUCCESS(Status)) {\n\t\t\tprintf(\"We failed to query its %s, status = 0x%08X\\n\", PhysicalAddressDescriptions[Index],\n\t\t\t\tStatus);\n\n\t\t\tcontinue;\n\t\t}\n\n\t\t\/\/\n\t\t\/\/ \u8f93\u51fa\u7269\u7406\u5730\u5740\u3002\n\t\t\/\/\n\n\t\tprintf(\"Its %s is: \", PhysicalAddressDescriptions[Index]);\n\n\t\tfor (AddressIndex = 0; AddressIndex < _countof(PhysicalAddress); AddressIndex++) {\n\n\t\t\tif (AddressIndex) {\n\t\t\t\tprintf(\"-\");\n\t\t\t}\n\n\t\t\tprintf(\"%02X\", PhysicalAddress[AddressIndex]);\n\t\t}\n\n\t\tprintf(\"\\n\");\n\t}\n\n\t\/\/\n\t\/\/ \u91ca\u653e\u8bbe\u5907\u3002\n\t\/\/\n\n\tZwClose(DeviceHandle);\n\n\treturn;\n}\n\n\/\/\n\/\/ \u5224\u65ad\u8bbe\u5907\u662f\u5426\u5b58\u5728\u3002\n\/\/\n\nBOOLEAN IsDeviceExists(\n\t__in PUNICODE_STRING DeviceName\n\t)\n{\n\tHANDLE DeviceHandle;\n\tNTSTATUS Status;\n\n\t\/\/\n\t\/\/ \u5c1d\u8bd5\u6253\u5f00\u8bbe\u5907\u3002\n\t\/\/\n\n\tStatus = ObtainDeviceHandle(&#038;DeviceHandle, DeviceName);\n\n\tif (NT_SUCCESS(Status)) {\n\t\tZwClose(DeviceHandle);\n\t}\n\n\treturn Status != STATUS_OBJECT_NAME_NOT_FOUND;\n}\n\nint main() {\n\tconst static WCHAR DeviceNameFormat[] = L\"\\\\Device\\\\NDMP%d\";\n\tconst static ULONG MaximumDeviceIndex = 10000;\n\n\tWCHAR DeviceNameBuffer[sizeof(DeviceNameFormat) + 12]; \/\/ ULONG \u8f6c\u6362\u4e3a\u5341\u8fdb\u5236\u4e0d\u4f1a\u8d85\u8fc7 12 \u4f4d\u3002\n\tULONG DeviceIndex;\n\n\t\/\/\n\t\/\/ \u679a\u4e3e\u8bbe\u5907\u3002\n\t\/\/\n\n\tDeviceIndex = 1; \n\t\n\tfor (DeviceIndex = 1; DeviceIndex < MaximumDeviceIndex; DeviceIndex++) {\n\t\tUNICODE_STRING DeviceName;\n\t\t\n\t\t\/\/\n\t\t\/\/ \u6784\u9020\u8bbe\u5907\u540d\u3002\n\t\t\/\/\n\n\t\twsprintfW(DeviceNameBuffer, DeviceNameFormat, DeviceIndex);\n\n\t\tRtlInitUnicodeString(&#038;DeviceName, DeviceNameBuffer);\n\n\t\tif (!IsDeviceExists(&#038;DeviceName)) {\n\t\t\tcontinue;\n\t\t}\n\n\t\t\/\/\n\t\t\/\/ \u67e5\u8be2\u8bbe\u5907\u7684\u7269\u7406\u5730\u5740\u3002\n\t\t\/\/\n\n\t\tprintf(\"We're checking device %ls:\\n\", DeviceName.Buffer);\n\n\t\tDumpNdisDevicePhysicalAddress(&#038;DeviceName);\n\n\t\tprintf(\"\\n\");\n\t}\n\n\treturn 0;\n}<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5305\u62ec\u51fa\u5382\u65f6\u7684\u5730\u5740\uff0c\u4ee5\u53ca\u7ba1\u7406\u5458\u6307\u5b9a\u7684\u5730\u5740\uff08\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u5219\u4e0e\u51fa\u5382\u5730\u5740\u76f8\u540c\uff09\u3002 \u7528 C++ \u7f16\u8bd1\u7684\u8bdd\u8981\u7ed9\u51fd\u6570\u539f\u578b\u58f0\u660e [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-530","post","type-post","status-publish","format-standard","hentry","category-1"],"_links":{"self":[{"href":"https:\/\/newbiecoder.0ginr.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/530","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/newbiecoder.0ginr.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/newbiecoder.0ginr.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/newbiecoder.0ginr.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/newbiecoder.0ginr.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=530"}],"version-history":[{"count":10,"href":"https:\/\/newbiecoder.0ginr.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/530\/revisions"}],"predecessor-version":[{"id":557,"href":"https:\/\/newbiecoder.0ginr.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/530\/revisions\/557"}],"wp:attachment":[{"href":"https:\/\/newbiecoder.0ginr.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=530"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/newbiecoder.0ginr.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=530"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/newbiecoder.0ginr.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=530"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}