April 28, 2024, 08:40:52 AM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Creating a DLL file

Started by EvangelMike, February 27, 2009, 08:56:09 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

EvangelMike

Greetings! I am trying to create a DLL File. I have some experience with VBA but am totally new as regards Basic.

The book Professional Excel Development tells how to create a DLL as follows:

Open VB 6 and do the following:

1. Select ActiveX DLL Project Type.
2. Click Open, and VB6 creates a new ActiveX Project.
3. Name the project "AFirstDLLProject."
4. Name the class module CHelloWorld, and then saved the project.
5. Open the class module and add the following code:

Public Sub ShowMessage()
   MsgBox "Hello World!"
End Sub

7. Using the File Menu, Select File > Make AFirstDLLProject.dll
8. In the directory will be the file AFirstDLLProject.dll

I tried to duplicate these steps in Emergence Basic as follows:

I opened Emergence Basic and did the following:

1. Selected File > New Project.
2. Named the project "AFirstDLLProject."
3. For Project Type, I selected "Windows DLL
4. The Project Path is "C:\Program Files\EBDev\projects"
I thought the Output File would be AFirstDLLProject.dll, but it is not; instead it is
AFirstDLLProject.exe. Should I change the extension to .dll? I decided to just continue without
changing the extension.
5. I then clicked OK
6. It is not clear to me how I would create the class module CHelloWorld as in VB 6 step 4 above. So I
proceeded by selecting File > New > Emergence Source File.
7. I then added the following code to the Source File

SUB PNTHELLO()
CLASS HelloWorld
DECLARE PrintHelloWorld( )
SUB HelloWorld::PrintHelloWorld( )
PRINT "Hello World"
END SUB
END Class
HelloWorld.PrintHelloWorld( )
END SUB

But, just learning Basic, I am not sure if I have correctly created a Class. And I have no idea how to make the output file a DLL.

Thanks in advance for your help. May you have a blessed day.

Michael D Fitzpatrick

acelli

Mike,

Change your target under executable options.

-Alan

EvangelMike

February 27, 2009, 11:48:35 AM #2 Last Edit: February 27, 2009, 12:13:41 PM by EvangelMike
Hi Alan:
   I could not find "executable options" on any of the menus of Emergence Basic. I clicked on all menus and only found Editor Options and Project Options. Hopefully you can direct me to the right menu. At what step of my above seven steps should I change my target? Thanks in advance for your help. May you have a blessed day.

Sincerely,

Michael

PS: I think I also need to add the statement export PrintHelloWorld just after SUB PNTHELLO() but I am not sure.

SUB PNTHELLO()
export PrintHelloWorld
CLASS HelloWorld
DECLARE PrintHelloWorld( )
SUB HelloWorld::PrintHelloWorld( )
PRINT "Hello World"
END SUB
END Class

HelloWorld.PrintHelloWorld( )
END SUB

When I clicked on Build Rebuild All I got the following errors:

Compiling...
HWLD.eba
File: C:\Documents and Settings\All Users\Documents\Emergence BASIC\projects\EBWin1\HWLD.eba (5) local subroutines not supported
File: C:\Documents and Settings\All Users\Documents\Emergence BASIC\projects\EBWin1\HWLD.eba (10) undefined variable - HelloWorld
File: C:\Documents and Settings\All Users\Documents\Emergence BASIC\projects\EBWin1\HWLD.eba (10) unknown class
File: C:\Documents and Settings\All Users\Documents\Emergence BASIC\projects\EBWin1\HWLD.eba (11) RETURN outside of subroutine - END SUB
Error(s) in compiling "C:\Documents and Settings\All Users\Documents\Emergence BASIC\projects\EBWin1\HWLD.eba"
Build Failed

I also tried the following: (trying to pattern after "Writing DLL's" in the User Guide)

SUB SimplDLL()

export myfunction
export myfunction2
export INTRAND

INT inn
inn = 0

SUB myfunction(inn as INT),INT
RETURN inn * 100
ENDSUB

SUB myfunction2(),INT
RETURN 0
ENDSUB

SUB INTRAND(min as INT,max as INT),INT
RETURN RAND(min,max)
ENDSUB

END

and got the error message:

Compiling...
SimplDLL.eba
File: C:\Documents and Settings\All Users\Documents\Emergence BASIC\projects\EBWin1\SimplDLL.eba (10) local subroutines not supported - INT
Error(s) in compiling "C:\Documents and Settings\All Users\Documents\Emergence BASIC\projects\EBWin1\SimplDLL.eba"
Build Failed

