【发布时间】:2014-09-30 11:53:31
【问题描述】:
我正在尝试创建一个 C++/CLR 项目以使用 IPHlpApi.dll 的 SendARP 方法。该函数需要一个指向 ULONG 变量数组的指针。我不确定如何实现或解决它。
MSDN 信息:
DWORD SendARP(
_In_ IPAddr DestIP,
_In_ IPAddr SrcIP,
_Out_ PULONG pMacAddr,
_Inout_ PULONG PhyAddrLen
);
pMacAddr [out]
A pointer to an array of ULONG variables. This array must have at least two ULONG elements to hold an Ethernet or token ring physical address. The first six bytes of this array receive the physical address that corresponds to the IPv4 address specified by the DestIP parameter.
这就是我的包装类中的内容。 IPAddr 和 PULONG 都是 typedef unsigned long 以便让 VB.Net 识别我已将它们转换为 ULONG 的类型。据我所知,其他 3 个参数都很好,我只需要更改 pMacAddr 并以某种方式从 VB 传递一个指针(或找到解决方法)。
ULONG SendARPRequest(ULONG DestIP, ULONG SrcIP, ULONG pMacAddr, ULONG PhyAddrLen) { return ULONG (m_nativeClass->SendARPRequest(IPAddr (DestIP), IPAddr(SrcIP), PULONG(pMacAddr), PULONG (PhyAddrLen))); };
如果您需要更多我的代码,请询问。我不想不必要地填补这个职位。提前致谢。
编辑:
Wrapper.h
#include <Windows.h>
#include <winerror.h>
#include <IPHlpApi.h>
#include "IPHelper.h"
#pragma comment(lib, "IPHLPAPI.lib")
using namespace System;
public ref class NativeClassWrapper {
IPHelper* m_nativeClass;
public:
static const ULONG IPH_NO_ERROR = NO_ERROR;
static const ULONG IPH_ERROR_BAD_NET_NAME = ERROR_BAD_NET_NAME; // The network name cannot be found. This error is returned on Windows Vista and later when an ARP reply to the SendARP request was not received. This error occurs if the destination IPv4 address could not be reached because it is not on the same subnet or the destination computer is not operating.
static const ULONG IPH_ERROR_BUFFER_OVERFLOW = ERROR_BUFFER_OVERFLOW; // The file name is too long. This error is returned on Windows Vista if the ULONG value pointed to by the PhyAddrLen parameter is less than 6, the size required to store a complete physical address.
static const ULONG IPH_ERROR_GEN_FAILURE = ERROR_GEN_FAILURE; //A device attached to the system is not functioning. This error is returned on Windows Server 2003 and earlier when an ARP reply to the SendARP request was not received. This error can occur if destination IPv4 address could not be reached because it is not on the same subnet or the destination computer is not operating.
static const ULONG IPH_ERROR_INVALID_PARAMETER = ERROR_INVALID_PARAMETER; //One of the parameters is invalid. This error is returned on Windows Server 2003 and earlier if either the pMacAddr or PhyAddrLen parameter is a NULL pointer.
static const ULONG IPH_ERROR_INVALID_USER_BUFFER = ERROR_INVALID_USER_BUFFER; //The supplied user buffer is not valid for the requested operation. This error is returned on Windows Server 2003 and earlier if the ULONG value pointed to by the PhyAddrLen parameter is zero.
static const ULONG IPH_ERROR_NOT_FOUND = ERROR_NOT_FOUND; //Element not found. This error is returned on Windows Vista if the the SrcIp parameter does not specify a source IPv4 address on an interface on the local computer or the INADDR_ANY IP address (an IPv4 address of 0.0.0.0).
static const ULONG IPH_ERROR_NOT_SUPPORTED = ERROR_NOT_SUPPORTED; //The SendARP function is not supported by the operating system running on the local computer.
public:
NativeClassWrapper() { m_nativeClass = new IPHelper(); }
~NativeClassWrapper() { this->!NativeClassWrapper(); }
!NativeClassWrapper() { delete m_nativeClass; }
//DWORD SendARPRequest(IPAddr DestIP, IPAddr SrcIP, PULONG pMacAddr, PULONG PhyAddrLen) { return m_nativeClass->SendARPRequest(DestIP, SrcIP, pMacAddr, PhyAddrLen); };
ULONG SendARPRequest(ULONG DestIP, ULONG SrcIP, ULONG pMacAddr, ULONG PhyAddrLen) { return ULONG (m_nativeClass->SendARPRequest(IPAddr (DestIP), IPAddr(SrcIP), PULONG(pMacAddr), PULONG (PhyAddrLen))); };
};
IPHelper.h
public class IPHelper {
public:
DWORD SendARPRequest(IPAddr DestIP, IPAddr SrcIP, PULONG pMacAddr, PULONG PhyAddrLen);
};
IPHelper.cpp
#include "stdafx.h"
#include "Wrapper.h"
//DWORD IPHelper::SendARPRequest(IPAddr DestIP, IPAddr SrcIP, PULONG pMacAddr, PULONG PhyAddrLen) {
DWORD IPHelper::SendARPRequest(IPAddr DestIP, IPAddr SrcIP, PULONG pMacAddr, PULONG PhyAddrLen) {
return SendARP(DestIP, SrcIP, pMacAddr, PhyAddrLen);
};
【问题讨论】:
-
创建 C++/CLI 包装类的原因是创建一个 .Net“本机”接口,因此您不会处理原始指针(C++/CLI 代码会翻译/转换/编组数据)。看起来你想要一个无符号长数组?
-
或者,您可以直接 p/invoke directly 跳过 C++/CLI。
-
有人建议我使用 C++/CLI 而不是 Interop。我以前从未做过,但决定试一试。我显然不明白这是如何工作的。我会将我的 C++ 代码添加到问题中。你能看看并告诉我我需要做什么才能让 C++ 转换/编组数据吗?非常感谢。
标签: .net vb.net windows pointers c++-cli