FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Creating a LIB or DLL
Posts: 125
Joined: Wed Oct 19, 2005 02:28 PM

Re: Creating a LIB or DLL

Posted: Thu Apr 02, 2009 07:50 PM
Antonio,

I compile ok now, however my test2 no longer displays in dll and returns nil

Here is how I call test2 dll from my code
Code (fw): Select all Collapse
************************** Test 2 Hello World
function test2()
   local cr:=chr(13)
   local hItem1 := ItemNew( "The Message is : Hello world!"+cr+"       Test 2" )
   local hItem2 := ItemNew( "This is the Title for the Harbour DLL" )

************************** for return string from dll
   local rtnitem:="Return Value not set yet." 
   
   ?HbDLLstring2( "TEST2", hItem1, hItem2 )

   ItemRelease( hItem1 )
   ItemRelease( hItem2 )

   MsgInfo( rtnitem+cr+"OK! We are back to the EXE." )

return nil






Here is my new maindll.c
Code (fw): Select all Collapse
/*
 * $Id: maindll.c 10658 2009-03-19 18:13:33Z vszakats $
 */

/*
 * Harbour Project source code:
 * Windows self-contained DLL entry point
 *
 * Copyright 1999 Antonio Linares <alinares@fivetech.com>
 * www - <!-- m --><a class="postlink" href="http://www.harbour-project.org">http://www.harbour-project.org</a><!-- m -->
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version, with one exception:
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this software; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307 USA (or visit the web site <!-- m --><a class="postlink" href="http://www.gnu.org/">http://www.gnu.org/</a><!-- m -->).
 *
 * As a special exception, the Harbour Project gives permission for
 * additional uses of the text contained in its release of Harbour.
 *
 * The exception is that, if you link the Harbour libraries with other
 * files to produce an executable, this does not by itself cause the
 * resulting executable to be covered by the GNU General Public License.
 * Your use of that executable is in no way restricted on account of
 * linking the Harbour library code into it.
 *
 * This exception does not however invalidate any other reasons why
 * the executable file might be covered by the GNU General Public License.
 *
 * This exception applies only to the code released by the Harbour
 * Project under the name Harbour.  If you copy code from other
 * Harbour Project or Free Software Foundation releases into a copy of
 * Harbour, as the General Public License permits, the exception does
 * not apply to the code that you add in this way.  To avoid misleading
 * anyone as to the status of such modified files, you must delete
 * this exception notice from them.
 *
 * If you write modifications of your own for Harbour, it is your choice
 * whether to permit this exception to apply to your modifications.
 * If you do not wish that, delete this exception notice.
 *
 */

#define HB_OS_WIN_32_USED

#include "hbvm.h"
#include "hbapiitm.h"

#if defined(HB_OS_WIN_32)

BOOL WINAPI _export DllEntryPoint( HINSTANCE hInstance, DWORD fdwReason, PVOID pvReserved )
{
   HB_TRACE( HB_TR_DEBUG, ("DllEntryPoint(%p, %p, %p)", hInstance, fdwReason,
             pvReserved ) );

   HB_SYMBOL_UNUSED( hInstance );
   HB_SYMBOL_UNUSED( fdwReason );
   HB_SYMBOL_UNUSED( pvReserved );


   switch( fdwReason )
   {
      case DLL_PROCESS_ATTACH:
           hb_vmInit( FALSE );  /* Don't execute first linked symbol */
           break;
      case DLL_PROCESS_DETACH:
           hb_vmQuit();
           break;
   }

 
   return TRUE;
}

LONG PASCAL _export HBDLLENTRY( char * cProcName )
{

   hb_itemDoC( cProcName, 0, 0 );

   return 0;
}

LONG PASCAL _export HBDLLENTRY1( char * cProcName, LONG pItem )
{

   hb_itemDoC( cProcName, 1, ( PHB_ITEM ) pItem, 0 );

   return 0;
}

LONG PASCAL _export HBDLLENTRY2( char * cProcName, LONG pItem1, LONG pItem2 )
{

   hb_itemDoC( cProcName, 2, ( PHB_ITEM ) pItem1, ( PHB_ITEM ) pItem2, 0 );

   return 0;
}
HB_EXPORT char * PASCAL HBDLLSTRING2( char * cProcName, LONG pItem1, LONG pItem2 )
{
   hb_itemDoC( cProcName, 2, ( PHB_ITEM ) pItem1, ( PHB_ITEM ) pItem2, 0 );

   return hb_parc( -1 );
}

#endif



Thanks
Dave Zowasky
Com1 Software, Inc.
http://www.com1software.com
information@com1software.com
330 653-3771
Posts: 44508
Joined: Thu Oct 06, 2005 05:47 PM

Re: Creating a LIB or DLL

Posted: Thu Apr 02, 2009 08:16 PM

Dave,

Have you included this in your EXE PRG ?

DLL FUNCTION HBDLLSTRING2( cProc AS LPSTR, pItem1 AS LONG, pItem2 AS LONG ) AS LPSTR PASCAL LIB "dll2test.dll"

