No announcement yet.

C# and PowerBasic DLLs

  • Filter
  • Time
  • Show
Clear All
new posts

  • C# and PowerBasic DLLs

    I write PowerBasic DLLs for a company that write their main programs in Visual Basic 6. Now they have decided to use C# for their main programs. Can C# use PowerBasic DLLs that have worked together with VB6 without changes of the DLLs? Do I have to learn something about C#to be able to write DLLs that work with C# or is it the C# programmer that has to adjust his code?


  • #2
    The C# programmer can:

    If it is a native dll with exported
    functions, then you need to create the declarations in C# to make the call
    through the P/Invoke layer.

    What ever that means.
    Roy Cline


    • #3
      Can C# use PowerBasic DLLs that have worked together with VB6 without changes of the DLLs?
      That depends, if any of your exported functions expect a string as a parameter and not an ASCIIZ

      Do I have to learn something about C#to be able to write DLLs that work with C#
      No, but it helps

      is it the C# programmer that has to adjust his code?

      I wrote a DLL years ago in PowerBasic for use with several programming languages (PB, VB, C++, C#, VB.NET, Labview, Matlab, etc...) all of which use the same DLL that I wrote in PB. I did this because I constantly get tech calls in various languages that I do not use often enough to remain fluent in all of them.

      Doing this I was able to support them all by learning "Just Enough to make it work" in each language, and all the "Grunt Work" is done in my dll that I wrote in PB.

      If you need an example of what you may be up against, go to and click the "VXM Driver" link. That will take you to a page that you can pick examples in each language, along with documentation on how to add the dll to your own project in that language.

      It does help if you write a wrapper to your functions (like I did) for each language, but not necessary if the programmer in that language is able to do it.
      Engineer's Motto: If it aint broke take it apart and fix it

      "If at 1st you don't succeed... call it version 1.0"

      "Half of Programming is coding"....."The other 90% is DEBUGGING"

      "Document my code????" .... "WHYYY??? do you think they call it CODE? "


      • #4
        uCalc FMP C# DLL example

        I've had a PB-compiled DLL that people were technically able to use with C# without any special provisions in the DLL for C#. However, it required too much effort on the user's part. Later, I made a special include for VB.NET, and figured that once I had something for one .NET compiler, it might work just as easily for any .NET compiler. However, that still was not quite the case the way I had done it.

        Although you can force your users to adapt to your DLL, and get it to work, it might be better to do it the other way around, and learn just enough C# to get a user-friendly DLL interface (and documentation) for it, once and for all. It's not hard, but there are some considerations to keep in mind (not all of which have to do with the DLL itself):
        • A long in PB is 32 bits whereas in C#, it is 64 bits. PB's long is an int in C# (or Integer in VB.NET). This seems like a small thing, but it can be a tricky source of problems if you're not aware of it.
        • A VB.NET (or VB 6) include file cannot automatically be converted to C#. However, you can compile a wrapper DLL using VB.NET, which can work smoothly with C#.
        • If you create a wrapper DLL like this, you may as well organize it into a class to make your C# users happy.
        • If you are using pointers in C# for variables to be used by the DLL, you will want to use the unsafe C# directive for functions (or larger blocks of code) that use pointers. Otherwise the .NET GC may move things around in memory and create errors.
        • For passing string arguments from C# to a DLL exported function, the Declare in my VB.NET file uses ByVal TextArg As String, and from the PB DLL itself, that same arg is exported as ByVal TextArg As Asciiz Ptr (I'm sure that's not the only way to do it).
        • If you were using AddressOf in your VB6 code, where your DLL now calls a function in your host C# or VB.NET program, using CALL DWORD, it can be a little tricky. You will have to use Delegates. And Delegate functions don't seem to work well with string arguments. You'll see how I've worked around it if you download uCalc FMP.

        Visit and download uCalc Fast Math Parser 2.95. After unzipping it, browse through the code in the CS directory (for C#). Also look at the DotNET directory to see the code for the .NET DLL wrapper.

        Daniel Corbier
        The uCalc family of products includes: various calculators, a math parser, programming language builder, a search/replace tool, and more to come.
        Daniel Corbier
        uCalc Fast Math Parser
        uCalc Language Builder


        • #5
          Thank you all for your help.