My Question is simple
VB.dll (VB5.0 I guess) includes these methods
Private Declare Function ffr_device_find Lib ".\ffr_32.dll" () As Boolean
Private Declare Function ffr_data_transceive_ex Lib ".\ffr_32.dll" (ByVal sp_sdata As String, ByVal sp_rdata As String) As Boolean
in C#.... ( .NET 4.5 )
[DllImport("FFR_32.dll", CallingConvention = CallingConvention.Cdecl)]
extern public static Boolean ffr_device_find();
[DllImport("FFR_32.dll", CallingConvention = CallingConvention.Cdecl)]
extern public static void ffr_data_transceive_ex([Out] string sp_sdata, [Out] string sp_rdata);
// FYI, I tried [Out], out, and ref but to no avail.
The first one works great,
but the second one spilt this error.
A call to PInvoke function 'ffr_data_transceive_ex' has unbalanced the stack.
This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.
FYI
This is a working code from VB... ( NOT INNER DLL SOURCES )
Dim st As String
Dim rData As String * 40
st = "4401" & "20202020202020202020202020202020"
Text1.Text = st
Cal_BCC
Call ffr_data_transceive_ex(Text1.Text, rData)
Text2.Text = rData
I don't even understand what Dim rData As String * 40
is about... will it become 0
when rData
is 0
? and become 40
when rData
has 1
? ...
What's wrong with my DllImport methods in C#???
ccr_data_transceive_ex
is[Out]
? Because I don't see it used as an output parameter in the example you provided. – kdojeteri* 40
in VB makes the string have a fixed preallocated length of 40. In C# try:[Out] StringBuilder sp_rdata
where sp_rdata is anew StringBuilder(40)
, do the same forsp_sdata
if it is infact an output, otherwise remove the out.ccr_data_transceive_ex
is notvoid
itsboolean
. – Alex K.[Out], ref or out
because the dll takes the param asByVal
– hina10531void
andboolean ( or bool )
don't work either. It produces the same error. – hina10531ByVal
has a different meaning in VB when applied to strings. Passing stringsByVal
to API functions is the way to do it, and the function can change the contents of the string. It is not related to[In]
and[Out]
, these are mere hints for the marshaler to skip copying data. – GSerg