Recent

Author Topic: [RESOLVED] Pascal & .NET Interoperability  (Read 1640 times)

Pixy

  • New Member
  • *
  • Posts: 47
[RESOLVED] Pascal & .NET Interoperability
« 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.
« Last Edit: May 31, 2019, 01:54:22 pm by Pixy »

Saylo_49

  • New Member
  • *
  • Posts: 34
Re: Pascal & .NET Interoperability
« Reply #1 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, ...
Lazarus 2.0.2 - FPC 3.0.4 - Win32

PascalDragon

  • Hero Member
  • *****
  • Posts: 700
  • Compiler Developer
Re: Pascal & .NET Interoperability
« Reply #2 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:
  • 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 for this, though I don't know how FPC compatible it is)
  • Host the .NET runtime in your Pascal code
I tested neither of these, so you'll have to find the best approach yourself.

Pixy

  • New Member
  • *
  • Posts: 47
Re: Pascal & .NET Interoperability
« Reply #3 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 for this, though I don't know how FPC compatible it is)
  • Host 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
« Last Edit: May 21, 2019, 03:26:44 pm by Pixy »

Pixy

  • New Member
  • *
  • Posts: 47
Re: Pascal & .NET Interoperability
« Reply #4 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.  

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7583
Re: Pascal & .NET Interoperability
« Reply #5 on: May 23, 2019, 11:49:58 am »
Afaik .NET assemblies, while having .DLL extension are not compatible with normal win32 DLLs, so no.

Pixy

  • New Member
  • *
  • Posts: 47
Re: Pascal & .NET Interoperability
« Reply #6 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?
« Last Edit: May 23, 2019, 12:02:02 pm by Pixy »

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7583
Re: Pascal & .NET Interoperability
« Reply #7 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.

Pixy

  • New Member
  • *
  • Posts: 47
Re: Pascal & .NET Interoperability
« Reply #8 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.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7583
Re: Pascal & .NET Interoperability
« Reply #9 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.

Pixy

  • New Member
  • *
  • Posts: 47
Re: Pascal & .NET Interoperability
« Reply #10 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?

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7583
Re: Pascal & .NET Interoperability
« Reply #11 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?

  • C++, Delphi, Lazarus and VB6 are all win32 and live in the 32-bit Windows subsystem.  When properly prepared they can communicate with eachother
  • C++ Delphi and Lazarus also have 64-bit versions and those live in the 64-bit windows subsystems. When properly prepared they can communicate with eachother
  • Then there is .NET. with several implementations. .NET apps live within their .NET runtime environment. As you said there are several such implementations (normal, core, Xamarin etc)and -versions. Within one implementation they can talk with eachother. Maybe there are bridges between others too (like straight .NET and .NET Core) , but I don't know the details.
  • Then there are Windows Universal Apps. Originally they were based on the WinRT platform that shared some underpinnings with COM. Afaik later a WinRT<->.NET bridge was added.

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.


« Last Edit: May 23, 2019, 02:48:54 pm by marcov »

Pixy

  • New Member
  • *
  • Posts: 47
Re: Pascal & .NET Interoperability
« Reply #12 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/

Handoko

  • Hero Member
  • *****
  • Posts: 3228
  • My goal: build my own game engine using Lazarus
Re: Pascal & .NET Interoperability
« Reply #13 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

Pixy

  • New Member
  • *
  • Posts: 47
Re: Pascal & .NET Interoperability
« Reply #14 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