Page 1 of 2
Convert hb_parc() in LPTSTR
Posted:
Mon Oct 20, 2008 4:52 pm
by Maurizio
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
Posted:
Mon Oct 20, 2008 5:22 pm
by Antonio Linares
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
}
Posted:
Tue Oct 21, 2008 7:10 am
by Maurizio
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
}
Posted:
Tue Oct 21, 2008 8:18 am
by Antonio Linares
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.
Posted:
Tue Oct 21, 2008 10:13 am
by Maurizio
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 .
Posted:
Tue Oct 21, 2008 11:57 am
by Antonio Linares
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.
Posted:
Tue Oct 21, 2008 12:24 pm
by Maurizio
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
Posted:
Mon Oct 27, 2008 10:24 am
by Maurizio
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
Posted:
Mon Oct 27, 2008 11:20 am
by Antonio Linares
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 );
Posted:
Mon Oct 27, 2008 11:49 am
by Maurizio
Antonio
,
with your solution the len of wname is always 3 characters .
Maurizio
Posted:
Mon Oct 27, 2008 2:31 pm
by Antonio Linares
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.
Posted:
Mon Oct 27, 2008 4:42 pm
by Maurizio
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
Posted:
Mon Oct 27, 2008 5:07 pm
by Antonio Linares
Maurizio,
Please show me your final C source code for your function, thanks
Posted:
Mon Oct 27, 2008 5:34 pm
by Maurizio
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
Posted:
Mon Oct 27, 2008 5:57 pm
by Antonio Linares
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);