Marshaling VB.Net Structure

I need to send a structure, containing strings, from VB.Net to unmanaged VC++ dll. In my VB.Net wrapper, there is the function declaration: Public Declare Ansi Function BDS_CapProc Lib "BDS_TK.dll" _ (ByRef in_cap As CapInput, ByRef outcap_eval As CapOutEval, _ ByRef outcap_annual As CapOutAnnual, ByRef outcap_intermed As CapOutIntermed) As Int16 The CapInput structure is defined as: <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _ Structure CapInput Dim CapCash() As Double Dim CapInfl() As Double Dim ProdIncFac() As Double Dim Pct_Replace As Double Dim CCA_Rate As Double Dim Salv_Fac As Double Dim Rem_Fac As Double Dim CR_Exp_Fac As Double Dim PIS_Exp_Fac As Double Dim Exp_Book_Fac As Double Dim Exp_Tax_Fac As Double Dim CFTiming As Int16 Dim OPST_apply As Int16 Dim QPST_apply As Int16 Dim TGET_exempt As Int16 Dim DSP_Treatment As Int16 Dim ComRetYr As Int16 Dim ComRetMth As Int16 Dim Life_Est As Int16 Dim Surv_Curve As Int16 Dim Scenario As Int16 Dim Mthly_Svc As Int16 Dim z_Fill As Int16 <MarshalAs(UnmanagedType.BStr)> _ Dim Cap_ID As String <MarshalAs(UnmanagedType.BStr)> _ Dim Cap_Description As String When I populate the CapInput structure, the data is good; when I send it to unmanaged code, the data is all corrupt. I have tried other variations of the <MarshalAs(UnmanagedType attribute (LPStr, LPWStr, LPTStr) all with the same reault. I have tried CharSet:=CharSet.Ansi and CharSet:=CharSet.Auto in the StructLayout attribute. Always the same result. The unmanaged code is in production, and works fine. It is currently called from Excel VBA. The structure definition is the same as above (except for the MarshalAs part, which is not need when calling from VBA). I have successfully passed managed structures containing only Doubles and Int16?s to the unmanaged code; no problem there. The other 3 structures in the above call contain no strings. So, I think the strings in the CapInput structure are hanging me up. Any ideas are appreciated.

Answer Wiki

Thanks. We'll let you know when a new response is added.

Is the string data fixed length?

Look at re the VBFixedStringAttribute.

Discuss This Question: 2  Replies

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.
  • Ppanke
    The string lengths are variable.
    0 pointsBadges:
  • Mistoffeles
    Can you send fixed-length strings and allow the DLL to interpret them as variable-length by appending "n" to each string before sending the structure?
    0 pointsBadges:

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

Thanks! We'll email you when relevant content is added and updated.


Share this item with your network: