Home > Enterprise >  error when call the unmanaged resouce functions from c#.net core dll into vb.netcore windows applica
error when call the unmanaged resouce functions from c#.net core dll into vb.netcore windows applica

Time:06-22

I got Issues when I call unmanaged resource functions from C#.net core DLL into VB.net core windows application. The program execution just stops suddenly when the function is hit.

Please below is my code

DLL Unmanaged function

using System;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;

namespace TestLibrary1
{
  public class Class1
  {
    [UnmanagedCallersOnlyAttribute]
    public static IntPtr AddNE(IntPtr a)
    {
      try
      {
        IntPtr add = a;
        return add;
      }
      catch (Exception ex)
      {
        Console.WriteLine("Exception: "   ex.Message);
      }
      return (IntPtr)0;
    }
  }
}

VB.netcore windows code below to call dll

Imports System.Runtime.InteropServices
Imports Google.Apis.Calendar.v3.Data

Public Class Form1
  Private Const DllName As String = "TestLibrary1NE.dll"

  <DllImport(DllName)>
  Private Shared Function AddNE(ByVal a As IntPtr) As IntPtr
  End Function
  Private Sub btnSend_Click(sender As Object, e As EventArgs) Handles btnSend.Click
    Try
      Dim ptrAddress As IntPtr = Marshal.StringToHGlobalAnsi(txtAddress.Text.Trim())
      Dim ptrResponse As IntPtr = AddNE(ptrAddress)
      Dim strResponse As String = Marshal.PtrToStringAnsi(ptrResponse)
      MessageBox.Show(strResponse)
    Catch ex As EntryPointNotFoundException
      Console.WriteLine(e.ToString())
    End Try
    End
  End Sub
End Class

When the function AddNE (from the C# DLL) above is hit it suddenly stops the execution from nowhere.

May I know what is the solution to fix this and how to find out the error?

CodePudding user response:

The DNNE package needs that 3 files be distributed together:

  1. The managed assembly (TestLibrary1.dll);
  2. The native assembly (TestLibrary1NE.dll);
  3. And the TestLibrary1.runtimeconfig.json.

It's not mentioned in the question but the code uses the DNNE package to create a native library that exports all marked functions with the attribute UnmanagedCallersOnly.

The catch is that both managed assembly (TestLibrary1.dll) and native assembly (TestLibrary1NE.dll) should be distributed together. And not only that but also the TestLibrary1.runtimeconfig.json. This is because the native assembly calls the managed assembly internally so all the files should be on the same path.

And to generate the TestLibrary1.runtimeconfig.json you should include the EnableDynamicLoading as true in the C# project configuration file (TestLibrary1.csproj). Take a look below:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <RootNamespace>c_sharp</RootNamespace>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>

    <!-- Include the line below to generate the runtimeconfig.json -->
    <EnableDynamicLoading>true</EnableDynamicLoading>

  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="DNNE" Version="1.0.32" />
  </ItemGroup>

</Project>

CodePudding user response:

I faced one more issue related to above.if i want to call grpc functions from dll, i am facing same issue like execution stops.I just added grpc.aspnetcore dll in c# dll code and written one grpc function [UnmanagedCallersOnlyAttribute] public static void GetGrpcaddressNE(IntPtr ptrAddress) { try { string strAddress = Marshal.PtrToStringAnsi(ptrAddress);

            channel = GrpcChannel.ForAddress(strAddress);
        }
        catch (Exception ex)
        {
            WriteLog("ExceptionLog.txt"   ex.Message);
        }
    }

and trying to call above function from vb.netcore windows application client.but still got old issue like execution stops.i thought this issue is coming when i add grpc.aspnetcore dll in dll code

  • Related