PDA

View Full Version : ThinBasic for .NET



zak
30-08-2009, 18:39
is it possible someday ".net thinbasic" or "thinbasic for .net" ??
i know that .net classess can be called from VB6 as explained in the article:
Can I Interest You in 5000 Classes?:
http://msdn.microsoft.com/en-us/library/aa719105(VS.71).aspx
and the article:
Using the .NET Framework Class Library from Visual Basic 6:
http://msdn.microsoft.com/en-us/library/aa719110(VS.71).aspx

3 years ago i have called the .net regex from vb6 , it is a fun experience to knock the door of a giant, even i am personally do not like .net because it is slowing the pc, any way do you think that PowerBasic will ever have an edition "powerbasic for .net" !? and not just using COM and dot NET Interoperability as in the above microsoft articles?
indeed i have a trouble in understanding the role of .net in the future of windows. is it possible that win32 api will be substituted completely by .net in a future versions of windows!! and all the win32 api dependent programs will be only called using the emulation mode!!.
the days are running very quickly and i am not hurry to see what will happened in the future, as someone said do not order your children to sleep ; when they awake they will be older by a one day; and suddenly like the magic you will see them men and women. "from a movie i forgot its name"
best wishes

zak
30-08-2009, 21:06
while browsing my article i have noticed that bob zale was reading it, bob zale for me is a legendary person, at some date between the years 1986 to 1990 i have used turbo basic wich was created by him, it was at that time considered the fastest basic available. i remember there was a book using turbo basic to demonstrate many complex math and physics problems,
i wish him long and good life

ErosOlmi
30-08-2009, 22:10
zak,

I do not think I'm in the position to think about thinBasic.net. thinBasic is still in his youth and has a lot of steps to do till to become a "real man" language. It can already be used for many complex tasks, it can already do many different things in many different areas. But we still need to complete it.

That said, Windows API will be with us at least for 10 years. I say 10 years but can be 100. thinBasic works fine under Windows 7. Windows 7 is not yet on the market and Microsoft has a 10 years support rule for its OS so I say 10 years as the minimum. Also consider many .Net areas rely on Windows API. So do not worry about this point.

Before going to .Net I think I need to add native COM interface to thinBasic Core language in order to easily be used more or less like VB6 did with COM objects. But this is thinBasic future.

Ciao
Eros

MouseTrap
02-09-2009, 19:46
Not directly related, but Here is a working example of using ThinBasic with mono.

http://community.thinbasic.com/index.php?topic=2478.msg18653

Also, DLL's compiled with any .net language can be directly used by Thinbasic. So if your primary interested in getting access the librarys, that's a pretty quick way to go.

Charles Pegge
02-09-2009, 21:11
That is very interesting! Would it then be possible to create a generic dll which could mediate between thinbasic and .net software in general? Possibly 2 components: A thinBasic module invoking a C# compiled dll.

Then 300 meg Mono would no longer be required.

Charles

John Spikowski
02-09-2009, 23:35
José Roca started an interesting thread on his forum that might spark some interest here as well.

http://basic-compiler.com/forum/index.php?topic=35.0

MouseTrap
03-09-2009, 13:49
That is very interesting! Would it then be possible to create a generic dll which could mediate between thinbasic and .net software in general? Possibly 2 components: A thinBasic module invoking a C# compiled dll.

Then 300 meg Mono would no longer be required.

Charles


