Board index » cppbuilder » Delphi GetMem under BCB^

Delphi GetMem under BCB^


2005-03-31 07:46:24 PM
cppbuilder13
Hi!
I have to translate some delphi code to BCB. I've stumbled over the
GetMem() routine and wonder what the BCB equivalent would be.
Thanks for your help.
Gunnar
 
 

Re:Delphi GetMem under BCB^

Gunnar Beushausen wrote:
Quote
I have to translate some delphi code to BCB. I've stumbled over the
GetMem() routine and wonder what the BCB equivalent would be.
Strictly speaking it would be malloc/free but modern C++ developers use
new[]/delete[] instead. It would be worth your while posting a code
snippet so we can mull it over.
--
Andrue Cope [TeamB]
[Bicester, Uk]
info.borland.com/newsgroups/guide.html
 

Re:Delphi GetMem under BCB^

Hi!
Andrue Cope [TeamB] schrieb:
Quote
Strictly speaking it would be malloc/free but modern C++ developers use
new[]/delete[] instead. It would be worth your while posting a code
snippet so we can mull it over.

Strange. Anyways, here is a code snippet. Would be cool if you would
have some hints.
---
procedure SetPrinterBin( Printername : pchar; BinCode : integer );
var
SpaceNeeded : word;
pDBuff : _devicemodeA;
Driver_info_2 : pDriverinfo2;
DevModeSize : integer;
dPtr : Pointer;
begin
OpenPrinter(Printername, hPrinter, nil);
// find out how much space we need for the info stuct
GetPrinter( hPrinter, 2, 0, 0, @SpaceNeeded);
// allocate the necessary
GetMem( Driver_info_2, SpaceNeeded);
// The second GetPrinter fills in all the current settings
GetPrinter( hPrinter, 2, Driver_info_2, SpaceNeeded, @SpaceNeeded);
// If GetPrinter didn't fill in the DEVMODE, try to get it by calling
// Documentproperties() - this is always true in Delphi, so ...
DevModeSize := DocumentProperties(0, hPrinter, Printername,
pDBuff, pDBuff, 0); // Zero returns buffer size. */
// get space
dPtr := allocmem( DevModeSize );
devmode := pDevmode( dPtr );
// this call fills our struct
DocumentProperties(0, hPrinter, Printername,
_devicemodeA( dPtr^), pDBuff, DM_OUT_BUFFER );
// make changes to devmode
devmode^.dmFields := DM_DEFAULTSOURCE;
devmode^.dmDefaultSource := BinCode;
// implement the change ...
try
//SetPrinter( hPrinter, 2, devmode, 0);
except
end;
// make sure all of devmode is copied back
DocumentProperties(0, hPrinter, Printername,
_devicemodeA( dPtr^), _devicemodeA(
dPtr^), DM_IN_BUFFER+DM_OUT_BUFFER );
FreeMem( Driver_info_2, SpaceNeeded);
freemem( dPtr, DevmodeSize);
end;
---
Thanks in advance,
Gunnar
 

{smallsort}

Re:Delphi GetMem under BCB^

"Gunnar Beushausen" <gunnar@localhost>wrote in message
Quote
Strange. Anyways, here is a code snippet. Would be cool if
you would have some hints.
Try this:
void __fastcall SetPrinterBin(LPSTR Printername, short BinCode)
{
HANDLE hPrinter;
LONG DevModeSize;
DEVMODE *DevMode;
if( ::OpenPrinter(Printername, &hPrinter, NULL) )
{
DevModeSize = ::DocumentProperties(NULL, hPrinter, Printername,
NULL, NULL, 0);
if( DevModeSize>0 )
{
DevMode = (DEVMODE*) ::GlobalAlloc(GMEM_FIXED, DevModeSize);
if( DevMode )
{
if( ::DocumentProperties(NULL, hPrinter, Printername,
DevMode, NULL, DM_OUT_BUFFER) == IDOK )
{
DevMode->dmFields = DM_DEFAULTSOURCE;
DevMode->dmDefaultSource = BinCode;
::SetPrinter(hPrinter, 2, (LPBYTE) DevMode, 0);
::DocumentProperties(NULL, hPrinter, Printername,
DevMode, DevMode, DM_IN_BUFFER|DM_OUT_BUFFER);
}
::GlobalFree(DevMode);
}
}
::ClosePrinter(hPrinter);
}
}
Gambit