How to use a 3rd party DLL in c#

0

I have a 3rd party DLL that I need to use in a c# application (.net 4.6.1). This is a 64 bit DLL, but I have a 32 bit version as well.

The DLL is used successfully in vba (excel 64 bit) as follows: Declaration:

Private Declare PtrSafe Function TheFunction Lib "TheDLL.dll" ( _
  ByVal Param1 As String _
, ByVal Param2 As String _
, ByVal Param3 As String _
, ByVal Param4 As String _
, ByVal Param5 As String _
, ByVal Param6 As String _
, ByVal Param7 As Integer) As Variant

Usage:

Dim answer As Variant
answer = TheFunction(Param1, Param2, Param3, Param4, Param5, Param6, Param7)

In c#, i have tried using:

public class WrapperClass
{
    [DllImport("TheDLL.dll", CallingConvention=CallingConvention.Cdecl)]
    public static extern IntPtr TheFunction(param list...);
}

IntPtr resPtr = WrapperClass.TheFunction(Param list...);
object resObj = Marshal.GetObjectForNativeVariant(resPtr);
WrapperClass.VariantClear(resPtr);
Marshal.FreeCoTaskMem(resPtr);
resPtr = IntPtr.Zero;
MessageBox.Show("answer = " + resObj.ToString());

I get an error when calling TheFunction: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)

I've also tried:

public class WrapperClass
{
    [DllImport("TheDLL.dll", CallingConvention=CallingConvention.Cdecl)]
    public static extern object TheFunction(param list...);
}

object resObj = WrapperClass.TheFunction(Param list...);
MessageBox.Show("answer = " + resObj.ToString());

I get an error PInvoke restriction: cannot return variants

I've also tried the above 2 methods with CallingConvention=CallingConvention.StdCall, but I get the same error messages.

What am I doing wrong?

edit: I've tried using both the 32 and 64 bit DLLs. I've also changed my project to be x86 and x64. All combinations of these still result in the same error message.

More perplexing, if I remove the DLL from my project (in solution explorer -> right click DLL -> delete) and change my DllImport line to read as [DllImport("nonexistent.dll")], it still runs and gives the PInvoke error. I'm using Visual Studio 2017 Community.

Edit 2: Stack trace (with sensitive/private info removed):

System.Runtime.InteropServices.MarshalDirectiveException
  HResult=0x80131535
  Message=PInvoke restriction: cannot return variants.
  Source=<MyApp>
  StackTrace:
   at <MyApp>.Model.<WrapperClass>.<TheFunction>(String <param1>, String <param2>, String <param3>, String <param4>, String <param5>, String <param6>, Int32 <param7>)
   at <MyApp>.Model.PostProcess.Process() in <BaseDir>\<MyApp>\<MyApp>\Model\PostProcess.cs:line 128
   at <MyApp>.Model.PostProcess..ctor() in <BaseDir>\<MyApp>\<MyApp>\Model\PostProcess.cs:line 31
   at <MyApp>.App.Application_Startup(Object Sender, StartupEventArgs e) in <BaseDir>\<MyApp>\<MyApp>\App.xaml.cs:line 30
   at System.Windows.Application.OnStartup(StartupEventArgs e)
   at System.Windows.Application.<.ctor>b__1_0(Object unused)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.DispatcherOperation.InvokeImpl()
   at MS.Internal.CulturePreservingExecutionContext.CallbackWrapper(Object obj)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at MS.Internal.CulturePreservingExecutionContext.Run(CulturePreservingExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Windows.Threading.DispatcherOperation.Invoke()
   at System.Windows.Threading.Dispatcher.ProcessQueue()
   at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
   at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
   at System.Windows.Application.RunDispatcher(Object ignore)
   at System.Windows.Application.RunInternal(Window window)
   at <MyApp>.App.Main()
c#
excel
vba
dll
asked on Stack Overflow Nov 30, 2018 by Shirosaki • edited Dec 2, 2018 by Shirosaki

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0