PlanetSquires Forums

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: How to write a simple DLL  (Read 497 times)

ChrisC

  • FireFly3 User
  • Little Newbie FireFly
  • *
  • Posts: 27
How to write a simple DLL
« on: July 31, 2018, 02:06:54 AM »

Hello All

i have been trying to write a simple DLL in FB but was not successful

the below DLL was working ok in oxygenbasic but not in FB

Code: [Select]
#include once "windows.bi"
 

extern "Windows-MS"
'declare Function RetString(stwant as string) as string
'end RetString(null)

'========================================
' Returns a wanted string back to a c# calling program
' Note that  do NOT change string to any other type
Function RetString alias "RetString"(stwant as string) as string export
If LCASE(TRIM(stwant)) = "one" then
   return  "First string (one)  was entered"
else
   return  " Other string was entered"
end if
end function

end extern


Each time C# calling program will return error message "DLL entry point not found"

so i tried using  DECLARE statement and again no success

tried using EXTERN also not successful

and lastly tried using ALIAS  also not successful

How to resolve this program.

The below is the C# calling program
Code: [Select]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

// need this to run DllImport
using System.Runtime.InteropServices;

namespace GitO2Data
{
    public partial class Form1 : Form
    {
        //  Note that this program is compiled to 64 bits while its FB dlls are also compiled to 64 bits
        //  setup the DLLs
        [DllImport("DllFirst.DLL", EntryPoint = "RetString")]
            [return: MarshalAs(UnmanagedType.AnsiBStr)]
          public static extern string RetString([MarshalAs(UnmanagedType.AnsiBStr)]
             ref string Seltxt);

        // reference  https://stackoverflow.com/questions/16332701/how-to-call-c-dll-in-c-sharp

        [DllImport("Greet.dll", EntryPoint = "greet")]
        public static extern int greet(string rfst);

        public Form1()
        {
            InitializeComponent();
        }

        // Button to get Message from an  FB DLL
        private void button1_Click(object sender, EventArgs e)
        {
             // display the greeting
            string txtmynam = "Good Man";
            int dummynum = greet(txtmynam);

            MessageBox.Show(" number from FB  : " + dummynum.ToString() );
         }


        // Button to get a string from an  FB DLL
        private void button2_Click(object sender, EventArgs e)
        {
            string txtReturn = "";
            // Obtain the entered text
               string EnterText = textBox1.Text.Trim();

            // get the return string
               txtReturn = RetString(ref EnterText);

            // Display the return string on the screen
             MessageBox.Show(" String  from FB  dll : " + txtReturn);
        }
    }
}

Logged

Josť Roca

  • FireFly3 Registered User
  • Master Member
  • *
  • Posts: 3109
    • Jos
Re: How to write a simple DLL
« Reply #1 on: July 31, 2018, 10:22:57 AM »

FreeBasic strings aren't compatible with C# strings. You may need to use the WinAPI functions SysAllocStringBytelen or SysAllocString and SysFreeString. But I never have used C#, so I don't know all the details.

I'm afraid that you aren't going very far with all that mixing of O2, C# and FreeBasic without still not knowing how to work with FreeBasic. This compiler is too low level for you. I wonder why you don't use a higher level language like VB or C#.

ChrisC

  • FireFly3 User
  • Little Newbie FireFly
  • *
  • Posts: 27
Re: How to write a simple DLL
« Reply #2 on: July 31, 2018, 12:12:07 PM »

thanks Jose

C# and VB.Net produces IL (intermediate language) upon compile. these IL are just clear text and are readable
and very easily reverse engineer.

 so any application written in C# can be easily reverse engineer and your source code would be stolen



whereas C++  ,FB , O2  compile assemblies in native machine codes, reverse engineering them is very difficult
if not impossible.  your source code won't be stolen or can be stolen with much difficulty

see https://stackoverflow.com/questions/14285025/why-is-difficult-to-disassemble-native-win32-but-easy-to-disassemble-net-app

C# is like an interpreter language so its execution is much slower than  machine codes
Logged

ChrisC

  • FireFly3 User
  • Little Newbie FireFly
  • *
  • Posts: 27
Re: How to write a simple DLL
« Reply #3 on: July 31, 2018, 12:23:36 PM »

That is why my boss who is a PB programmer, uses PB to compile his applications which is compile
to native machine codes. but bcos of users demand, he needs to provide  64bits applications.

so converting from PB to FB and O2 is then suitable as these can compile to native codes.

C# which can provide a nice interface would be suitable for the front end and all backend computations
would be done in FB or O2

 
Logged

Marc Pons

  • FireFly3 Registered User
  • Junior Member
  • *
  • Posts: 133
  • FF3 User
Re: How to write a simple DLL
« Reply #4 on: July 31, 2018, 12:35:28 PM »

@ChrisC

So why not use only FB, with Josť includes + Paul ide and winapi you will have all you need

Marc
Logged

Pierre Bellisle

  • FireFly3 User
  • Junior Member
  • *
  • Posts: 83
Re: How to write a simple DLL
« Reply #5 on: July 31, 2018, 12:53:05 PM »

Logged

Josť Roca

  • FireFly3 Registered User
  • Master Member
  • *
  • Posts: 3109
    • Jos
Re: How to write a simple DLL
« Reply #6 on: July 31, 2018, 01:02:19 PM »

-
 FBWiki

All these simple samples only use numbers and "conveniently" avoid the problems os using dynamic strings. FB dynamic strings are proprietary and incompatible with other languages. Therefore, you have to use standard null terminated strings or learn how to do conversions. Don't expect to export the result of a function AS STRING and that it will work when used with another language.

Josť Roca

  • FireFly3 Registered User
  • Master Member
  • *
  • Posts: 3109
    • Jos
Re: How to write a simple DLL
« Reply #7 on: July 31, 2018, 01:07:49 PM »

@ChrisC

So why not use only FB, with Josť includes + Paul ide and winapi you will have all you need

Marc

Low level programming takes years of study and practice. Beginners want to start to write applications without first learning the language. Converting from one language to another is even more difficult because you need to know very well both languages. You can't take the original code, change some things without knowing what you're doing and expect that it will work.

Marc Pons

  • FireFly3 Registered User
  • Junior Member
  • *
  • Posts: 133
  • FF3 User
Re: How to write a simple DLL
« Reply #8 on: July 31, 2018, 01:22:12 PM »

Josť

I agree

i should better say :
with Josť includes + Paul ide and winapi you will have all you need + knowledge + passion + time


i remember what I've done in the past on same subject http://www.jose.it-berater.org/smfforum/index.php?topic=4599.msg17535#msg17535
Logged

ChrisC

  • FireFly3 User
  • Little Newbie FireFly
  • *
  • Posts: 27
Re: How to write a simple DLL
« Reply #9 on: July 31, 2018, 03:02:18 PM »


Quote
All these simple samples only use numbers and "conveniently" avoid the problems os using dynamic strings. FB dynamic strings are proprietary and incompatible with other languages. Therefore, you have to use standard null terminated strings or learn how to do conversions. Don't expect to export the result of a function AS STRING and that it will work when used with another language.

thanks a lot,  Jose

in that case, i will devise ways to send coded integers or longs to and from FB dll  and do not use any FB strings

inside the FB dll , the strings are coded into a bunch of coded integers

so inside the C# calling program, i would use a decoding routine to change the transmitted integers from FB DLL
to strings , i don't see any problems now

Logged