Sign in to follow this  

[.net] VB.Net and cross-platformness

This topic is 4589 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I don't really understand how this works, so could you please answer a few questions: 1) Linux users somehow run Windows programs. Can they also run .Net programs? Do they use Mono? 2) What is Mono? .Net framework for different systems, something like VM for Java, but for .Net, or a .Net cross-platform compiler? 3) What I should/should not use in my VB.Net program to make it at least potentially cross-platform? Thanks in advance!

Share this post


Link to post
Share on other sites
Quote:
Original post by Seroja
1) Linux users somehow run Windows programs. Can they also run .Net programs? Do they use Mono?

To run normal windows programs they typically use WINE. Running .NET programs is actually done totally differently (and is somewhat easier as well). You're right in that Mono is used to run .NET programs under Linux.

Quote:
2) What is Mono? .Net framework for different systems, something like VM for Java, but for .Net, or a .Net cross-platform compiler?

Basically, yes. It includes an implementation of the Common Language Runtime (CLR, the virtual machine like thing that .NET programs run under), but also includes a large portion of the .NET Framework (the libraries), a number of extra libraries that aren't part of the .NET Framework, and a C# compiler. I also remember hearing mention of a VB.Net compiler.

Quote:
3) What I should/should not use in my VB.Net program to make it at least potentially cross-platform?

Probably the main thing to avoid (and the most annoying) is WinForms. Last I heard very little of this had been implemented, and certainly wasn't particularly useful. There are alternatives though, for example GTK#. GTK+ is a cross-platform GUI library, and GTK# is a set of .NET binding for it (in other words, a .NET library that lets you use the non-.NET GTK+). You should also avoid using P/Invoke directly, as this is used to call OS-specific libraries.

Something probably also worth mentioning, you usually only need to compile your program once. For example, if you compile a cross-platform C# program under Visual Studio, it should run fine under any other .NET implementation without being recompiled (assuming all the required libraries are available)

Share this post


Link to post
Share on other sites
Thanks for the answers! I have a few follow-up questions:

1) Is WinForms the regular forms that are used in VB.Net designer? I mean, it opens up with a frm where you can place and position controls. If so, how are the alternatives (GTK#) used in VB? Because I'm used to working with forms visually, and have events in code, not the C-style callback.

2) I use OpenGL from VB.Net - just API calls, no frameworks. So, here are examples of a few functions I need:

Public Declare Function ChoosePixelFormat Lib "gdi32.dll" (ByVal hdc As Integer, ByRef ppfd As PIXELFORMATDESCRIPTOR) As Integer

Public Declare Function wglCreateContext Lib "opengl32.dll" (ByVal hdc As Integer) As Integer

Public Declare Function GetDC Lib "user32.dll" (ByVal hWnd As Integer) As Integer

Public Declare Function GetTickCount Lib "kernel32.dll" () As UInteger

Public Declare Sub glClear Lib "opengl32.dll" (ByVal mask As UInteger)

How should I declare those if I want it cross-platform?

3) What is P/Invoke?

4) I use System.Runtime.InteropServices.Marshal namespace. Will there be any problems about it?

5) I've heard about embedding mono - e.g. making it so that the program will run without mono installed. Is it possible in VB.Net, or do I have to use mono's VB compiler? And will it work on Windows as well, so I can run my program without .Net framework installed?

6) How do I determine what OS am I running under?

Thanks again.

Share this post


Link to post
Share on other sites
1)Yes. I know work is underway for a Windows.Forms implementation on mono, but I haven't really done anything significant there. You should probably stroll on over go-mono.com

2)You should look into the tao framework, as its cross platform. It also happens to be part of mono now.

3)Platform Invoke - Let's you use native dll's in managed code.

4)Kind of required for the above.

5)Dunno.

6)System.Environment.OSVersion

Share this post


Link to post
Share on other sites
Thanks, but I don't want to use any frameworks. The whole point was using OpenGL in pure VB, and the frameworks are written in C#. So I converted C headers to VB. It works fine, but I wonder what should I change to make it cross platform. Probably OpenGL DLL on Linux/MacOS is not opengl32.dll, but how can I make multiple declares, depending on system? Will it require me to recompile the program for each platform?

BTW, Marshal is not yet available :(

Edit: Just found out that cross-platform Tao supports WGL. How can that be possible? On Linux, GLX is supposed to be used instead, isn't it?

Share this post


Link to post
Share on other sites
I'll only touch on what POedBoy didn't

Quote:
5) I've heard about embedding mono - e.g. making it so that the program will run without mono installed. Is it possible in VB.Net, or do I have to use mono's VB compiler? And will it work on Windows as well, so I can run my program without .Net framework installed?


