How to pass a long array from VB6 to C# thru COM - c#

I need to pass an array of int or long (doesn't matter) from a VB6 application to a C# COM Visible class. I've tried declaring the interface in C# like this:
void Subscribe([MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_I4)]int[] notificationTypes)
void Subscribe(int[] notificationTypes)
But both of them raised a Function or interface markes as restricted, or the function uses an Automation type not supported in Visual Basic.
How should I declare the C# method?

If you get desperate, code the signature in a dummy VB6 ActiveX dll project. Then generate the .NET Interop version of the vb6 component via Visual studio or the command line tool. Then use Reflector or dotPeek to pull the code out of the interop assembly. It is the long way around, but it works.

I ran into this problem 9 years later. The solution I came up with is to pass a pointer to the first element of the array, along with the upper bound (UBound in VB6). Then iterate the pointer to the upper bound and put each element into a list.
on the vb6 side use
Dim myarray(3) As float
Dim ptr As integer
Dim upperbound as integer
myarray(0) = 0.1
myarray(1) = 0.2
myarray(2) = 0.3
myarray(3) = 0.4
ptr = VarPtr(myarray(0))
upperbound = UBound(myarray)
SetMyFloat(ptr, upperbound)
C# code
public float MyFloat {get; set;}
public unsafe void SetMyFloat(float* ptr, int ubound)
{
MyFloat = PointerToFloatArray(ptr, ubound);
}
public unsafe float[] PointerToFloatArray(float* ptr, int ubound)
//this is to deal with not being able to pass an array from vb6 to .NET
//ptr is a pointer to the first element of the array
//ubound is the index of the last element of the array
{
List<float> li = new List<float>();
float element;
for (int i = 0; i <= ubound; i++)
{
element = *(ptr + i);
li.Add(element);
}
return li.ToArray();
}

Related

How to copy an array of floats from c# to a C dll

