Convert hb_parc() in LPTSTR

Convert hb_parc() in LPTSTR

Postby Maurizio » Mon Oct 20, 2008 4:52 pm

Hello ,
I have to pass a string (Ex: "Hello Word") to a C function .

This is the C function
HB_FUNC( PRINTLINE )
}
LPTSTR lpStr;
lpStr=_T("Hello World");
}

Is it possible convert hb_parc(1) in the variable lpStr ?
Thank

Maurizio
User avatar
Maurizio
 
Posts: 824
Joined: Mon Oct 10, 2005 1:29 pm

Postby Antonio Linares » Mon Oct 20, 2008 5:22 pm

Maurizio,

If it is a string to be used from the Pocket PC, keep in mind that the Pocket PC uses unicode, so you you have to convert it from "ansi" to "wide":
Code: Select all  Expand view
LPWSTR AnsiToWide( LPSTR );

HB_FUNC( PRINTLINE )
{
   LPWSTR pW = AnsiToWide( hb_parc( 1 ) );   

   ... use pW ...

   hb_xfree( pW ); // free the allocated memory
}
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42080
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Postby Maurizio » Tue Oct 21, 2008 7:10 am

Antonio ,

Yes I use for FWPPC

This is a sample , PRINT_1 works PRINT_2 no

Regards Maurizio




Code: Select all  Expand view
#include "FWCE.ch"
function Main()
   Print_1()
   Print_2("Test")
return nil

#pragma BEGINDUMP

#include <hbapi.h>
#include <windows.h>
#include <commdlg.h>   
#include "PrintDC.h"   

HB_FUNC( PRINT_1 )
{
   HDC hPrDC2;
   hPrDC2=PDC_GetPrinterDC() ;
   LPTSTR pW ;
   pW =_T("Hello World");
   PDC_ExtTextOut(hPrDC2,100,100, 0, NULL, pW, _tcslen(pW), NULL);
}   

LPWSTR AnsiToWide( LPSTR );
   
HB_FUNC( PRINT_2 )
{
   HDC hPrDC2;
   hPrDC2=PDC_GetPrinterDC() ;
   
   LPWSTR pW = AnsiToWide( hb_parc( 1 ) );   
   PDC_ExtTextOut(hPrDC2,100,100, 0, NULL, pW, _tcslen(pW), NULL);
   hb_xfree( pW ); // free the allocated memory
}   
User avatar
Maurizio
 
Posts: 824
Joined: Mon Oct 10, 2005 1:29 pm

Postby Antonio Linares » Tue Oct 21, 2008 8:18 am

Maurizio,

You are not releasing the printer DC. If you test it this way, probably PRINT_1 will not work:
Code: Select all  Expand view
#include "FWCE.ch"
function Main()
   Print_2("Test")
   Print_1()
return nil

Look for a PDC_ReleasePrinterDC( hPrDC2 ) or similar.

Everytime a DC is created, then it has to be released, when you are finished with it.
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42080
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Postby Maurizio » Tue Oct 21, 2008 10:13 am

Antonio ,
The problem was in the conversion in TCAHR now I found the solution :

LPSTR pW ;
pW = hb_parc( 1 ) ;
int lenwName ;
lenwName = sizeof(pW) ;
TCHAR wName[sizeof(pW)];
MultiByteToWideChar( CP_ACP, 0 ,pW , -1 , wName, sizeof( wName ) / sizeof( wName[ 0 ] ) );
PDC_ExtTextOut(hPrDC,200,200, 0, NULL, wName, _tcslen(wName) , NULL);

Antonio, it is correct ?

About the DC :
There are not a function to release the DC but a function that close the process at the end .
So , It is better declare hPrDC static at the start or call every line ( every function)
hPrDC=PDC_GetPrinterDC()
what do you think .
User avatar
Maurizio
 
Posts: 824
Joined: Mon Oct 10, 2005 1:29 pm

Postby Antonio Linares » Tue Oct 21, 2008 11:57 am