How would I go about fixing  local subroutines not supported - INT? Thanks!

MDF


MDF

Ionic Wind Support Team

Mike,
You are trying to progress faster than you should with a new language, you need to walk before you can run ;).  Classes have nothing to do with creating a DLL, and your statements are completely out of order.

Read the users guide please, section "General Programming"->"Writing DLL's"  which contains the most basic DLL code you can create:


export myfunction
export myfunction2
export INTRAND

SUB myfunction(in as INT),INT
RETURN in * 100
ENDSUB

SUB myfunction2(),INT
RETURN 0
ENDSUB

SUB INTRAND(min as INT,max as INT),INT
RETURN RAND(min,max)
ENDSUB


The executable options dialog comes up when you build a single file, "Build Menu"->"Build Single".  Again users guide "IDE Topics"->"Single File Compiling".  And you chose "Windows DLL" as the target.

You shouldn't even be attempting to use projects yet.  Get used to the language first as all you need is a single source file to play with and the aforementioned single file compile.  It is meant specifically for new users.

Thanks,
Paul.
Ionic Wind Support Team

fasecero

Hi, maybe i can help you a little:

First, yes, you have to change the extension to .dll. This can be done if you already create a project in the menu "Project - Options", in "Project type" choose "Windows Dll" and in "Output File" must say "AFirstDLLProject.dll" not .exe, change it if necesary.

Second, you can't declare a class inside a function. You must to declare the class outside.
Then you can use an "instance"  of the class inside the functions, try this:



/*
Compile as a DLL target
*/

'-----------------------------------------
'exports
export PNTHELLO

' ----------------------------------------
'first declare the class
CLASS HelloWorld
   DECLARE PrintHelloWorld()
END CLASS

' then you can define the methods (functions) of the class
SUB HelloWorld::PrintHelloWorld( )
    PRINT "Hello World from the dll"
ENDSUB

'-----------------------------------------
' the export fuction
SUB PNTHELLO()
'we create an instance
HelloWorld HW
'and we use it
HW.PrintHelloWorld()
ENDSUB



Copex

February 27, 2009, 12:07:47 PM #5 Last Edit: February 27, 2009, 12:09:56 PM by Copex
 ::) ::) seems we all posted at the same time :-) :-)

dll_test.eba should be found in the examples directory, if not open ebasic CTRL+N to open a new source file, copy the code below, then press F8 a window will appear you will see a drop down menu under target, select "windows dll" and theclick create :-)

/*
Code for a very simple DLL
For EBASIC 1.0 or greater
Compile as a DLL target
*/


export MYFUNCTION
export MYFUNCTION2
export INTRAND

SUB MYFUNCTION(in as INT),INT
RETURN in
ENDSUB

SUB MYFUNCTION2(),INT
RETURN 0
ENDSUB

SUB INTRAND(min as INT,max as INT),INT
RETURN RAND(min,max)
ENDSUB
-
I really should learn how to use a spell checker! though im not sure how it will help someone who can not spell?
-
Except where otherwise noted, content Posted By Copex is
licensed under a Creative Commons Attribution 3.0 License

http://creativecommons.org/licenses/by/3.0/

EvangelMike

Greetings! With all of your help I just succeeded in creating a dll. The source code that worked is below. (Please let me know if you see any problems in this code. Thanks!) Now I am going to try to call this dll from Excel.

export HelloWindow

DEF w1 as WINDOW

SUB HelloWindow()
REM define a window variable
' DEF w1 as WINDOW
REM open the window
OPENWINDOW w1,0,0,350,350,@MINBOX|@MAXBOX|@SIZE,NULL,"Simple Window",&main
REM print a message
PRINT w1,"Hello World"
REM when w1 = 0 the window has been closed
WAITUNTIL w1 = 0
ENDSUB

REM every time there is a message for our window
REM the operating system will GOSUB here
SUB main
    IF @MESSAGE = @IDCLOSEWINDOW
        REM closes the window and sets w1 = 0
        CLOSEWINDOW w1
    ENDIF
RETURN
ENDSUB

Sincerely,

Michael Fitzpatrick

EvangelMike

Greetings! I created a new blank Excel workbook, opened the Visual Basic Editor from Excel, inserted a standard code modue, and attempted to set a reference to the dll created by Emergence Basic.  Excel then provided the error message:

"Can't add a reference to the specified file"

