如何将VB6中的字符串数组有效传递至C语言程序?
- 内容介绍
- 文章标签
- 相关推荐
本文共计435个文字,预计阅读时间需要2分钟。
如何通过COM+Interop将VB6字符串数组传递给C++,以下是一个简化的示例:
cpp// C++ 端#include #include
extern C __declspec(dllexport) void receiveStringArray(const char* const* array, int size) { for (int i=0; i // VB6 端Declare PtrSafe Function receiveStringArray Lib YourDLL.dll (ByRef arr() As String, ByVal size As Long) As LongSub TestInterop() Dim arr() As String arr=Array(a, b, c, d) Call receiveStringArray(arr, UBound(arr))End Sub
我试图将C#字符串数组传递给VB,将VB字符串数组传递给C#,如下所示C# – > VB工作正常但其他方式(VB => C#)给出一个编译错误,称为“函数或接口标记为受限制,或者function使用visual basic中不支持的自动化类型“.我的代码如下
C# public interface ITest { string[] GetArray(); void SetArray(string[] arrayVal ); } public class Test : ITest { string[] ITest.GetArray() { //Working fine string[] stringArray = { "red ", "yellow", "blue" }; return stringArray; } } void ITest.SetArray(string[] arrayVal) //Giving an issue { string[] stringArray1 = arrayVal; }
VB
Dim str As Variant Debug.Print ".NET server returned: " For Each str In dotNETServer.GetArray 'dotNETServer=TestServer.Test Debug.Print str Next Dim arr(3) As String arr(1) = "Pahee" arr(2) = "Tharani" arr(3) = "Rathan" dotNETServer.SetArray (arr) 'This one causing the compile error which I mentioned earlier
更新:
::::::
We need to pass the array as reference in C#.在界面和方法中更改它
void SetArray(ref string[] arrayVal ); //ref added 编组到适当的类型将解决您的问题.请注意下面的编组和ref关键字更改
void ITest.SetArray([MarshalAs(UnmanagedType.SafeArray, SafeArraySubType=VT_BSTR)] ref string[] arrayVal) { string[] stringArray1 = arrayVal; }
我根据你的代码和问题制作了这个解决方案,你无法从VB6中获取数据.如果以上解决方案对您不起作用,请尝试在此处查找适合您的应用程序的数组类型/子类型msdn.microsoft.com/en-us/library/z6cfh6e6(v=vs.110).aspx
本文共计435个文字,预计阅读时间需要2分钟。
如何通过COM+Interop将VB6字符串数组传递给C++,以下是一个简化的示例:
cpp// C++ 端#include #include
extern C __declspec(dllexport) void receiveStringArray(const char* const* array, int size) { for (int i=0; i // VB6 端Declare PtrSafe Function receiveStringArray Lib YourDLL.dll (ByRef arr() As String, ByVal size As Long) As LongSub TestInterop() Dim arr() As String arr=Array(a, b, c, d) Call receiveStringArray(arr, UBound(arr))End Sub
我试图将C#字符串数组传递给VB,将VB字符串数组传递给C#,如下所示C# – > VB工作正常但其他方式(VB => C#)给出一个编译错误,称为“函数或接口标记为受限制,或者function使用visual basic中不支持的自动化类型“.我的代码如下
C# public interface ITest { string[] GetArray(); void SetArray(string[] arrayVal ); } public class Test : ITest { string[] ITest.GetArray() { //Working fine string[] stringArray = { "red ", "yellow", "blue" }; return stringArray; } } void ITest.SetArray(string[] arrayVal) //Giving an issue { string[] stringArray1 = arrayVal; }
VB
Dim str As Variant Debug.Print ".NET server returned: " For Each str In dotNETServer.GetArray 'dotNETServer=TestServer.Test Debug.Print str Next Dim arr(3) As String arr(1) = "Pahee" arr(2) = "Tharani" arr(3) = "Rathan" dotNETServer.SetArray (arr) 'This one causing the compile error which I mentioned earlier
更新:
::::::
We need to pass the array as reference in C#.在界面和方法中更改它
void SetArray(ref string[] arrayVal ); //ref added 编组到适当的类型将解决您的问题.请注意下面的编组和ref关键字更改
void ITest.SetArray([MarshalAs(UnmanagedType.SafeArray, SafeArraySubType=VT_BSTR)] ref string[] arrayVal) { string[] stringArray1 = arrayVal; }
我根据你的代码和问题制作了这个解决方案,你无法从VB6中获取数据.如果以上解决方案对您不起作用,请尝试在此处查找适合您的应用程序的数组类型/子类型msdn.microsoft.com/en-us/library/z6cfh6e6(v=vs.110).aspx

