HRESULT CtestDlg::GetDeviceIP(OUT CHAR* pszDeviceIP, IN size_t size)
{
HRESULT hr = E_FAIL; IRAPIDesktop *pIRapiDesktop = NULL;
IRAPIEnumDevices *pIRapiEnumDevices = NULL;
IRAPIDevice *pIRapiDevice = NULL;
RAPI_CONNECTIONINFO connectionInfo = {0}; SOCKADDR_STORAGE *pIpaddr = NULL;
sockaddr_in *psoc4 = NULL; if (NULL == pszDeviceIP)
{
hr = E_INVALIDARG;
goto Exit;
} // CoCreateInstance assumes that Threading model has already been set
hr = CoCreateInstance(CLSID_RAPI, NULL, CLSCTX_INPROC_SERVER,
IID_IRAPIDesktop,
(void**)&pIRapiDesktop
);
if (FAILED(hr))
{
goto Exit;
} // Enumerate the Windows Mobile devices connected
hr = pIRapiDesktop->EnumDevices(&pIRapiEnumDevices);
if (FAILED(hr))
{
goto Exit;
}
// Only one device can get connected to AS 4.0 at a time.
// Get the first device connected
hr = pIRapiEnumDevices->Next(&pIRapiDevice);
if (FAILED(hr))
{
goto Exit;
} // Get connection information using pIRapiDevice returned previously
hr = pIRapiDevice->GetConnectionInfo(&connectionInfo);
if (FAILED(hr))
{
goto Exit;
}
// Convert the ip address received to an ANSI string
pIpaddr = &(connectionInfo.ipaddr);
psoc4 = reinterpret_cast<sockaddr_in *> (pIpaddr);
hr = StringCchPrintfA(pszDeviceIP, size, "%s", inet_ntoa(psoc4->sin_addr));
if (FAILED(hr))
{
goto Exit;
} hr = S_OK;Exit:
// Time to free resources
if (NULL != pIRapiDesktop)
{
pIRapiDesktop->Release();
} if (NULL != pIRapiEnumDevices)
{
pIRapiEnumDevices->Release();
} if (NULL != pIRapiDevice)
{
pIRapiDevice->Release();
} return hr;
}
这个函数是vc++里面的,请问如何修改成c#里面可以用的呢?
{
HRESULT hr = E_FAIL; IRAPIDesktop *pIRapiDesktop = NULL;
IRAPIEnumDevices *pIRapiEnumDevices = NULL;
IRAPIDevice *pIRapiDevice = NULL;
RAPI_CONNECTIONINFO connectionInfo = {0}; SOCKADDR_STORAGE *pIpaddr = NULL;
sockaddr_in *psoc4 = NULL; if (NULL == pszDeviceIP)
{
hr = E_INVALIDARG;
goto Exit;
} // CoCreateInstance assumes that Threading model has already been set
hr = CoCreateInstance(CLSID_RAPI, NULL, CLSCTX_INPROC_SERVER,
IID_IRAPIDesktop,
(void**)&pIRapiDesktop
);
if (FAILED(hr))
{
goto Exit;
} // Enumerate the Windows Mobile devices connected
hr = pIRapiDesktop->EnumDevices(&pIRapiEnumDevices);
if (FAILED(hr))
{
goto Exit;
}
// Only one device can get connected to AS 4.0 at a time.
// Get the first device connected
hr = pIRapiEnumDevices->Next(&pIRapiDevice);
if (FAILED(hr))
{
goto Exit;
} // Get connection information using pIRapiDevice returned previously
hr = pIRapiDevice->GetConnectionInfo(&connectionInfo);
if (FAILED(hr))
{
goto Exit;
}
// Convert the ip address received to an ANSI string
pIpaddr = &(connectionInfo.ipaddr);
psoc4 = reinterpret_cast<sockaddr_in *> (pIpaddr);
hr = StringCchPrintfA(pszDeviceIP, size, "%s", inet_ntoa(psoc4->sin_addr));
if (FAILED(hr))
{
goto Exit;
} hr = S_OK;Exit:
// Time to free resources
if (NULL != pIRapiDesktop)
{
pIRapiDesktop->Release();
} if (NULL != pIRapiEnumDevices)
{
pIRapiEnumDevices->Release();
} if (NULL != pIRapiDevice)
{
pIRapiDevice->Release();
} return hr;
}
这个函数是vc++里面的,请问如何修改成c#里面可以用的呢?
我做了个dll
extern "C" _declspec(dllexport) int GetDeviceIP(OUT CHAR* pszDeviceIP, IN size_t size)
//
可是我在使用的时候不会用,我是这样做的
类里面:
public delegate int GetDeviceIP(ref string pszDeviceIP, int size); //???然后Form1_Load里面:
hModule = LoadLibrary("GetIP.dll"); GetDeviceIP IP = (GetDeviceIP)GetAddress(hModule,"GetDeviceIP",typeof(GetDeviceIP)); char[] IPadd = new char[256]; //???
string str1 = new string(IPadd); //???
int xx = IP(ref str1, 256); //??? FreeLibrary(hModule);主要是字符串的问题,我的CHAR == char.麻烦大家了。呵呵
string str1 = new string(IPadd); //??? 一个存放新添加的IP的字符串
int xx = IP(ref str1, 256); //??? 解析IP
public delegate int GetDeviceIP(ref string pszDeviceIP, int size); //可能不对,怎么改?hModule = LoadLibrary("GetIP.dll");GetDeviceIP IP = (GetDeviceIP)GetAddress(hModule,"GetDeviceIP",typeof(GetDeviceIP));
string MobileIP=string.Empty; //可能不对,怎么改?
int xx = IP(ref str1, 256); //可能不对,怎么改?FreeLibrary(hModule); 哪位知道该怎么修改吗?谢谢啦?着急