I'm trying to copy an array of floats from my C# application to an array in a C-coded DLL.
Im used to programming in C#, not so much with C. However I have no problem doing the reverse procedure ie. reading an array of floats from a C coded DLL into my C# application. I've read several threads on this site but cant work out where Im going wrong.
C# CODE
[DllImport(#"MyDll")]
static extern int CopyArray(double[] MyArray);
double[] myValues = new double[100]
int a = 0;
while(a < 100)
{
myValues[a] = a;
a++;
}
CopyArray(myValues);
C++ DLL
This is the function header;
__declspec(dllexport) int CopyArray(float* Wavelengths);
This is the function code;
float theValues[100];
int CopyArray(float* theArray)
{
status = 0;
int count = 0;
while (count < 100)
{
theValues[count] = theArray[count];
++count;
}
return(status);
}
I'm expecting my C# array to end up in the C array "theValues" but that's not happening. There is nothing getting into "theValues" array.
A couple of things.
You are mixing data types and they are different lengths (float is 32bit and double is 64bit). Both types exist in both languages, but your caller and callee need to agree on the data type. Here is a list of data types and their managed/unmanaged equivalents.
The parameter you are sending is not a pointer. It might be translated to that automatically by the compiler, but there are several options. It is entirely possible that the compiler will pick one you don't want (more info here). The one you are probably looking for is [In]. If you want to get data back from C to C#, you will also want [Out]:
[DllImport(#"MyDll")]
static extern int CopyArray([In, Out]double[] MyArray);

Where is the data type mismatch when passing arrays from vba to c#?

My C# COM DLL has a method that accepts a float array and a long int array. It returns a float.
From VBA in an MS Access module, I create an array of type single and another of type long, populate them, create the DLL app.class object and then call its method with the two arrays. But I get a "type mismatch" error.
The following is the actual code, but it is simple because I'm trying to work out the communications before adding the "real" code.
C# code:
public float JustTesting(float[] Array1, long[] Array2)
{
return 96.0F;
}
VBA code:
Public Sub Test()
Dim a1(0 To 0) As Single, a2(0 To 0) As Long, sng As Single
a1(0) = 5
a2(0) = 10
Dim o As Variant
Set o = CreateObject("MyApp.MyClass")
sng = o.JustTesting(a1, a2)
Debug.Print CStr(sng)
Set o = Nothing
End Sub
Where is the data type mismatch?
A Long in VBA is only 32 bits, the same was as an int in C#. So your method needs to take an array of ints
public float JustTesting(float[] Array1, int[] Array2)
{
return 96.0F;
}

Exception Passing SAFEARRAY in VARIANT to C# From C++ COM Server

I have spent the last day searching documentation, reviewing forum posts, and googling to try to do something that I am guessing could be easily done with the right information.
I have a very large existing C++ application that has an already defined COM server with many methods exposed. I am trying to use those COM methods in a C# application (I am experienced in C++, but a C# newbie).
So in my VS2010 C# application, I add the COM server as a reference. COM Methods are visible in the object browser, and passing single-valued strings, floats, and ints seems to work fine.
But I am stumped trying to read SAFEARRAY values passed out of the C++ COM server into the C# application. Eventually I need to pass string arrays from C++ server to the C# app, but in just testing passing an array of floats, I have code that builds but fails with the following exception when I try to cast the System.Object containing the array of floats to (float[]) ,
"exception {System.InvalidCastException: Unable to cast object of type 'System.Object[]' to type 'System.Single[]'."
With intellisence I can see that the Object contains the correct 8760 long array of floats, but I am unable to access that data in C#.
Here is the code on the C# side (d2RuleSet is an interface defined in the DOE2Com COM server). The exception above is thrown on the last line below.
DOE2ComLib.DOE2Com d2RuleSet;
d2RuleSet = new DOE2ComLib.DOE2Com();
System.Int32 i_Series =0;
System.Object pv_WeatherData;
float[] faWeatherData;
iOut = d2RuleSet.GetWeatherData(i_Series, out pv_WeatherData);
Type typeTest;
typeTest = pv_WeatherData.GetType();
int iArrayRank = typeTest.GetArrayRank();
Type typeElement = typeTest.GetElementType();
faWeatherData = (float[])pv_WeatherData;
Below is the section in the idl file defining the C++ COM method
HRESULT GetWeatherData( [in] int iSeries, [out] VARIANT* pvWeatherData, [out,retval] int * piErrorCode);
Below is the C++ code where the VARIANT data is loaded.
void CDOE2BaseClass::GetWeatherData( int iSeries, VARIANT* pvWeatherData, int* piErrorCode)
{
*piErrorCode = 0;
if (iSeries < 0 || iSeries >= D2CWS_NumSeries)
*piErrorCode = 1;
else if (m_faWeatherData[iSeries] == NULL)
*piErrorCode = 3;
else
{
SAFEARRAYBOUND rgsaBound;
rgsaBound.lLbound = 0;
rgsaBound.cElements = 8760;
// First lets create the SafeArrays (populated with VARIANTS to ensure compatibility with VB and Java)
SAFEARRAY* pSAData = SafeArrayCreate( VT_VARIANT, 1, &rgsaBound );
if( pSAData == NULL ) {
#ifndef _DOE2LIB
_com_issue_error( E_OUTOFMEMORY);
#else
//RW_TO_DO - Throw custom Lib-version exception
OurThrowDOE2LibException(-1,__FILE__,__LINE__,0,"OUT OF MEMORY");
#endif //_DOE2LIB
}
for (long hr=0; hr<8760; hr++)
{
COleVariant vHrResult( m_faWeatherData[iSeries][hr] );
SafeArrayPutElement( pSAData, &hr, vHrResult );
}
// Now that we have populated the SAFEARRAY, assign it to the VARIANT pointer that we are returning to the client.
V_VT( pvWeatherData ) = VT_ARRAY | VT_VARIANT;
V_ARRAY( pvWeatherData ) = pSAData;
}
}
Thanks in advance for help with this problem, I feel like I spent too much time on what should be a simple problem. Also please post up any links or books that that cover interop between native C++ and C# well (I think I have already ping-ponged through most of the Visual Studio/MSDN documentation, but maybe I missed something there too).
-----------------End Of Original Question------------------------------------------------------
I am editing to post the code from phoog's successful solution below, so others can read and use it.
int iOut = 0;
System.Int32 i_Series =0;
System.Object pv_WeatherData = null;
iOut = d2RuleSet.GetWeatherData(i_Series, out pv_WeatherData);
Type typeTest;
typeTest = pv_WeatherData.GetType();
int iArrayRank = typeTest.GetArrayRank();
Type typeElement = typeTest.GetElementType();
//float[] faWeatherData = (float[])pv_WeatherData;
float[] faWeatherData = ConvertTheArray((object[])pv_WeatherData);
....
float[] ConvertTheArray(object[] inputArray)
{
float[] result = new float[inputArray.Length];
for (var index = 0; index < result.Length; index++)
result[index] = (float)inputArray[index];
return result;
}
The marshalled SAFEARRAY is a System.Object[]; you can't reference-convert that to a System.Single[]. You have to cast the individual elements. This would work, assuming that all the elements of the argument array are in fact boxed floats:
float[] ConvertTheArray(object[] inputArray)
{
float[] result = new float[inputArray.Length];
for (var index = 0; index < result.Length; index++)
result[index] = (float)inputArray[index];
return result;
}
You could do that with a lot less typing using Linq, but as you're a C# newbie I thought a more basic solution might be better.
EDIT
Since you indicated in your comment that your object[] is referenced as an object, here's the usage example:
object obj = GetMarshalledArray();
float[] floats = ConvertTheArray((object[])obj);
EDIT 2
A shorter solution using Linq:
float[] ConvertTheArray(object[] inputArray)
{
return inputArray.Cast<float>().ToArray();
}

passing 2D array from managed C++ to unmanaged C++

I'm working on a managed C++ wrapper for unmanaged C++ code and have a question.
Just for simplicity let's say that I need to pass a 2D array from C# code to Managed C++ to Unmanaged C++. I have no problem with 1D array but stuck with 2D version. Converting it to 1D is the option, but I want to take a look if there are other ways.
For simplicity let say I want to send 2D array to unmanaged code using intermediate wrapper and change its values.
so here is C# code with a call to managed C++:
MNumeric wrapper = new MNumeric(); //managed C++ object
int[,] dArr = new int[10, 10];
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
dArr[i, j] = 10;
}
}
wrapper.ChangeArray(dArr, Convert.ToInt32(Math.Sqrt(dArr.Length)))
Managed C++:
void MNumeric::ChangeArray(cli::array<int,2> ^arr, int mySize)
{
//some code to call Unmanaged C++ passing managed 2D array ???
}
Unmanaged C++
void UNumeric::ChangeArray(int** arr, int mySize)
{
for(int i=0;i<mySize;i++)
{
for(int j=0;j<mySize;j++)
{
arr[i][j]=i;
}
}
}
Thanks a lot for your help.
Looks like I fix one error but got another. My C++ Managed code looks like this now.
void MNumeric::ChangeArray(cli::array<int,2> ^arr, int mySize)
{
pin_ptr<int> p_arr = &arr[0,0];
u_num->ChangeArray((int**)p_arr, mySize);
}
where u_num is just a pointer to UNumeric class.
The error I got now is the following:
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Any ideas appreciated.
void MNumeric::ChangeArray(cli::array&ltint,2> ^arr, int mySize)
{
pin_ptr&ltint> p = &arr[0,0]; // pin pointer to first element in arr
int* np = p; // pointer to the first element in arr
UNumeric::ChangeArray((int**)np, mySize);
}
You should not use a cast here, it could hide potentially important warnings. Notably, a 2D array is not convertible to an int**. An int**, is a pointer to an array of pointers. An int*[] is a pointer to an array of arrays. You have a function that takes an array of pointers. It wants a managed int[][], not an int[,]. Your compiler would thoroughly have shouted at you for attempting this, if you hadn't casted.

Calling FORTRAN dll from C# and assigning values to array of structures

I can pass a C# struct into FORTRAN just fine. I can even pass an array of a C# struct as an array of TYPE() in FORTRAN. Where I run into trouble is when I tried to return values back into C#. Here is an example:
The fortran dll is:
MODULE TESTING
TYPE VALUEREF
INTEGER*4 :: A
ENDTYPE VALUEREF
CONTAINS
SUBROUTINE TEST_REF(T,N)
!DEC$ ATTRIBUTES DLLEXPORT :: TEST_REF
!DEC$ ATTRIBUTES ALIAS:'TEST_REF' :: TEST_REF
!DEC$ ATTRIBUTES VALUE :: N
IMPLICIT NONE
INTEGER*4 :: A,I,N
TYPE(VALUEREF) :: T(N)
A = 100
DO I=1,N
T(I)%A = A + I
END DO
END SUBROUTINE
END MODULE
and the C# calling function that expects results is:
[StructLayout(LayoutKind.Sequential)]
public struct ValueRef
{
public int a;
}
[DllImport("mathlib.dll")]
static extern void TEST_REF(ValueRef[] t, int n);
void Main()
{
ValueRef[] T = new ValueRef[4];
for (int i = 0; i < T.Length; i++)
{
T[i].a = i;
}
Console.WriteLine("Initialize");
for (int i = 0; i < T.Length; i++)
{
Console.WriteLine(" A={0}", T[i].a);
}
Console.WriteLine("Call Fortran");
TEST_REF(T, T.Length);
for (int i = 0; i < T.Length; i++)
{
Console.WriteLine(" A={0}", T[i].a);
}
}
With results:
Initialize
A=0
A=1
A=2
A=3
Call Fortran
A=0
A=1
A=2
A=3
Debugging through the FORTRAN code, I see the initial values pass from C# to FORTRAN just fine. The the values get overridden with new values and control is passed back into C# where the old values are still contained within the ValueRef instances.
Why is it that I can pass and return an array of float, or int in a similar fashion, just fine. and I can pass and return singular structures with ref keyword, and I can pass but not return and array of struct?
PS. I am using Compaq Visual Fortran 6.5 & .NET 3.5
PS2. I appreciate any comments/ideas on this. I am 95% done with my project, and now I run into this issue. The whole point of this project is to use structures as much as possible to reduce the #of arguments passed to functions and retain certain aspects of OOP design.
I have done this in the past using a pointer, not an array. I think that your structures are being copied for the P/Invoke call:
[DllImport("mathlib.dll")]
static extern void TEST_REF(ValueRef* t, int n);
You will need to pin your array before calling the method.
fixed (ValueRef* pointer = t)
{
TEST_REF(pointer, n);
}
Edit:
Based on the comments the solution is to declare the external as
[DllImport("mathlib.dll")]
static extern void TEST_REF([Out] ValueRef[] t, int n);
Here is a MSDN reference on Marshaling of arrays, and how they default to [In].

Categories