Lazarus

Programming => General => Topic started by: Pixy on May 21, 2019, 01:28:35 am

Title: [RESOLVED] Pascal & .NET Interoperability
Post by: Pixy on May 21, 2019, 01:28:35 am
Is it possible to combine Pascal with Visual Basic? At least I would like to read a variable from one language in the other.
Title: Re: Pascal & .NET Interoperability
Post by: Saylo_49 on May 21, 2019, 02:40:48 am
I know this forum is dedicated exclusivly to Lazarus Freepascal
And I know this is like a treason to our Lazarus

But I suggest to you the PascalABC.net (under GPL)

http://pascalabc.net/en/

a RAD builder IDE created by a russian university that use the Microsoft .NET framework
so you will use pascal like a VisualStudio

P.S : be careful, little syntax changed in Pascal to support .NET, like the Foreach, Namespace, ...
Title: Re: Pascal & .NET Interoperability
Post by: PascalDragon on May 21, 2019, 09:10:39 am
Is it possible to combine Pascal with Visual Basic? At least I would like to read a variable from one language in the other.
There are multiple ways you can achieve this:
I tested neither of these, so you'll have to find the best approach yourself.
Title: Re: Pascal & .NET Interoperability
Post by: Pixy on May 21, 2019, 02:47:20 pm
Is it possible to combine Pascal with Visual Basic? At least I would like to read a variable from one language in the other.
There are multiple ways you can achieve this:
  • Use two processes and communicate between them using an IPC mechanism (for example pipes)
  • Put your Pascal code into a library and P/Invoke that from your VB code
  • Decorate your VB classes as ComVisible and import them as a type library in your Pascal code (you might want to check JclDotNet (https://github.com/project-jedi/jcl/blob/master/jcl/source/windows/JclDotNet.pas) for this, though I don't know how FPC compatible it is)
  • Host (https://code.msdn.microsoft.com/CppHostCLR-e6581ee0) the .NET runtime in your Pascal code
I tested neither of these, so you'll have to find the best approach yourself.

Hmm, interesting methods. I was thinking of the library options. Adding a DllImport like
:
Code: Pascal  [Select]
  1. Imports System.Runtime.InteropServices
  2.  
  3. <DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
  4.  

but I will most likely encounter errors. Will try this:
https://docs.microsoft.com/en-us/dotnet/framework/interop/consuming-unmanaged-dll-functions
Title: Re: Pascal & .NET Interoperability
Post by: Pixy on May 23, 2019, 11:36:40 am
Is there a way to communicate between two programs? (One running Pascal and the other VB) For instance, I have a Pascal program which calls a function from a .dll written in Pascal. The function "Start" is called like this:
Code: Pascal  [Select]
  1. Start(ExePath + '\a.dll', @Start, True);
  2.  
and in dll there is the corresponding function. Is it possible to have a.dll written in VB instead of Pascal? So a.dll will be like:
Code: Pascal  [Select]
  1. Public Shared Function Start(ByVal X, ...)
  2. End function
  3.  
Title: Re: Pascal & .NET Interoperability
Post by: marcov on May 23, 2019, 11:49:58 am
Afaik .NET assemblies, while having .DLL extension are not compatible with normal win32 DLLs, so no.
Title: Re: Pascal & .NET Interoperability
Post by: Pixy on May 23, 2019, 11:53:37 am
Afaik .NET assemblies, while having .DLL extension are not compatible with normal win32 DLLs, so no.

There are different types of libraries like Framework, Universal, Standard etc... Supposedly there's a type of library that works cross-platform
https://docs.microsoft.com/en-us/dotnet/standard/cross-platform/cross-platform-development-with-the-portable-class-library
https://stackoverflow.com/questions/42939454/what-is-the-difference-between-net-core-and-net-standard-class-library-project
I don't know if that solves what you are saying, but could this work?
Title: Re: Pascal & .NET Interoperability
Post by: marcov on May 23, 2019, 12:02:44 pm
Afaik .NET assemblies, while having .DLL extension are not compatible with normal win32 DLLs, so no.

There are different types of libraries like Framework, Universal, etc... Supposedly there's a type of library that works cross-platform

Much what I know of .NET is from 2005 when I briefly had a  .NET job, but it looks like all variants of .NET, except "Windows universal" which are Windows 10 Apps, which is native but afaik com based (?), but anyway, the missing is the one that you need, Winapi apps aka win32/win64.

So no, those links don't help at all. It only shows .NET fragmentation.

You really need to research Sven's recommendations.
Title: Re: Pascal & .NET Interoperability
Post by: Pixy on May 23, 2019, 12:15:21 pm
Afaik .NET assemblies, while having .DLL extension are not compatible with normal win32 DLLs, so no.

There are different types of libraries like Framework, Universal, etc... Supposedly there's a type of library that works cross-platform

Much what I know of .NET is from 2005 when I briefly had a  .NET job, but it looks like all variants of .NET, except "Windows universal" which are Windows 10 Apps, which is native but afaik com based (?), but anyway, the missing is the one that you need, Winapi apps aka win32/win64.

So no, those links don't help at all. It only shows .NET fragmentation.

You really need to research Sven's recommendations.

I see. Yes I do research, but they are very new things to me and I hardly understand them. I looked at
http://wiki.freepascal.org/Using_Pascal_Libraries_with_.NET_and_Mono#A_simple_VB.NET_app
and I thought of calling the Pascal .dll and then when the function is called it should in turn call a function in VB .dll. (Program -> P.dll -> VB.dll) So, maybe I need the opposite of this, but even so I think the VB code will not be compatible with the Pascal program, because, ultimately, the .dll and the program communicate with each other.
Title: Re: Pascal & .NET Interoperability
Post by: marcov on May 23, 2019, 01:52:15 pm
Again, a vb.net "dll" and a FPC ".dll" are not the same, even though they have the same extension.
Title: Re: Pascal & .NET Interoperability
Post by: Pixy on May 23, 2019, 02:10:32 pm
Again, a vb.net "dll" and a FPC ".dll" are not the same, even though they have the same extension.

Sure. But then again, that is why I am asking about interop. I'm just considering that in the end every code becomes 1011.. and maybe I can find a way to exchange data between two processes which use different languages. I'm also thinking about how other programs written in Delphi access and communicate with C++ programs, even though most of them are "hacks". About the Winapi thing that you mentioned, I am under the impression that I have seen it in vb6 code. Could you explain it a bit more?
Title: Re: Pascal & .NET Interoperability
Post by: marcov on May 23, 2019, 02:27:59 pm
Again, a vb.net "dll" and a FPC ".dll" are not the same, even though they have the same extension.

Sure. But then again, that is why I am asking about interop. I'm just considering that in the end every code becomes 1011.. and maybe I can find a way to exchange data between two processes which use different languages. I'm also thinking about how other programs written in Delphi access and communicate with C++ programs, even though most of them are "hacks". About the Winapi thing that you mentioned, I am under the impression that I have seen it in vb6 code. Could you explain it a bit more?


Anyway all these groups are roughly different targets, and can't call eachother directly. In some cases the road via COM is preprepared and less visible, but still, the only thing in common is COM.


Title: Re: Pascal & .NET Interoperability
Post by: Pixy on May 23, 2019, 02:33:40 pm
Nice explanation, thank you very much. From what I understand I should research COM https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/com-interop/
Title: Re: Pascal & .NET Interoperability
Post by: Handoko on May 23, 2019, 03:14:12 pm
Is it possible to combine Pascal with Visual Basic? At least I would like to read a variable from one language in the other.

Maybe what you wanted to say was to combine Pascal Lazarus with Visual Basic.

Have you tried IPC? It is relatively easy and as @PascalDragon said, you can use it to allow 2 process to communicate. If you interested, read here:

https://forum.lazarus.freepascal.org/index.php/topic,44154.msg310153.html#msg310153
Title: Re: Pascal & .NET Interoperability
Post by: Pixy on May 23, 2019, 03:34:16 pm
Well, I think the program is compiled with Delphi 2010 studio, but the syntax is the same with FreePascal. I don't really understand which language the program uses, maybe Delphi's object is FreePascal or something like that.

I have no control over the program, but I have control of the .dll and its function that the program calls. In my understanding, with SimpleIPC I would be able to communicate with two dlls one in FP and the other in VB. So, when the function is called, instead of showing delphi form from FP dll I will call another function from VB dll to show VB form.

Ideally, I would like to replace FP dll with VB dll and communicate directly, but if that's not possible I will use two dlls. SimpleIPC sounds promising, I will check it out along with COM. http://wiki.lazarus.freepascal.org/SimpleIPC_Library
Title: Re: Pascal & .NET Interoperability
Post by: devEric69 on May 24, 2019, 10:32:33 am
Quote
Is it possible to combine Pascal with Visual Basic? At least I would like to read a variable from one language in the other.

A few 2-cent ideas:
- to dialogue via creation\deletion of text file names, names that emulate a rustic protocol.
- if .net supports the SOAP protocol, then it's a way to communicate between 2 programs written in different languages.
Title: Re: Pascal & .NET Interoperability
Post by: Pixy on May 30, 2019, 03:26:11 pm
How about external? Can I show my VB form with

Code: Pascal  [Select]
  1. function ShowForm: boolean; stdcall; external 'C:\...\VB.dll';
  2.  

?
Title: Re: Pascal & .NET Interoperability
Post by: PascalDragon on May 31, 2019, 09:03:53 am
If you mean VB.net, then no, you can't. You need to use one of the points I mentioned.
Title: Re: Pascal & .NET Interoperability
Post by: jamie on May 31, 2019, 12:54:53 pm
I suppose one could check to see If the app has any OLE abilities or do what I have done a few times in the
past and that was get the menu handles of the app and send messages to it to trigger the functions..
Title: Re: Pascal & .NET Interoperability
Post by: Pixy on May 31, 2019, 01:54:08 pm
I see. Well, thank you very much guys, the only effective way seems to be COM with OleVariants. I still have issues, but I managed to show my form. I mark this topic as resolved. Special thanks to Dragon.