I imagine you'll have to use Mono's compiler. Mono isn't Linux only, you can use it in Windows instead of Microsoft's .NET implementation, so I imagine if you can do it in Linux then you can do it in Windows. There should be details on the Mono website.

Quote:
Thanks, but I don't want to use any frameworks. The whole point was using OpenGL in pure VB, and the frameworks are written in C#.

The beauty of .NET is that language doesn't matter. You can use C# libraries in a VB.Net program or VB.Net libraries in a C# program.

Quote:
It works fine, but I wonder what should I change to make it cross platform.

Rewrite Tao [smile]......and I'm actually serious. As far as OpenGL is concerned, Tao is just a wrapper which does exactly the same thing as what you'd end up writing yourself.

Quote:
Probably OpenGL DLL on Linux/MacOS is not opengl32.dll, but how can I make multiple declares, depending on system? Will it require me to recompile the program for each platform?

If you really want to know how it's done, check out the Tao source. Sure it's in C#, but so long as you can read C# you can do the same things in VB.Net. Yes, you'd need to recompile since at the very least the dynamic library names are different.

Quote:
Just found out that cross-platform Tao supports WGL. How can that be possible? On Linux, GLX is supposed to be used instead, isn't it?

You're correct. What you do is check which OS you're running under and use the appropriate functions. If you use the wrong one you'll get a runtime error saying that the library cannot be found, but it means you don't have to recompile.

Share this post


Link to post
Share on other sites
Well I am trying to rewrite Tao in VB :-) Only that I don't make it as a framework, just a few modules with lots of declares, so I call the functions directly.

In Tao they DLLImported all the functions. They just used different libraries for different systems.

Well, up to now my program is not cross-platform :-(
Hopefully they'll finish with WinForms and Marshal when I'm done :-)

Share this post


Link to post
Share on other sites
Quote:
Original post by Seroja
Well I am trying to rewrite Tao in VB :-) Only that I don't make it as a framework, just a few modules with lots of declares, so I call the functions directly.

Why though!? Someone else has already done it for you! (and btw, yours being in VB makes ABSOLUTELY NO DIFFERENCE)

Quote:
In Tao they DLLImported all the functions. They just used different libraries for different systems.

Which is exactly what you'll end up doing. Correct?

Quote:
Well, up to now my program is not cross-platform :-(
Hopefully they'll finish with WinForms and Marshal when I'm done :-)

Not likely. These expose too much of the underlying operating system (yes, even WinForms does) and would be a serious pain in the **** to implement, and very difficult to justify the time spent (especially since WinForms will be replaced by Avalon soon-ish).

Share this post


Link to post
Share on other sites
Yes - you can run .NET programs under Mono, but there are certain things which will never work:

P/Invoke into win32 DLLs - is clearly not going to work on Mono on non-win32 platforms.

(P/Invoke into platform-independent DLLs might conceivably work though)

OpenGL itself is no different, but creating contexts is.

I can't see the wgl functions *ever* being available under (for example) Linux - which of course has its own set of functions for setting up contexts etc, the "glx" functions. These are different because they're interfacing into a different underlying window system.

You could conceivably P/Invoke GLFW.dll (if indeed, it is a DLL) and opengl32.dll (libGL.so or whatever on Linux) - but I don't hold my hopes out.

Mark

Share this post