Maurizio,

I would do it this way:
Code: Select all  Expand view
LPSTR pW = hb_parc( 1 ) ;
int len = hb_parclen( 1 );
TCHAR wName[ len ];
MultiByteToWideChar( CP_ACP, 0 ,pW , -1 , wName, len * 2 );
PDC_ExtTextOut(hPrDC,200,200, 0, NULL, wName, _tcslen(wName) , NULL);

is it working your way ?

Regarding the hDC, yes better reuse the first that you create. Use a static variable.
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42080
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Postby Maurizio » Tue Oct 21, 2008 12:24 pm

Antonio ,

Yes my way works . If I try your way I have this error :
stampa.prg(148) : error C2057: expected constant expression
stampa.prg(148) : error C2466: cannot allocate an array of constant size 0
stampa.prg(148) : error C2133: 'wName' : unknown size

Where :
147 - LPSTR pW = hb_parc( 1 ) ;
148 - int len = hb_parclen( 1 );
149 - TCHAR wName[ len ];
150 - MultiByteToWideChar( CP_ACP, 0 ,pW , -1 , wName, len * 2 );

Regards MAurizio
User avatar
Maurizio
 
Posts: 824
Joined: Mon Oct 10, 2005 1:29 pm

Postby Maurizio » Mon Oct 27, 2008 10:24 am

Antonio ,

I have still problem with

LPSTR pW = hb_parc( 1 ) ;
int len = hb_parclen( 1 );
TCHAR wName[ len ];

I have the error C2057 because [ ] requires a constant expression
Do you know is there another solution to fix the len of
TCHAR wName[ len ]

Thank MAurizio
User avatar
Maurizio
 
Posts: 824
Joined: Mon Oct 10, 2005 1:29 pm

Postby Antonio Linares » Mon Oct 27, 2008 11:20 am

Maurizio,

You can dynamically allocate it:
Code: Select all  Expand view
LPSTR pW = hb_parc( 1 ) ;
int len = hb_parclen( 1 );
TCHAR * wName = ( TCHAR * ) hb_xgrab( len * sizeof( TCHAR ) );

...

hb_xfree( wName );
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42080
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Postby Maurizio » Mon Oct 27, 2008 11:49 am

