从 c# COM dll 返回时,C++ SAFEARRAY 具有无效数据
C++ SAFEARRAY has invalid data when returned from c# COM dll
我尝试从 c++ 中的 c# dll 获取数据(以 class/struct 数组的形式)。我尝试使用 safearray 辅助函数,但我收到的所有数据都是无效的....
调试所有内容时 works/looks 完美,直到我达到接收 dll 中生成的数据的程度(只有 SAFEARRAY* 看起来无效),所以这可能是 C++ 应用程序和 C++ 应用程序之间的通信问题c# COM dll
VS SAFEARRAY Debug/Auto Window
VS DownloadList Debug/Auto Window
这是一个代码示例:
C#
// data class
public class Download
{
public string Target;
public string Data;
public int Port;
}
// function called from outside dll
public Download[] GetData()
{
[...]
return DownloadList.ToArray(); // List<Download>
}
C++
#import "[...]/MSSQL_Lib.tlb"
// pDB is a class instance which contain the GetData func
[...]
SAFEARRAY* Data = pDB->GetData();
if( Data != nullptr )
{
// print varian type info from result
SafeArrayLock( Data );
VARIANT* ValueArray = (VARIANT*)Data->pvData;
long Lower = 0, Upper = 0;
SafeArrayGetLBound( Data, 1, &Lower );
SafeArrayGetUBound( Data, 1, &Upper );
for( long i = 0; i <= ( Upper - Lower ); ++i )
{
PrintVariant( &ValueArray[i] );
}
SafeArrayUnlock( Data );
SafeArrayDestroy( Data );
}
[...]
// function end
void PrintVariant( VARIANT* pV )
{
switch( pV->vt )
{
case VT_BSTR:
wprintf( L" String : %s \n", pV->bstrVal );
break;
default:
wprintf( L" Unrecognized Type : %d \n", pV->vt );
break;
}
}
我还尝试编组 c# class :
[StructLayout(LayoutKind.Sequential)]
public class Download
{
[MarshalAs(UnmanagedType.BStr)]
public string Target;
[MarshalAs(UnmanagedType.BStr)]
public string Data;
public int Port;
}
但结果相同。有了元帅,我得到了这个警告:"Type library exporter warning processing '[...].Download, [...]'. Warning: The reference type had sequential or explicit layout, and so was exported as a struct." 但我想它不应该导致完全无效的返回数据
打印结果如下:
Unrecognized Type : 65432
Unrecognized Type : 65048
Unrecognized Type : 64664
Unrecognized Type : 64280
Unrecognized Type : 1
Unrecognized Type : 0
Unrecognized Type : 0
Unrecognized Type : 43008
Unrecognized Type : 21288
Unrecognized Type : 1331
但它们每隔 运行 就会改变一点点。
所以我希望任何人都可以帮助我找到我错过的细节^^
感谢阅读,欢迎随时询问更多详情
您的数组不是 VARIANT 数组,而是 IUnknown* 数组(调用 SafeArrayGetVartype 会告诉您这一点)。您必须将代码更改为如下内容:
SAFEARRAY *psa = pDB->GetData();
if (psa)
{
SafeArrayLock(psa);
LONG lb;
LONG ub;
SafeArrayGetLBound(psa, 1, &lb);
SafeArrayGetUBound(psa, 1, &ub);
for (int i = lb; i <= ub; i++)
{
_DownloadPtr download(((IUnknown**)psa->pvData)[i]);
wprintf(L"%s\n", (LPWSTR)download->GetTarget());
}
SafeArrayUnlock(psa);
}
请注意,这意味着您的下载 class 也必须标记为 [ComVisible(true)]
,并且字段将转换为 public 属性。因为在这个例子中你需要一个 IUnknown 接口(看到自动添加到生成的 C++ 代码中的 GetTarget() 等方法),我也建议你给它添加一个 [ClassInterface(ClassInterfaceType.AutoDual)]
属性。
如果您确实想要 VARIANT 数组(为什么要这样做?),那么您必须使用 object[]
而不是键入 objects.arrays.
我尝试从 c++ 中的 c# dll 获取数据(以 class/struct 数组的形式)。我尝试使用 safearray 辅助函数,但我收到的所有数据都是无效的....
调试所有内容时 works/looks 完美,直到我达到接收 dll 中生成的数据的程度(只有 SAFEARRAY* 看起来无效),所以这可能是 C++ 应用程序和 C++ 应用程序之间的通信问题c# COM dll VS SAFEARRAY Debug/Auto Window VS DownloadList Debug/Auto Window
这是一个代码示例: C#
// data class
public class Download
{
public string Target;
public string Data;
public int Port;
}
// function called from outside dll
public Download[] GetData()
{
[...]
return DownloadList.ToArray(); // List<Download>
}
C++
#import "[...]/MSSQL_Lib.tlb"
// pDB is a class instance which contain the GetData func
[...]
SAFEARRAY* Data = pDB->GetData();
if( Data != nullptr )
{
// print varian type info from result
SafeArrayLock( Data );
VARIANT* ValueArray = (VARIANT*)Data->pvData;
long Lower = 0, Upper = 0;
SafeArrayGetLBound( Data, 1, &Lower );
SafeArrayGetUBound( Data, 1, &Upper );
for( long i = 0; i <= ( Upper - Lower ); ++i )
{
PrintVariant( &ValueArray[i] );
}
SafeArrayUnlock( Data );
SafeArrayDestroy( Data );
}
[...]
// function end
void PrintVariant( VARIANT* pV )
{
switch( pV->vt )
{
case VT_BSTR:
wprintf( L" String : %s \n", pV->bstrVal );
break;
default:
wprintf( L" Unrecognized Type : %d \n", pV->vt );
break;
}
}
我还尝试编组 c# class :
[StructLayout(LayoutKind.Sequential)]
public class Download
{
[MarshalAs(UnmanagedType.BStr)]
public string Target;
[MarshalAs(UnmanagedType.BStr)]
public string Data;
public int Port;
}
但结果相同。有了元帅,我得到了这个警告:"Type library exporter warning processing '[...].Download, [...]'. Warning: The reference type had sequential or explicit layout, and so was exported as a struct." 但我想它不应该导致完全无效的返回数据
打印结果如下:
Unrecognized Type : 65432
Unrecognized Type : 65048
Unrecognized Type : 64664
Unrecognized Type : 64280
Unrecognized Type : 1
Unrecognized Type : 0
Unrecognized Type : 0
Unrecognized Type : 43008
Unrecognized Type : 21288
Unrecognized Type : 1331
但它们每隔 运行 就会改变一点点。
所以我希望任何人都可以帮助我找到我错过的细节^^ 感谢阅读,欢迎随时询问更多详情
您的数组不是 VARIANT 数组,而是 IUnknown* 数组(调用 SafeArrayGetVartype 会告诉您这一点)。您必须将代码更改为如下内容:
SAFEARRAY *psa = pDB->GetData();
if (psa)
{
SafeArrayLock(psa);
LONG lb;
LONG ub;
SafeArrayGetLBound(psa, 1, &lb);
SafeArrayGetUBound(psa, 1, &ub);
for (int i = lb; i <= ub; i++)
{
_DownloadPtr download(((IUnknown**)psa->pvData)[i]);
wprintf(L"%s\n", (LPWSTR)download->GetTarget());
}
SafeArrayUnlock(psa);
}
请注意,这意味着您的下载 class 也必须标记为 [ComVisible(true)]
,并且字段将转换为 public 属性。因为在这个例子中你需要一个 IUnknown 接口(看到自动添加到生成的 C++ 代码中的 GetTarget() 等方法),我也建议你给它添加一个 [ClassInterface(ClassInterfaceType.AutoDual)]
属性。
如果您确实想要 VARIANT 数组(为什么要这样做?),那么您必须使用 object[]
而不是键入 objects.arrays.