Link to post
Share on other sites
Why make it in VB:
1) Kind of a challenge for myself - to see if I can make it ALL by myself.
2) Have the whole program written in one language.
3) Have it in as global functions, and not as functions of a class.
4) Be able to change anything I want (though not really relevant as they make it work, but still makes me feel better, hate stuff I can't change crash).

I need Marshal only for pointers. VB doesn't have them properly, so I use stuff like PtrToStringAnsi. I guess there could be a way around this by using dummy functions and ByRef, but Marshal works well until now.

About WinForms, any example on creating cross-platform forms in VB? The best I can do is open a form in C-style, through API calls. But that would still be a WinForm. Or are there APIs like CreateWindow on all platforms?

And it *might* work on one compile, I think VB allows to declare functions that don't exist and doesn't throw an error as long as you don't try to use them.

As a side note, what is faster, Win32 API or .Net framework? I mean, there are many functions that are present in both, e.g. TicksCount. I know I should use .Net for cross-platformness, but won't that slow me down?

Share this post


Link to post
Share on other sites
Quote:
Original post by Seroja
Why make it in VB:
1) Kind of a challenge for myself - to see if I can make it ALL by myself.
2) Have the whole program written in one language.
3) Have it in as global functions, and not as functions of a class.
4) Be able to change anything I want (though not really relevant as they make it work, but still makes me feel better, hate stuff I can't change crash).

That's your choice. It seems like a lot of work to get it up and running and fixing bugs to get it working reliably on all platforms when Tao's already doing the same thing, but hey...

Quote:

I need Marshal only for pointers. VB doesn't have them properly, so I use stuff like PtrToStringAnsi. I guess there could be a way around this by using dummy functions and ByRef, but Marshal works well until now.

Marshal doesn't work?!?! I use Marshal.Copy when loading textures and my program works fine on Linux! Maybe it's more recently added, but I know Marshal.Copy works on Mono 1.0.5.

You should only need pointers for interacting with the underlying API, but the wrapper exposing it as a .Net function should handle that. Even if you write your own wrapper, that should be the only place you need pointers. That's if you're writing your wrapper like Tao, which to your app treats them as .Net types (I.E. glGetString would return a System.String instead of a char[]). If you write your wrapper in that style, it will be easier to use as you only do the conversions in one place isntead of everywhere you use the function.

Quote:

About WinForms, any example on creating cross-platform forms in VB? The best I can do is open a form in C-style, through API calls. But that would still be a WinForm. Or are there APIs like CreateWindow on all platforms?

If you want to write cross-platform WinForms, you should write your code on Mono and only use things that work on Mono. That will be extremely limiting, though.

You could also use a cross-platform library like GTK#. I think that would need to be installed to run on Windows, though. With GTK# you can use the stand-alone program Glade to design your windows and load that file into your .Net app to create the window at runtime. I've messed around with it a bit in C# on Linux; it's not too complicated.

Quote:

And it *might* work on one compile, I think VB allows to declare functions that don't exist and doesn't throw an error as long as you don't try to use them.

As a side note, what is faster, Win32 API or .Net framework? I mean, there are many functions that are present in both, e.g. TicksCount. I know I should use .Net for cross-platformness, but won't that slow me down?

In general, good .Net code should get ~95% of the speed of good C++ code. I don't know how that changes with win32 and WinForms, but remember that on Longhorn win32 will be legacy code, even moreso than WinForms.

Alternatively, if you choose to use GTK#, they will probably eventually write a new version that runs directly on top of the Longhorn API.

Share this post


Link to post
Share on other sites
.Net is good for the future :)

Weird about Marshal, I looked a few days ago, it said to be added later. I'll recheck that. I use it that way:

Public Declare Function glGetString Lib "opengl32.dll" (ByVal name As UInteger) As Integer

Public Function vbglGetString(ByVal name As UInteger) As String
tmpIntPtr = New IntPtr(glGetString(name))
Return PtrToStringAnsi(tmpIntPtr)
End Function

Thought there might be a better way.

I'll try to use GTK#, I'll see what it is.

Thanks for the info.

Share this post


Link to post
Share on other sites
Yay, it looks like 97% already! (which is enough for me, they seem to have trouble with several controls, but I don't use any :)

BTW, what's the .Net alternative to GetDC API (if any)? I tried to use System.Drawing.Graphics to create object from hWnd, and then get its DC, but it didn't work (it did return something, but OpenGL context didn't setup with that).

Oh, and does Mono work with .Net framework 2.0? Because I'm coding with VS.Net 2005 Beta, and backward-converting the code will just add more trouble (VS.Net 2003 really dislikes when I use unsigned integers).

Share this post


Link to post
Share on other sites
I was talking about VB.Net compiled using Visual Studio, not Mono compiler. Their VB compiler is not complete yet. I was talking about that if I compiled my project using VB.Net 2005 Beta, and run it on a system with 1.1 .Net framework, it failed displaying a message that it requires 2.0 framework. Strange enough, I don't remember using 2.0-specific features. Will this happen on Mono as well?

And as I said, I already tried to fetch DC through Graphics, and it didn't work. But I'll try again later using CreateGraphics (I did it in another way).

Mono WinForms is now supported on MacOS and X11. Now what is X11? Linux?

Share this post


Link to post
Share on other sites
Quote:
Original post by Seroja
I was talking about VB.Net compiled using Visual Studio, not Mono compiler. Their VB compiler is not complete yet. I was talking about that if I compiled my project using VB.Net 2005 Beta, and run it on a system with 1.1 .Net framework, it failed displaying a message that it requires 2.0 framework. Strange enough, I don't remember using 2.0-specific features. Will this happen on Mono as well?

And as I said, I already tried to fetch DC through Graphics, and it didn't work. But I'll try again later using CreateGraphics (I did it in another way).

Mono WinForms is now supported on MacOS and X11. Now what is X11? Linux?


X11 is the way Linux (as well as some Unixes?) do GUI apps. It's a protocol to connect to an X server (usually XFree86 or X.org), which works with the window manager and GUI apps to make the graphical interface work.

Share this post


Link to post
Share on other sites

This topic is 4589 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this