Is Test1() still working ?

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 125
Joined: Wed Oct 19, 2005 02:28 PM

Re: Creating a LIB or DLL

Posted: Thu Apr 02, 2009 09:01 PM

Antonio,

Yes to both questions. My last maindll.c posted is correct my prior one may have had a problem.

I am making sure test1 is good as a control. Let me know if you need me to post any of my code.

Thanks

Dave Zowasky
Com1 Software, Inc.
http://www.com1software.com
information@com1software.com
330 653-3771
Posts: 125
Joined: Wed Oct 19, 2005 02:28 PM

Re: Creating a LIB or DLL

Posted: Thu Apr 02, 2009 09:05 PM
Antonio,

I did and impdef on the dll

Code (fw): Select all Collapse
LIBRARY     DLL2TEST.DLL

EXPORTS
    DllEntryPoint                  @1   ; DllEntryPoint
    HBDLLENTRY                     @2   ; HBDLLENTRY
    HBDLLENTRY1                    @3   ; HBDLLENTRY1
    HBDLLENTRY2                    @4   ; HBDLLENTRY2
    __DbgWndProc                   @6   ; __DbgWndProc
    __WndProc                      @5   ; __WndProc
    ___CPPdebugHook                @7   ; ___CPPdebugHook



No hbdllstring

Here is what i have in the buildhd,bat

%bcdir%\bin\bcc32 -c -I%hdir%\include;%bcdir%\include -L%bcdir%\..\lib maindll.c

Thanks
Dave Zowasky
Com1 Software, Inc.
http://www.com1software.com
information@com1software.com
330 653-3771
Posts: 125
Joined: Wed Oct 19, 2005 02:28 PM

Re: Creating a LIB or DLL

Posted: Thu Apr 02, 2009 10:18 PM
Antonio,

Added _export after PASCAL now appears in def and displays message in dll. :-)

Still no return value.

current code in maindll.c
Code (fw): Select all Collapse
HB_EXPORT char * PASCAL _export HBDLLSTRING2( char * cProcName, LONG pItem1, LONG pItem2 )
{
   MessageBox( 0, "Inside ", "1", 0 );
   hb_itemDoC( cProcName, 2, ( PHB_ITEM ) pItem1, ( PHB_ITEM ) pItem2, 0 );
   MessageBox( 0, hb_parc( -1 ), "2", 0 );


   return hb_parc( -1 );
}


Thanks
Dave Zowasky
Com1 Software, Inc.
http://www.com1software.com
information@com1software.com
330 653-3771
Posts: 44508
Joined: Thu Oct 06, 2005 05:47 PM

Re: Creating a LIB or DLL

Posted: Thu Apr 02, 2009 10:25 PM

Dave,

Is this msg properly showing the text ?

MessageBox( 0, hb_parc( -1 ), "2", 0 );

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 125
Joined: Wed Oct 19, 2005 02:28 PM

Re: Creating a LIB or DLL

Posted: Thu Apr 02, 2009 10:33 PM

Antonio,

No, the box 2 appears empty.

Thanks

Dave Zowasky
Com1 Software, Inc.
http://www.com1software.com
information@com1software.com
330 653-3771
Posts: 44508
Joined: Thu Oct 06, 2005 05:47 PM

Re: Creating a LIB or DLL

Posted: Thu Apr 02, 2009 11:01 PM
Dave,

Is this MsgInfo() showing properly ?
Code (fw): Select all Collapse
function Test2( cMsg1, cMsg2 )
local cr:=chr(13)

   cMsg1:=cMsg1+cr+"This is being added on in the DLL."
   cMsg1:=cMsg1+cr+"     So Far so good."
   cMsg1:=cMsg1+cr+"Time to send this message"
   cMsg1:=cMsg1+cr+"back the TESTDLL.EXE."

   MsgInfo( cMsg1, cMsg2 )

return (cMsg1)
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 125
Joined: Wed Oct 19, 2005 02:28 PM

Re: Creating a LIB or DLL

Posted: Thu Apr 02, 2009 11:08 PM

Antonio,

Yes since I added the _export after PASCAL.

Thanks

Dave Zowasky
Com1 Software, Inc.
http://www.com1software.com
information@com1software.com
330 653-3771
Posts: 44508
Joined: Thu Oct 06, 2005 05:47 PM

Re: Creating a LIB or DLL

Posted: Thu Apr 02, 2009 11:23 PM
Dave,

Please try it this way:
Code (fw): Select all Collapse
HB_EXPORT char * PASCAL _export HBDLLSTRING2( char * cProcName, LONG pItem1, LONG pItem2 )
{
   PHB_ITEM pResult;   

   MessageBox( 0, "Inside ", "1", 0 );
   pResult = hb_itemDoC( cProcName, 2, ( PHB_ITEM ) pItem1, ( PHB_ITEM ) pItem2, 0 );
   MessageBox( 0, hb_itemGetC( pResult ), "2", 0 );


   return hb_itemGetC( pResult );
}
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 125
Joined: Wed Oct 19, 2005 02:28 PM

