Passing Array from C# manged code to unmanged code

  • Thread starter Thread starter Manish
  • Start date Start date
M

Manish

Hi,
I m facing a problem in re-writing a code from VB to C #. We are using
a third party DLL to develop a application of Fax. The current
application is already written in VB and we have to convert it into
C#. In VB we are using a function from the DLL whose signature is like
:

Declare Function RFVB_SendFiles1 Lib "RF2VB.DLL" (ByVal hServer As
Long, _
ByVal lFI10Object As
Long, _
ByVal lNI10Object As
Long, _
ByVal fSendWithCover
As Boolean, _
sCoverSheetModel As
String, _
aFiles() As String,
_
hNewFax As Long) As
Long

Now in C# , we are having problems in converting the signature of this
function. We tried the following , but it didn't work. It doesn't give
any error at compile time but at run time this function doesn't work.
I m suspecting that passing a array has some problem

[DLLImport("RF2VB.dll")]
public static extern int RFVB_SendFiles1(int hServer, int lFI10Object
, int lNI10Object, bool fSendWithCover,ref string sCoverSheetModel,
[MarshalAs(UnmanagedType.SafeArray,SafeArraySubType =
VarEnum.VT_BSTR)] ref string [] aFiles ,ref int hNewFax);

Does anybody has any idea of declaring this properly.

Thanks,
Manish
 
Manish,

I see two problems with this declaration. The first is with the array.
You can not pass the array the way that you are passing it (by ref). You
should marshal the array as is, without the ref modifier.

Also, in VB, a boolean is 16 bits, so you will have to pass it as a
short in .NET. Your declaration should look like this:

[DLLImport("RF2VB.dll")]
public static extern int RFVB_SendFiles1(
int hServer,
int lFI10Object,
int lNI10Object,
short fSendWithCover,
string sCoverSheetModel,
string[] aFiles ,
ref int hNewFax);

Do you have the declaration of the function in a C++ header file? It is
possible that the declarations of the parameters were tailored for VB, but
without it, it is hard to tell.

Hope this helps.
 
Nicholas,

I tried with what you suggested but it didn't work too. I got an Invalid
Parameter Passed error froim the API. Unfortunatelty I don't have the
C++ header file , All I have is the dll.

Thanks,
Manish
 
Back
Top