The 2 are different animals. The test I posted back in feb. is for mono only and lets you work with an instantiated object.
If you wanted to work with Microsoft.net, you can just load the dll in Thinbasic as any other win32 dll using "DECLARE function" and LIB
But for practical purposes your .net dll will need to be a wrapper exposing functions.
For example, If I want use a struct from .Net in some other non .net language its best to create a static .net class function that takes a struct by reference and populates it. (I'm not sure if TB types act like C structs or not in mem)
However static functions are simple to export and can even be private!

Charles Pegge
03-09-2009, 16:05
Thanks for clarifying. I did not realise that a .net dll could provide conventional export functions before you explained it.

thinBasic would have no problem in passing a UDT byref - I presume that a .net static class object would have a virtual function table pointer as its first member. So the UDT would have to provide this explicitly.

Then it would not be necessary to go via a registered COM interface.

Charles

MouseTrap
03-09-2009, 16:25
I apologize that I left out an important detail.
natively, .net dlls can only be used in .net. To use them in another languge, you need to dissasemble the dll to IL, make a tweak and re-assemble.
Doing it by hand takes about a minute, I use a tool that lets me pick what functions i want to export and handles it automatically.
Here http://www.blitzbasic.com/codearcs/codearcs.php?code=716 is a post detailing how it works.

Charles Pegge
03-09-2009, 18:05
Many thanks!

I can see that MS does not really want .net to be used with `unmanaged code`, and have not provided a simple compiler option to enable export table creation. You have to climb in through a window at the back rather than use the front door so to speak.

Charles

Petr Schreiber
03-09-2009, 18:37
MouseTrap,

thanks a lot for some very interesting information, I didn't know it was possible to do such a hacks.
Too bad MS does not provide direct .NET EXE/DLL 2 Win32 EXE/DLL conversion on their own. I guess that is their way to push developers more to .NET only way.

MouseTrap
03-09-2009, 18:42
Ya, its strange. I can understand them putting that ".corflags" trick in early on for testing or early design decision. but the interesting thing is that they never removed it. i think a lot people are using .net in that way.
I've seen people write lengthy articles about doing com interop and proxy dlls to get .net dll's to work in a __stdcall way. It always makes me smile.
I suppose you could actually use .Net to write a proxy dll for Com. That's probably how i would do it for my own personal use.

zak
04-09-2009, 06:28
during yesterday i have experimented too much with calling .Net 2 from thinbasic, this is a successfull example:
the main idea is to use com module, in my example i have used Petr example in this thread:
http://community.thinbasic.com/index.php?topic=2325.msg17671#msg17671
first: you must add vbc.exe to the path; my own path is:
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727
look for how to do so here:
http://nickmasao.wordpress.com/2008/08/12/running-a-vbnet-program-with-command-line-arguments/
second: you need gacutil.exe and sn.exe from a .Net sdk, i have copied it from another computer for sdk v 2, included with the attached file,
we need to make a .Net dll to add two numbers like this(included as wrappers.vb):

Imports System.Text.RegularExpressions
Imports System.Runtime.InteropServices

<ClassInterface(ClassInterfaceType.AutoDual)> _
Public Class addNumbers
Public addition As Integer

Public Function add(ByVal num1 As String, ByVal num2 As String) As Integer
Dim first As Integer = Cint(num1)
Dim second As Integer = Cint(num2)
addition = first+second

Return addition
End Function

End Class

proceed in the following steps:
1- copy all files with the project file (wrappers.vb) to c:\tlbs
then proceed as in the image:
7710
2- sn -k COMInterOp.snk
3- vbc /out:wrappers.dll /t:library /keyfile:COMInterOp.snk wrappers.vb
4- gacutil -i wrappers.dll
5- regasm wrappers.dll /tlb:c:\tlbs\wrappers.tlb

5- regasm wrappers.dll /tlb:c:\tlbs\wrappers.tlb
then run the TB code:


uses "com", "console"

dim objaddition as dword
dim RetVal as long
dim vResult as variant

' -- Create instance of class
objaddition = COM_CreateObject("addNumbers", RetVal)
printl "objaddition", objaddition

printl repeat$(79, "-")

' -- Put parameters for method to array of variants
%MAX_NUMBER_OF_PARAMETERS = 2
dim datain(%MAX_NUMBER_OF_PARAMETERS) as variant

datain(1) = 23
datain(2) = 15
printl "first num:", datain(1)
printl "second num:", datain(2)

IF COM_Succeeded(RetVal) THEN
printl "Object creation succeeded"

print "Calling method..."
printl IIF$(COM_Succeeded(COM_CallMethod(objaddition, "add", %MAX_NUMBER_OF_PARAMETERS, datain(1), vResult )), "Success", "Failure")

printl repeat$(79, "-")
printl "first num:", datain(1)
printl "second num:", datain(2)
printl "Result:", vResult

else
printl "Com failed, oh no... did you registered the DLL?"

end if

printl repeat$(79, "-")
COM_Release(objaddition)
waitkey


the result as this:
7711
i am using always the virtualization using free version of Returnil program, from:
http://www.returnilvirtualsystem.com/
so any undesirable change in the c:\ whatever it is, just reboot and everything is as before the change.
regards
p.s the line Imports System.Text.RegularExpressions in the project file wrappers.vb is not neccesary, so delete it.
if you want to unregister the dll use the commands:
gacutil -u wrappers
regasm /u wrappers.dll
references:
http://www.devcity.net/Articles/28/1/20020328.aspx

zak
04-09-2009, 07:36
if you want to know which .Net version you have, run the following command from MS internet explorer:
javascript:alert(navigator.userAgent)
you will get all the .Net versions you have like in the photo:
look at " .NET CLR 2.0.50727 " which is my version

Petr Schreiber
04-09-2009, 11:47
Hi Zak,

your tutorial worked well on my PC. I was little bit surprised the .NET already placed VisualBASIC .NET compiler to my system, although I have no MSVS for VB.NET installed.

I have .NET 3.x on my PC, but the 2.x route you described worked well.


Petr

Petr Schreiber
04-09-2009, 12:12
Here is little script you should save to place where you extracted Zaks archive.

It allows you to:

Automate DLL compilation and preparation mentioned in Zaks post, without need for specific directory or adding anything to path
It produces BAT file for unregistering




uses "Console", "UI", "OS", "FILE"

$libraryName = "wrappers"
dim DotNetDir as string = os_getWindowsDir+"Microsoft.NET\Framework\v2.0.50727\"

if file_exists(DotNetDir+"vbc.exe") = %FALSE then
printl "Could not locate VB.NET compiler, please select its location..."
DotNetDir = ""
DotNetDir = Dialog_OpenFile(0, "Find VBC.EXE", os_getWindowsDir+"Microsoft.NET\Framework\", "VB.NET compiler|vbc.exe", "exe", %OFN_FILEMUSTEXIST or %OFN_ENABLESIZING)

if len(DotNetDir) = 0 then
printl "No compiler found, program will terminate after key press..."
waitkey
stop
end if

DotNetDir = file_PathSplit(DotNetDir, %Path_RootPath)
printl "VBC located at "+DotNetDir
end if '

dim sBatFile as string

printl "Creating BAT file"
sBatFile = FileWithSafeScriptPath("sn.exe") + " -k " + FileWithSafeScriptPath("COMInterOp.snk") + $CRLF + _
DotNetDir+"vbc /out:"+FileWithSafeScriptPath($libraryName+".dll")+" /t:library /keyfile:"+FileWithSafeScriptPath("COMInterOp.snk")+" "+FileWithSafeScriptPath($libraryName+".vb") + $CRLF + _
FileWithSafeScriptPath("gacutil.exe") + " -i "+FileWithSafeScriptPath($libraryName+".dll") + $CRLF + _
DotNetDir+"regasm "+FileWithSafeScriptPath($libraryName+".dll")+" /tlb:"+FileWithSafeScriptPath($libraryName+".tlb")

file_save("CompileVBAndPrepareDLLForTB.bat", sBatFile)
printl "Executing BAT file"

os_shell("CompileVBAndPrepareDLLForTB.bat", 0, %OS_SHELL_SYNC)

printl
printl "Preparing unregistration script for later..."
sBatFile = FileWithSafeScriptPath("gacutil.exe") + " -u "+ $libraryName + $CRLF + _
DotNetDir+"regasm /u "+FileWithSafeScriptPath($libraryName+".dll")

file_save("UnregisterDLL.bat", sBatFile)

printl "Operation finished, you can run your thinBasic script now!"
printl "Press any key to continue"
waitkey

function FileWithSafeScriptPath( fName as string ) as string
function = $DQ+app_sourcePath+fName+$DQ
end function


EDIT: Fixed problem with "ugly" paths

zak
04-09-2009, 12:45
Hi Petr
thank you very much for your automated program, i have tried it succesfully, but it is working if the directory is simple ie: have no spaces in its name like c:\netdll but not like a folder in a desktop "c:\documnts and settings\...", it may need here a quotation "".
i am also surprised about vbc.exe , so my scipticism on what may be some program may insert such files ;make me try install .net 2 on a friend computer wich have'nt .net installed on it, and yes there is the vbc.exe on it and the folder size about 87 MB. i have tried the first command:
sn -k COMInterOp.snk
but failed with a message: the system can't execute the specified program
i do not know why, but using the supplied COMInterOp.snk the other commands are working well
your previously code was available to the rescue, since there is no docs about COM_CallMethod , COM_CreateObject , COM_Succeeded and the related.
if someone have trouble in running the sn.exe file then he can download .net sdk 2.0 - size is about 354 mb, other versions is much more.
best regards

Petr Schreiber
04-09-2009, 13:38
I fixed the script in my original post, please let me know if it is better.
I also added $libraryName to the script, so it can be used for other libraries, only thing needed is to change one string.

COM documentation is little empty now, I will try to write help for it and send it to Eros.

zak
04-09-2009, 15:50
Hi Petr
yes it is working now with any folder name, thanks

Petr Schreiber
08-09-2009, 10:48
Hi Zak,

as promised, some more COM commands are documented in latest beta (http://community.thinbasic.com/index.php?topic=2899.msg21972#msg21972).
There is still lot of missing docs on this, I am investigating it.