Antonio :( ,
with your solution the len of wname is always 3 characters .

Maurizio
User avatar
Maurizio
 
Posts: 824
Joined: Mon Oct 10, 2005 1:29 pm

Postby Antonio Linares » Mon Oct 27, 2008 2:31 pm

Maurizio,

Are you using hb_retclen() to return the string from C ?

Keep in mind that the string may have embedded zeroes, so if you use hb_retc() then those zeroes will trim the real string length, thats why you can get 3 bytes only.
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42080
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Postby Maurizio » Mon Oct 27, 2008 4:42 pm

Antonio ,
sorry but I dont understand .

I use this your sample , and hb_parc(1) = "Print Test"
I use wName in the C function .

Regards Maurizio
User avatar
Maurizio
 
Posts: 824
Joined: Mon Oct 10, 2005 1:29 pm

Postby Antonio Linares » Mon Oct 27, 2008 5:07 pm

Maurizio,

Please show me your final C source code for your function, thanks
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42080
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Postby Maurizio » Mon Oct 27, 2008 5:34 pm

Antonio

The function is PRINTLINE .
The printer print only 2 characters .


Thank Maurizio
Code: Select all  Expand view
// FiveWin for Pocket PC - Testing comboboxes

#include "FWCE.ch"
#define PD_SELECTPORTRAIT                 0x00040000
#define PD_SELECTLANDSCAPE                0x00080000
//----------------------------------------------------------------------------//
function Main()

   local oWnd, cValue := "One"
   Local oFont
   Local cTitle := "Stampa"
   
   DEFINE WINDOW oWnd TITLE cTitle
   
   
   DEFINE FONT oFont NAME "Tahoma" SIZE 80, 80
   
   @ 1, 2 BUTTON "Test" SIZE 80, 30 ACTION PrintCE()
   
   ACTIVATE WINDOW oWnd
   
return nil
//----------------------------------------------------------------------------//
function PrintCE()

OPEN_CE()

PrintLine("Print this line")

CLOSE_CE()

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

#pragma BEGINDUMP


#include <hbapi.h>
#include <windows.h>
#include <aygshell.h>
#include <commdlg.h>   
#include "PrintDC.h"   

static far HDC hPrDC;
//==============================================================================================

HB_FUNC( PRINTLINE )
{
   LPSTR pW = hb_parc( 1 ) ;
   int len = hb_parclen( 1 );

   TCHAR * wName = ( TCHAR * ) hb_xgrab( len * sizeof( TCHAR ) );
   
   MultiByteToWideChar( CP_ACP, 0 ,pW , -1 , wName, sizeof( wName ) / sizeof( wName[ 0 ] ) );
   PDC_ExtTextOut(hPrDC,100,100, 0, NULL, wName, _tcslen(wName) , NULL);
   
   hb_xfree( wName );
}

//==============================================================================================

HB_FUNC( OPEN_CE )
{
   //Initialize PrintDC
   HWND hWnd;
   hWnd = FindWindow (NULL,L"Stampa");
   // MessageBox( 0, wName, L"TRUE", 0 );
   PDC_Init(_T("Key"));  // 
   PRINTDLG pdlg;
   memset(&pdlg, 0, sizeof(PRINTDLG));
   pdlg.cbStruct = sizeof(PRINTDLG);
   pdlg.hwndOwner= hWnd;
   pdlg.dwFlags = PD_SELECTPORTRAIT | PD_INTHOUSANDTHSOFINCHES;
   if (!PDC_PrintDlg(&pdlg)) {  //Show "Select Printer" dialog
      PDC_KillDoc(NULL);  //user may have pressed cancel - free PrintDC resources and clear error condition
      return;
   }
   //Get the printer DC
   hPrDC=pdlg.hdc; //Can also use PDC_GetPrinterDC()
   if (hPrDC==NULL) return;
   //Get printer resolution
   int DPI;
   DPI=PDC_GetDeviceCaps(hPrDC,LOGPIXELSX);
   //Get our margins - stored as thousandths of an inch.since used PD_INTHOUSANDTHSOFINCHES
   // Convert margins to printer units (DPI) and subract physical offsets
   int leftmargin,topmargin;
   leftmargin=(pdlg.rcMargin.left*DPI)/1000 - PDC_GetDeviceCaps(hPrDC,PHYSICALOFFSETX);
   topmargin=(pdlg.rcMargin.top*DPI)/1000 - PDC_GetDeviceCaps(hPrDC,PHYSICALOFFSETY);
   //Lets get started printing
   DOCINFO di;
   PDC_StartDoc(hPrDC,&di);
   PDC_StartPage(hPrDC);
   //Print our text using default font
}


//==============================================================================================
HB_FUNC( CLOSE_CE )
{
   PDC_EndPage(hPrDC);
   PDC_EndDoc(hPrDC);
   if (hPrDC) PDC_DeleteDC((HDC) hPrDC);
   PDC_UnInit();
}

#pragma ENDDUMP
User avatar
Maurizio
 
Posts: 824
Joined: Mon Oct 10, 2005 1:29 pm

Postby Antonio Linares » Mon Oct 27, 2008 5:57 pm

Maurizio,

Please try this:

TCHAR * wName = ( TCHAR * ) hb_xgrab( len * sizeof( TCHAR ) );

MultiByteToWideChar( CP_ACP, 0 ,pW , -1 , wName, sizeof( wName ) / sizeof( wName[ 0 ] ) );
PDC_ExtTextOut(hPrDC,100,100, 0, NULL, wName, len * sizeof( TCHAR ) , NULL);
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42080
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Next

Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 89 guests