Re: Creating a LIB or DLL

Posted: Thu Apr 02, 2009 11:29 PM

Antonio,

That works :)

Thanks

Dave Zowasky
Com1 Software, Inc.
http://www.com1software.com
information@com1software.com
330 653-3771
Posts: 44508
Joined: Thu Oct 06, 2005 05:47 PM

Re: Creating a LIB or DLL

Posted: Thu Apr 02, 2009 11:40 PM

very good :-)

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 125
Joined: Wed Oct 19, 2005 02:28 PM

Re: Creating a LIB or DLL

Posted: Fri Apr 03, 2009 03:13 PM
Antonio,

Thank you very much for all your help.

My project is going very well and I very happy with what we have been able to do so far. :-)

I would like to ask your advice on how best to apply the next changes to the program.

I am trying to have the DoSum return the value from the dll to the exe

In the EXE I will have:
function test3 in the testdll.prg
Code (fw): Select all Collapse
************************** Test 3 DoSum
function test3(x,y)
   local cr:=chr(13)
   local hItem1 := ItemNew( x )
   local hItem2 := ItemNew( y )
   local rtnval :=0
  
   rtnval:=HbDLLDoSum2( "DoSum", hItem1, hItem2 )
   
   ItemRelease( hItem1 )
   ItemRelease( hItem2 )

   MsgInfo( str(rtnval)+"=Total"+cr+"OK! We are back to the EXE." )

return nil


Call to DLL
Code (fw): Select all Collapse
DLL FUNCTION HBDLLDoSUM2( cProc AS LPSTR, pItem1 AS LONG, pItem2 AS LONG ) AS LPSTR PASCAL LIB "dll2test.dll"




In the maindll.c
Code (fw): Select all Collapse
HB_EXPORT char * PASCAL _export HBDLLDOSUM2( char * cProcName, LONG pItem1, LONG pItem2 )
{
   PHB_ITEM pResult;  
   pResult = hb_itemDoC( cProcName, 2, ( PHB_ITEM ) pItem1, ( PHB_ITEM ) pItem2, 0 );
   return hb_itemGetC( pResult );
}


In the dll2test.prg:
Code (fw): Select all Collapse
function Test3( x, y )
local z:=X+y
return (z)



Thanks
Dave Zowasky
Com1 Software, Inc.
http://www.com1software.com
information@com1software.com
330 653-3771
Posts: 44508
Joined: Thu Oct 06, 2005 05:47 PM

Re: Creating a LIB or DLL

Posted: Fri Apr 03, 2009 03:35 PM
Dave,

This time you are going to generate a number (LONG). In case you plan to use decimals, then a different code is needed. Please review Harbour\source\vm\itemapi.c functions:
Code (fw): Select all Collapse
DLL FUNCTION HBDLLDoSUM2( cProc AS LPSTR, pItem1 AS LONG, pItem2 AS LONG ) AS LONG PASCAL LIB "dll2test.dll"


In the maindll.c
Code (fw): Select all Collapse
HB_EXPORT LONG PASCAL _export HBDLLDOSUM2( char * cProcName, LONG pItem1, LONG pItem2 )
{
   PHB_ITEM pResult;  
   pResult = hb_itemDoC( cProcName, 2, ( PHB_ITEM ) pItem1, ( PHB_ITEM ) pItem2, 0 );
   return hb_itemGetNL( pResult );
}
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 125
Joined: Wed Oct 19, 2005 02:28 PM

Re: Creating a LIB or DLL

Posted: Fri Apr 03, 2009 10:12 PM
Antonio,

I applied changes cannot get to MessageBox Point 2

I get a GPF send to microsoft crash.

code in maindll.c
Code (fw): Select all Collapse
HB_EXPORT LONG PASCAL _export HBDLLDOSUM2( char * cProcName, LONG pItem1, LONG pItem2 )
{
   PHB_ITEM pResult;  
  MessageBox( 0, "Point 1", "1", 0 );
   pResult = hb_itemDoC( cProcName, 2, ( PHB_ITEM ) pItem1, ( PHB_ITEM ) pItem2, 0 );
  MessageBox( 0, "Point 2", "2", 0 );
   return hb_itemGetNL( pResult );
}



code in exe

Code (fw): Select all Collapse
  ?HbDLLDoSum2( "TEST3",3, 2 )


Code (fw): Select all Collapse
DLL FUNCTION HBDLLDOSUM2( cProc AS LPSTR, pItem1 AS LONG, pItem2 AS LONG ) AS LONG PASCAL LIB "dll2test.dll"


Code in dll
Code (fw): Select all Collapse
  
function Test3( x, y )
local z:=X+y
return (z)


Thanks
Dave Zowasky
Com1 Software, Inc.
http://www.com1software.com
information@com1software.com
330 653-3771