0

我必须在 vb.net 应用程序中获取 Features 数组。如何做到这一点。这是 VC++ 中的一个函数。

STDMETHODIMP CclsLicense::FeatureList(VARIANT* Features,BSTR HostName, VARIANT_BOOL   *ret){
USES_CONVERSION;
int status                  = 0;
int iCount                  = 0;
int nLicenseFeatures        = 0;        
char **featureList          = NULL;     // List of features
// Safe Array
SAFEARRAYBOUND  bound[1];
SAFEARRAY       *safeArray  = NULL;     // A Safe array for VB
CComVariant     *pBstr      = NULL;     // Array of BSTR Value
// Initialize the return value
*ret = VARIANT_FALSE;

nLicenseFeatures = 0;

featureList = new char*[MAX_FEATURES];
   for (i=0;i<4;i++)
   {
featureList[nLicenseFeatures]=array[i];

nLicenseFeatures++;
}   

    // Array starts at 0 and has the number of features as elements
bound[0].lLbound = 0;
bound[0].cElements = nLicenseFeatures;

// Initialize Array
if((safeArray = ::SafeArrayCreate( VT_VARIANT, 1, bound)) == NULL)
    return E_FAIL;

::VariantClear(Features);

Features->vt     = VT_VARIANT | VT_ARRAY;
Features->parray = safeArray;

//use direct access to data
if(FAILED(hr = ::SafeArrayAccessData(safeArray, (void HUGEP**)&pBstr)) || pBstr == NULL)
    return hr;

    iCount = 0;
while( featureList[iCount] != NULL )
{


        // Add to Array
        if(pBstr[iCount].bstrVal != NULL)
        {
            ::SysFreeString(pBstr[iCount].bstrVal);
            pBstr[iCount].bstrVal = NULL;
        }

        if(featureList[iCount] == NULL)
            pBstr[iCount].bstrVal = ::SysAllocString(OLESTR(""));   //imposible
        else
            pBstr[iCount].bstrVal = ::SysAllocString(T2OLE(featureList[iCount]));

        pBstr[iCount].vt = VT_BSTR;



    // Increment counter
    iCount++;

}
// Release Array
::SafeArrayUnaccessData(safeArray);

*ret = VARIANT_TRUE;

return S_OK;

}

Vb.Net 函数获取功能列表

 Public Shared Function FeatureList(ByVal strLicensePath As String)
    Dim features(10) As String
    Try
        m_objUTSLicense = CreateObject("dll name")
        Call m_objUTSLicense.FeatureList(features, "192.168.1.3")

    Catch ex As Exception

    End Try
    Dim i As Integer
    Dim size As Integer = features.Length
    For i = 0 To size - 1
        MessageBox.Show(features(i))
    Next

End Function

当我尝试此代码时,收到错误“尝试读取或写入受保护的内存。这通常表明其他内存已损坏”

4

1 回答 1

0

如果没有您的整个项目,我无法对此进行测试,但您可以尝试声明一个成员变量(而不是本地变量),以便您可以应用必要的编组属性。就像是:

Imports System.Runtime.InteropServices

<MarshalAs(UnmanagedType.SafeArray, safearraysubtype:=VarEnum.VT_BSTR)>
Private features As System.Array
于 2013-10-11T06:54:55.783 回答