Call C function from Clipper .PRG

Call C function from Clipper .PRG

Postby paulrhanson » Thu Oct 23, 2008 11:05 am

I have created a function in C that I want to call from a PRG clipper code.

Clipper Code:
Code: Select all  Expand view
MsgInfo( "In Clipper Prog" )
::oRect := CRect():New( GetWorkRect(), 'Options',,, .t. ) // SPI_GETWORKAREA


GetWorkRect() is the function that I have created in C:
Code: Select all  Expand view
#include <extend.api>
#include <ClipApi.h>
// #include <WinSock.h>
#include <fwmsgs.h>

typedef struct _RECT {
    LONG left;
    LONG top;
    LONG right;
    LONG bottom;
} RECT;

BOOL
SystemParametersInfoA(
    UINT uiAction,
    UINT uiParam,
    PVOID pvParam,
    UINT fWinIni);


/*
* GetWorkRect()
*
* Wrapper to SystemParametersInfo(SPI_GETWORKAREA,...
* Retrieves the size of the work area on the primary display monitor.
* The work area is the portion of the screen not obscured by the system taskbar
* or by application desktop toolbars. The pvParam parameter must point to a RECT
* structure that receives the coordinates of the work area, expressed in virtual
* screen coordinates.
*/

CLIPPER GETWORKREC()     // -->  { nTop, nLeft, nBottom, nRight }
{
   RECT rct;
   rct.top    = 0;
   rct.left   = 0;
   rct.bottom = 0;
   rct.right  = 0;

   #define SPI_GETWORKAREA            48
   SystemParametersInfoA(SPI_GETWORKAREA,0,&rct,0);

   hb_reta( 4 );
   _storni( rct.top,    (WORD)-1, 1 );
   _storni( rct.left,   (WORD)-1, 2 );
   _storni( rct.bottom, (WORD)-1, 3 );
   _storni( rct.right,  (WORD)-1, 4 );
}


When I link using vc98 results are:
Code: Select all  Expand view
c:\dev\pcr5>link @KEYLESS\build\pcrHBH.lnk /nologo /subsystem:windows /force:multiple /NODEFAULTLIB:libcmt /LIBPATH:C:\HarbourM\lib  /OUT:C:\DEV\PCR5\KEYLESS\EXE\HB\PCREGW.EXE
hbrtl.lib(tgetint.obj) : warning LNK4006: _HB_FUN_GETNEW already defined in FiveHM.lib(TCLIPGET.obj); second definition ignored
hbw32.lib(win_dll.obj) : warning LNK4006: _HB_FUN_CALLDLL already defined in FiveHCM.lib(CALLDLL.obj); second definition ignored
CPCREG.OBJ : error LNK2001: unresolved external symbol _HB_FUN_GETWORKRECT
COPYDATA1.obj : error LNK2001: unresolved external symbol "int __cdecl SystemParametersInfoA(unsigned int,unsigned int,void *,unsigned int)" (?SystemParametersInfoA@@YAHIIPAXI@Z)
C:\DEV\PCR5\KEYLESS\EXE\HB\PCREGW.EXE : fatal error LNK1120: 2 unresolved externals


Question 1:
How do I resolve the unresolved external? I do include the GetWorkRect OBJ file in the link.

Question 2:
How do I resolve the problem with SystemParametersInfoA?

Just to be sure, I did remove the reference to SystemParametersInfoA and complied the OBJ again. When I link with that OBJ, I only have the first unresolved external _HB_FUN_GETWORKRECT.

Question 3:
I'd like to get rid of the warnings, but for now, I'm still able to run the resulting EXE if I delete the code that creates the unresolved Externals up to the point where I call the MsgInfo() function.

Thanks,
Paul
-Paul
paulrhanson
 
Posts: 13
Joined: Fri Sep 12, 2008 2:40 am

Re: Call C function from Clipper .PRG

Postby mmercado » Thu Oct 23, 2008 3:03 pm

paulrhanson wrote:Question 2: How do I resolve the problem with SystemParametersInfoA?
Hi Paul:

Here you are a working sample (compiled with xHarbour and borland):
Code: Select all  Expand view
#include "FiveWin.ch"

function Main()

   local oDlg, ;
         aRect := GetWorkRect()

   DEFINE DIALOG oDlg From 00,00 to 400,500 TITLE "Work Rect" PIXEL OF oDlg COLORS CLR_BLACK,CLR_WHITE

   @  10, 10 SAY "1=" + cValToChar( aRect[ 1 ] ) OF oDlg SIZE 50, 10 PIXEL
   @  20, 10 SAY "2=" + cValToChar( aRect[ 2 ] ) OF oDlg SIZE 50, 10 PIXEL
   @  30, 10 SAY "3=" + cValToChar( aRect[ 3 ] ) OF oDlg SIZE 50, 10 PIXEL
   @  40, 10 SAY "4=" + cValToChar( aRect[ 4 ] ) OF oDlg SIZE 50, 10 PIXEL

   ACTIVATE DIALOG oDlg CENTERED

Return nil

#pragma BEGINDUMP
#include <Windows.h>
#include <ClipApi.h>
#include <WinUser.h>

BOOL WINAPI SystemParametersInfoA( UINT uiAction, UINT uiParam, PVOID pvParam, UINT fWinIni) ;

/*
* GetWorkRect()
*
* Wrapper to SystemParametersInfo(SPI_GETWORKAREA,...
* Retrieves the size of the work area on the primary display monitor.
* The work area is the portion of the screen not obscured by the system taskbar
* or by application desktop toolbars. The pvParam parameter must point to a RECT
* structure that receives the coordinates of the work area, expressed in virtual
* screen coordinates.
*/

HB_FUNC( GETWORKRECT )     // -->  { nTop, nLeft, nBottom, nRight }
{
   RECT rct;
   rct.top    = 0;
   rct.left   = 0;
   rct.bottom = 0;
   rct.right  = 0;

   SystemParametersInfoA( SPI_GETWORKAREA, 0, &rct, 0 ) ;

   hb_reta( 4 );
   _storni( rct.top,    -1, 1 );
   _storni( rct.left,   -1, 2 );
   _storni( rct.bottom, -1, 3 );
   _storni( rct.right,  -1, 4 );
}
#pragma ENDDUMP


Regards.

Manuel Mercado
User avatar
mmercado
 
Posts: 782
Joined: Wed Dec 19, 2007 7:50 am
Location: Salamanca, Gto., México

Postby paulrhanson » Thu Oct 23, 2008 11:08 pm

Dear Mr Mercado,

Thanks! I'm rather new at this and have aquired a rather complex set of source code that I'm trying to convert from regular FW to FWH. So I'm learning all at once.

Could you please provide for me the method of compile commands and link commands. Do you use the standard Fivetech buildh.bat to compile and link?

Thanks again,
Paul
-Paul
paulrhanson
 
Posts: 13
Joined: Fri Sep 12, 2008 2:40 am

Postby mmercado » Fri Oct 24, 2008 12:05 am

paulrhanson wrote:Could you please provide for me the method of compile commands and link commands. Do you use the standard Fivetech buildh.bat to compile and link?
Building the posted sample I used the standard BuildX.bat from \fwh\samples. I haven't tried with Harbour (BuildH.bat)

Regards.

Manuel Mercado
User avatar
mmercado
 
Posts: 782
Joined: Wed Dec 19, 2007 7:50 am
Location: Salamanca, Gto., México


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 94 guests