Cliking on Help, Help says: "Not all object libraries or type libraries can be accessed by Visual Basic. This error has the following cause and solution: You tried to use the Add References dialog box to add a reference to a type library or object library that can't be used by Visual Basic. Check the documentation for the object represented by the library to see if it's available in some other form that Visual Basic can use."

Has anyone in the forum tried to set a reference to a dll created by Emergence Basic

Admittedly, the book whose instructions I am following wanted me to set a reference to a VB project, not a VB single file. So I tried to do this by creating a new project in Emergence Basic, and inserting a file into it with the following code:

export HelloWindow

DEF w1 as WINDOW

SUB HelloWindow()
REM define a window variable
' DEF w1 as WINDOW
REM open the window
OPENWINDOW w1,0,0,350,350,@MINBOX|@MAXBOX|@SIZE,NULL,"Simple Window",&main
REM print a message
PRINT w1,"Hello World"
REM when w1 = 0 the window has been closed
WAITUNTIL w1 = 0
ENDSUB

REM every time there is a message for our window
REM the operating system will GOSUB here
SUB main
    IF @MESSAGE = @IDCLOSEWINDOW
        REM closes the window and sets w1 = 0
        CLOSEWINDOW w1
    ENDIF
RETURN
ENDSUB

This compiles without error but whether I do a Build Compile, or a Build Rebuild All, when I then click on Build again, the Compile and Execute option is greyed out. Thus no dll is created. What is the best way to proceed? Thanks in advance for your help.

Sincerely,

Michael D Fitzpatrick

Ionic Wind Support Team

Mike,
The DLL's created by Emergence are standard Windows DLL's.  They are not "objects" or have anything to do with VB.  In fact once compiled they are just like any other windows API DLL. 

Creating a project in Emergence has nothing to do with the final output, which in this case is a binary dynamic link library.

I've never tried using a standard DLL in Excel myself, and can only assume it is using VB script.  So your first question would have to be "how do I use a DLL from Excel or VB Script, regardless of what language it is created in?"  I don't know the answer myself, try Google.......

I simply searched Yahoo, and the first result was this:

http://msdn.microsoft.com/en-us/library/bb687915.aspx

Which contains instructions on how to import and call a DLL function from within Excel.

Paul.

Ionic Wind Support Team

EvangelMike

Greetings! I found the web page at <http://msdn.microsoft.com/en-us/library/bb687915.aspx>; very difficult to understand. At any rate, I inserted the following VBA code into a Standard Excel Module:

Option Explicit

' Format of Declare:
' Public Declare Sub HelloWindow Lib "libname" [([arglist])] [As type]

Public Declare Function HelloWindow Lib "C:\Documents and Settings\All Users\Documents\Emergence BASIC\projects\EBWin1\HWWindow.dll" ()
Sub TestCallDLL()

Call HelloWindow

End Sub

Upon execution, a window opened saying "Hello World". But when I closed that window, I got the following error message:

"Run-time error '49':
Bad DLL calling convention."

I clicked on Help which says:

"Bad DLL calling convention (Error 49)

Arguments passed to a dynamic-link library (DLL) or Macintosh code resource routine must exactly match those expected by the routine. Calling conventions deal with number, type, and order of arguments. This error has the following causes and solutions:

Your program is calling a routine in a DLL (in Windows) or a code resource (on the Macintosh) that's being passed the wrong type of arguments.
Make sure all argument types agree with those specified in the declaration of the routine you are calling.

Your program is calling a routine in a DLL (in Windows) or a code resource (on the Macintosh) that's being passed the wrong number of arguments.
Make sure you are passing the same number of arguments indicated in the declaration of the routine you are calling.

Your program is calling a routine in a DLL, but isn't using the StdCall calling convention.
If the DLL routine expects arguments by value, then make sure ByVal is specified for those arguments in the declaration for the routine."

which did not make any sense to me since I am not passing any arguments at all to the dll.

Any help or suggestions will be greatly appreciated.

Sincerely,

Michael D Fitzpatrick

EvangelMike

FINALLY:

I solved the problem by changing the Declare statement to refer to a "Sub" instead of a "Function:

Public Declare Sub HelloWindow Lib "C:\Documents and Settings\All Users\Documents\Emergence BASIC\projects\EBWin1\HWWindow.dll" ()

Now when I execute the VBA code, a window opens saying "Hello World". And when I close that window, there is no error message. We did it together team. Without your help and suggestions, there would have been no solution. Thanks! A blessed weekend to one and all.

Sincerely,

Michael D Fitzpatrick