IT-Consultant: José Roca (PBWIN 10+/PBCC 6+) (Archive only) > COM Programming

Calling .NET classes from PowerBASIC

(1/2) > >>

José Roca:
 
To use .NET classes with PowerBASIC you have to register the assembly as a  COM object with the regasm.exe tool.


--- Quote ---The Assembly Registration tool reads the metadata within an assembly and adds the necessary entries to the registry, which allows COM clients to create .NET Framework classes transparently. Once a class is registered, any COM client can use it as though the class were a COM class. The class is registered only once, when the assembly is installed. Instances of classes within the assembly cannot be created from COM until they are actually registered.
http://msdn.microsoft.com/en-us/library/tzat5yw6(VS.71).aspx

--- End quote ---

The following example creates an instance of the "System.Net.WebClient" class to download a picture from an URI and save it to a file. If system.dll is not still registered in your system, you have to do it first using regasm.exe. Both regasm.exe and system.dll are located in C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727 in my computer.


--- Code: ---' ########################################################################################
' The following example creates an instance of the "System.Net.WebClient" class to
' download a picture from an URI and save it to a file.
' ########################################################################################

#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN () AS LONG

   LOCAL owc AS DISPATCH
   LOCAL vAddress AS VARIANT
   LOCAL vFileName AS VARIANT

   owc = NEWCOM "System.Net.WebClient"

   IF ISOBJECT(owc) THEN
      TRY
         vAddress = "http://www.jose.it-berater.org/webpages_images/h_2.jpg"
         vFileName = EXE.Path$ & "h_2.jpg"
         OBJECT CALL owc.DownloadFile(vAddress, vFileName)
         owc = NOTHING
         MSGBOX "Picture saved"
      CATCH
         IF OBJRESULT = &H80020009 THEN
            MSGBOX "Error &H" & HEX$(IDISPINFO.CODE) & $CRLF & IDISPINFO.DESC$
         ELSE
            MSGBOX "Error &H" & HEX$(OBJRESULT)
         END IF
      END TRY
   END IF

END FUNCTION

--- End code ---

Petr Schreiber:
Thank you José,

I tried it on PC with .NET 3.5 installed and it worked well.


Petr

Patrice Terrier:
José,

I didn't try it yet, but for sure that is a very useful tutorial.

Thank you!

Petr Schreiber:
Nice,

not only .NET core libraries, but all "ClassLibraries" created by programmers in MSVS can be called in PB.

If anybody is interested, in MSVS C# 2008 Express you just:

* Create ClassLibrary project
* In project options /  Build / check "Register for COM interop"
* In assembly info make sure that ComVisible is enabled: "[assembly: ComVisible(true)]"
* Build the library
Worked well, tried even returning values. I built it as 3.5 assembly to see if it works just for 2.0, but no problem.
Still not sure how to make methods visible for COM Browser / TypeLibBrowser, but that is detail.

Thanks José, I would never believe it is possible :)


Petr

José Roca:
 

--- Quote ---Still not sure how to make methods visible for COM Browser / TypeLibBrowser, but that is detail.

--- End quote ---

I don't know if it is possible. Unless there is a way unknown to me, it generates dispatch only interfaces that can only be used with late binding. But at least, it works. And TRY/END TRY can be used to catch exceptions, and IDISPINFO can be used to retrieve the error code and description when OBJRESULT returns %DISP_E_EXCEPTION (&H80020009&).

Navigation

[0] Message Index

[#] Next page

Go to full version