Use .NET Classes with PureBasic (COMmate & Disphelper)

Share your advanced PureBasic knowledge/code with the community.
User avatar
fsw
Addict
Addict
Posts: 1603
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Use .NET Classes with PureBasic (COMmate & Disphelper)

Post by fsw »

After seing on the internet that others have successfully connected their programming language with .NET I thought I give it a shot with PureBasic and translated an example...

Using the DispHelper lib (works fine):

Code: Select all

dhToggleExceptions(#True)

Procedure DownLoadWebImage(szURL.s, szFileName.s)
  Protected objHTTP.l

  objHTTP = dhCreateObject("System.Net.WebClient")
  If objHTTP
    dhCallMethod(objHTTP,".Downloadfile (%s,%s)",@szURL, @szFileName)
    dhReleaseObject(objHTTP)
    ProcedureReturn #True
  EndIf
  
EndProcedure

If DownLoadWebImage("http://www.purebasic.fr/english/styles/subsilverPlus/imageset/purebasic_logo.png", "h:\purebasic_logo.png")
  MessageRequester("Info", ".NET component called successfully.")
Else
  MessageRequester("Error", ".NET component called unsuccessfully!")
EndIf

EDIT:
see srod's code to get it to work with COMmate.
thanks srod :D


Now there is no one stopping you to use some of the .NET classes to get the job done.


Take care...
fsw
Last edited by fsw on Tue Nov 24, 2009 6:04 pm, edited 3 times in total.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Use .NET Classes with PureBasic

Post by srod »

Code: Select all

IncludePath "..\"
XIncludeFile "COMatePLUS.pbi"

 
Procedure DownLoadWebImage(szURL.s, szFileName.s)
  Define.COMateObject objHTTP
  objHTTP = Comate_CreateObject("System.Net.WebClient")
  If objHTTP
    objHTTP\Invoke("Downloadfile('" + szURL + "', '" +  szFileName + "')")
    objHTTP\Release()
    ProcedureReturn #True
  EndIf
  
EndProcedure

If DownLoadWebImage("http://www.purebasic.fr/english/styles/subsilverPlus/imageset/purebasic_logo.png", "h:\purebasic_logo.png")
  MessageRequester("Info", ".NET component called successfully.")
Else
  MessageRequester("Error", ".NET component called unsuccessfully!")
EndIf

I may look like a mule, but I'm not a complete ass.
User avatar
Kiffi
Addict
Addict
Posts: 1485
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: Use .NET Classes with PureBasic

Post by Kiffi »

srod wrote:

Code: Select all

objHTTP = Comate_CreateObject("System.Net.WebClient")
doesn't work for me.
COMate_GetLastErrorDescription wrote:Invalid progID/CLSID. Check your spelling of the programmatic identifier. Also check that the component / ActiveX control has been registered.
I would be surprised if it were that simple.

Greetings ... Kiffi
Hygge
User avatar
fsw
Addict
Addict
Posts: 1603
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Re: Use .NET Classes with PureBasic

Post by fsw »

Kiffi wrote:
srod wrote:

Code: Select all

objHTTP = Comate_CreateObject("System.Net.WebClient")
doesn't work for me.
COMate_GetLastErrorDescription wrote:Invalid progID/CLSID. Check your spelling of the programmatic identifier. Also check that the component / ActiveX control has been registered.
I would be surprised if it were that simple.

Greetings ... Kiffi
maybe you have to register the dll first.

The command line is:
regasm System.dll
regasm should reside in your .net directory (2.0 dir on my system, even if 3.5 is installed)

Hope this gets you going.
User avatar
Kiffi
Addict
Addict
Posts: 1485
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: Use .NET Classes with PureBasic

Post by Kiffi »

fsw wrote:Hope this gets you going.
:shock: It works!

I never thought, that it is so easy to call a function out of a dotnet-assembly.

I've always assumed that this is not possible because dotnet-DLLs have no COM-Interfaces.

Thanks a lot for your hint & Greetings ... Kiffi
Hygge
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Use .NET Classes with PureBasic (COMmate & Disphelper)

Post by srod »

@fsw, did the COMate code work okay? I can't test as I cannot locate the system.dll assembly even though IE8 tells me I have .Net 2, 3 and 3.5 installed; none of which I have actually installed myself! :)
I may look like a mule, but I'm not a complete ass.
LuckyLuke
Enthusiast
Enthusiast
Posts: 181
Joined: Fri Jun 06, 2003 2:41 pm
Location: Belgium

Re: Use .NET Classes with PureBasic (COMmate & Disphelper)

Post by LuckyLuke »

@srod : COMate code is okay.
Location of System.dll : C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727

LuckyLuke
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Use .NET Classes with PureBasic (COMmate & Disphelper)

Post by srod »

LuckyLuke wrote:@srod : COMate code is okay.
Location of System.dll : C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727
Got it. Thanks.

:)
I may look like a mule, but I'm not a complete ass.
User avatar
Rings
Moderator
Moderator
Posts: 1435
Joined: Sat Apr 26, 2003 1:11 am

Re: Use .NET Classes with PureBasic (COMmate & Disphelper)

Post by Rings »

very nice
SPAMINATOR NR.1
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Re: Use .NET Classes with PureBasic (COMmate & Disphelper)

Post by SFSxOI »

Oooooo...ahhhhh...I can't wait to try this. Thank you very much :)
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
User avatar
Kiffi
Addict
Addict
Posts: 1485
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: Use .NET Classes with PureBasic (COMmate & Disphelper)

Post by Kiffi »

Code: Select all

Define.COMateObject oForm

oForm = COMate_CreateObject("System.Windows.Forms.Form")

If oForm
  
  oForm\SetProperty("Text = 'I am a .Net-Window!'")
  oForm\Invoke("ShowDialog()")
  oForm\Release()
  
Else
  Debug "?"
  Debug COMate_GetLastErrorDescription()
EndIf
8)

Unfortunately i find no solution for adding controls (button, etc.) to the form.controls-collection :(

Greetings ... Kiffi
Hygge
User avatar
fsw
Addict
Addict
Posts: 1603
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Re: Use .NET Classes with PureBasic (COMmate & Disphelper)

Post by fsw »

Kiffi wrote: Unfortunately i find no solution for adding controls (button, etc.) to the form.controls-collection :(

Greetings ... Kiffi
There seems to be a limitation in regards of classes that can (or cannot) be used by non .NET languages.

Apparently there are some static classes that cannot be accessed that easily (if ever...).

:|
User avatar
fsw
Addict
Addict
Posts: 1603
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Re: Use .NET Classes with PureBasic (COMmate & Disphelper)

Post by fsw »

srod wrote:
LuckyLuke wrote:@srod : COMate code is okay.
Location of System.dll : C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727
Got it. Thanks.

:)
Yes srod,
worked like a charm.

Don't know how far this can be pushed though...

Thanks
User avatar
fsw
Addict
Addict
Posts: 1603
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Re: Use .NET Classes with PureBasic (COMmate & Disphelper)

Post by fsw »

srod wrote:... I can't test as I cannot locate the system.dll assembly even though IE8 tells me I have .Net 2, 3 and 3.5 installed; none of which I have actually installed myself! :)
On my machine (running WindowsXPsp3) there are 2 located at:
c:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\
and
c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\

fsw
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Use .NET Classes with PureBasic (COMmate & Disphelper)

Post by luis »

Interesting, nice work !

Thank you :)
"Have you tried turning it off and on again ?"
A little PureBasic review
Post Reply