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#里面可以用的呢?

解决方案 »

  1.   

    直接copy到vc++.net中编译一个dll
      

  2.   

    楼上,楼上的楼上楼上的楼上的楼上,说的是啊。
    我做了个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.麻烦大家了。呵呵
      

  3.   

    char[]   IPadd   =   new   char[256];         //???   定义一个字符数组IPadd
    string   str1   =   new   string(IPadd);     //???       一个存放新添加的IP的字符串
    int   xx   =   IP(ref   str1,   256);             //???   解析IP
      

  4.   

    回楼上,我哪有做事错误的。
    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); 哪位知道该怎么修改吗?谢谢啦?着急