0

I have a strange behaviour of a clr application with .NET 6.0

C# Dll CSharpDll CSharpClass.cs

namespace CSharpDll
{
    public class CSharpClass
    {
        public double Sum(double a, double b)
        { 
            return a + b; 
        }
    }
}

C++ DLL clr.h

#pragma once

extern "C"
{
    __declspec(dllexport) double __cdecl sum(double a, double b);
}

clr.cpp

#include "pch.h"
#include "clr.h"

#using "CSharpDll.dll"


double sum(double a, double b)
{
    CSharpDll::CSharpClass^ pInstance = gcnew CSharpDll::CSharpClass();

    return pInstance->Sum(a, b);

}

C++ application clrapp.cpp

#include \<iostream\>

#include "clr.h"

int main(int argc, char** argv)
{
    std::cout << "Sum is equal: " << sum(17.0, 8.5) << std::endl;
}

I can compile it with enter image description here

Running in the debugger a get an exception. Unhandled exception at 0x00007FFC7CD6CD29 (KernelBase.dll) in ClrApp.exe: 0xE0434352 (parameters: 0xFFFFFFFF80070002, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x00007FFBA3A90000).

If I put everything (all dlls and exes) into a directory I get this error.

D:\Users\c565698\Documents\Git\ClrApp\Test>clrapp Unhandled exception. System.IO.FileNotFoundException: Could not load file or assembly 'CSharpDll, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified. File name: 'CSharpDll, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' at sum(Double a, Double b)

Although the ClrDll.dll is contained in the directory this message file not found occours. Is there some missing?

23.11.2022  10:19    <DIR>          .
23.11.2022  10:19    <DIR>          ..
23.11.2022  10:33            68’096 ClrApp.exe
23.11.2022  10:33         1’241’088 ClrApp.pdb
23.11.2022  10:17               147 ClrApp.runtimeConfig.json
23.11.2022  10:37               410 ClrDll.deps.json
23.11.2022  10:32           121’856 ClrDll.dll
23.11.2022  10:32               715 ClrDll.exp
23.11.2022  10:32             1’666 ClrDll.lib
23.11.2022  10:32           856’064 ClrDll.pdb
23.11.2022  10:32               147 ClrDll.runtimeconfig.json
23.11.2022  10:32               419 CSharpDll.deps.json
23.11.2022  10:32             4’096 CSharpDll.dll
23.11.2022  10:32            10’344 CSharpDll.pdb
24.10.2022  23:48           152’240 Ijwhost.dll
23.11.2022  10:19               320 runtimeConfig.json
ora
  • 115
  • 1
  • 1
  • 12

1 Answers1

-1

Maybe you need to register the DLL in windows ?

Click Start > All Programs > Accessories and right-click on "Command Prompt" and select "Run as Administrator" OR in the Search box, type CMD and when cmd.exe appears in your results, right-click on cmd.exe and select "Run as administrator" At the command prompt, enter: REGSVR32 "PATH TO THE DLL FILE"

Also, according to Microsoft doc:

https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/interop/example-com-class

https://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2010/w29wacsy(v=vs.100)

To register a component for COM interop With a project selected in Solution Explorer, on the Project menu, click Properties.

Click the Compile tab in Visual Basic. Click the Build tab in C#.

Select the Register for COM interop check box.

And you will need to add some interop attributes:

using System.Runtime.InteropServices;

namespace project_name
{
    [Guid("EAA4976A-45C3-4BC5-BC0B-E474F4C3C83F")]
    public interface ComClass1Interface
    {
    }

    [Guid("7BD20046-DF8C-44A6-8F6B-687FAA26FA71"),
        InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    public interface ComClass1Events
   {
   }

    [Guid("0D53A3E8-E51A-49C7-944E-E72A2064F938"),
        ClassInterface(ClassInterfaceType.None),
        ComSourceInterfaces(typeof(ComClass1Events))]
    public class ComClass1 : ComClass1Interface
   {
   }
}
Gaël James
  • 157
  • 13