What are the challenges you face when working across database platforms? Take the survey
Options

VB.Net CallByName problem with optional parameters

michaelPmichaelP Posts: 4
edited April 14, 2014 9:26AM in SmartAssembly
Hi,

I've encountered a problem when calling an obfuscated vb.net 2012 DLL

Product: SmartAssembly standard v6.7.0.239 (also have the same issue with SmartAssembly Developer v6.8.0.121)


I'm making a late bound call to a public method with optional parameter(s)

When the call fails, the following error is returned:

"An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)"


Example method signature that fails:

Public Sub BrokenMethod(Optional ByVal badParameter As SqlConnection = Nothing)


I have identified that some optional parameter types (string and primitive types, from what I can tell) seem to be handled correctly. The issue seems to relate to more complex objects.



Any advice to address this issue would be gratefully received. We need to obfuscate our binaries before sending the out to customers and we use CallByName with optional parameters a lot.



Thanks,
Michael





Sample code to replicate this error:

CallingApplication.vbproj
-------------------------

Module CallingModule

    Sub Main()
        Dim objExec As Object = LoadByName("Assembly1.dll", "Assembly1.Broken")
        Dim o As Object = CallByName(objExec, "BrokenMethod", CallType.Method)
    End Sub

    Public Function LoadByName( _
            ByVal assemblyName As String, _
            ByVal className As String) _
            As Object

        Try
            Dim assem1 As Reflection.Assembly = Reflection.Assembly.LoadFrom(assemblyName)
            Return assem1.CreateInstance(className)

        Catch ex As Exception
            Console.WriteLine("bad times...")
        End Try

        Return Nothing

    End Function

End Module



Assembly1.VBproj
----------------

Imports System.Data.SqlClient


Public Class Broken

    Public Sub New()

        Console.WriteLine("Broken::c'tor()")

    End Sub


    Public Sub BrokenMethod(Optional ByVal badParameter As SqlConnection = Nothing)

        Console.WriteLine("BrokenMethod() has been called")

        If badParameter Is Nothing Then

            badParameter = New SqlConnection()
        End If

    End Sub

End Class


Comments

  • Options
    H- truly sorry about this. There is a long-standing bug regarding optional parameters. Can you test without optional parameters. I can test too but it might take a little longer getting my environment set up.
  • Options
    Hi,

    I forgot to mention ... this issue doesn't appear to occur with framework 3.5.

    Frameworks 4.0 and 4.5 do exhibit this unwanted behaviour

    Thanks,
    Michael
  • Options
    Hi Chris,

    Thanks for your reply
    H- truly sorry about this. There is a long-standing bug regarding optional parameters. Can you test without optional parameters. I can test too but it might take a little longer getting my environment set up.


    Unfortunately we have quite a number of places where we use late bound optional parameters.
    Just as unfortunately, we originally evaluated SmartAssembly using .net 3.5 and that was fine.

    I think I'll see what Red Gate's support team say
Sign In or Register to comment.