3

I have written a selenium script in C# and now I want to expose all functions and use them all in c++ code. My point is to deliver a dll to sb who wants to use this dll in pascal (delphi). The C# code is a complete code and all I want to do is to make a bridge between my C# and pascal. I found out that pascal is an unmanaged language so C# code, which is a managed one, can not be consumed in pascal. For that reason, I started to write a c++ dll that uses my C# dll and makes a bridge between my C# and pascal code. I've read all available topics on stackoverflow and these are what I've found useful:

  1. expose my functions using COM
  2. using c++/cli to communicate with my C# dll.

I started with the first one. I was able to create COM visible dll and use that in my c++ native code but I had problems with data types. I found it hard and moved on to the second option. But I'm not sure if I can wrap all my functions like that and let unmanaged languages use them. a sample of my c# code:

namespace MainEngine
{
public static class StartMainEngine
{
        public static void test_pascal(out string _result)
        {
            _result = "Hello World!";
        }
}
}

my c++/cli code(I have referenced to my dll and visual studio shows me all functions inside my dll. So, It's not a dll import issue):

#using<ManagedLibrary.dll>
using namespace MainEngine;
char* r;
StartMainEngine::test_pascal(r);

Please guide me through this and let me know if I'm doing this in the correct way to be able to consume this dll in pascal.

AliTab
  • 31
  • 4
  • Asuming you use Delphi (pascal), COM should be your best bet. Maybe you need to rewrite the C# API a bit to fix the datatype issues. Which ones are you having problems with right now? – Pepijn Kramer Aug 06 '22 at 05:45
  • Hey @PepijnKramer Thanks for your comment. I'm not able to pass c# out strings (in my c# function arguments) to my c++. I tried with [MarshalAs(UnmanagedType.BStr)] in my C# function declaration but still can not work with that in my c++ code. Also, all my functions return bool and show if the function did its job successfully but again can not pass that to my c++ code. Also I have one Object class which I'm passing to my c# funtions and again it's data type is just Object in my c++ code and not my class name. – AliTab Aug 06 '22 at 14:12
  • 1
    I think I see what your problem is. Your methods should not return a bool, nor have out parameters. Throw an exception in C# if things fail, just return the value if they succeed. E.g. C# : String GetName(); Then enable the COM generation/registration in C# and you will not need a C++ layer. The sequence will then be C#/Exception -> COM/HRESULT -> Delphi/Exception. The resulting C#/COM dll should be registered on the client PC at installation time. And Delphi I think should import the dll (typelibrary). – Pepijn Kramer Aug 06 '22 at 14:24
  • Thanks @PepijnKramer for your straightforward explanation. When I import C# COMvisible dll in my C++ code and create a dll of my c++ code, they all will be packed together into 1 dll. Am I right? Then, I have to COM register the final dll (c++ dll I mean)? – AliTab Aug 06 '22 at 23:35
  • And one more question, can you guide me through how to send data types, especially strings with an example? Many thanks! – AliTab Aug 06 '22 at 23:41
  • I don't have the time to make an example project (and I don't have Delphi). But this is a nice start https://aakinshin.net/posts/wrap-cs-in-com/. You can just add a method `string GetVersion()` or something and test that from Delphi too. If you use developer studio then you can create guids from the `Tools->Generate Guid` meu – Pepijn Kramer Aug 07 '22 at 03:45

1 Answers1

0

It's possible using COM, CLR or IPC

Full answer you can find in this topic here