Registriert seit: 8. Mai 2005
Ort: Sondershausen
4.274 Beiträge
Delphi 6 Personal
|
Re: C nach Delphi übersetzen
11. Jun 2006, 00:55
Zitat von Luckie:
Was hast du denn vor? ...
Eine "schöne" Function zu schreiben die ich meiner Lib hinzufügen kann um sie
in Windowsversionen von 98 ++ zu verwenden.
Unter Verwendung dieses Codes bin ich nun soweit gekommen.
Delphi-Quellcode:
program Project1;
uses
Windows, WinSpool;
type
TPRINTER_INFO_5 = PRINTER_INFO_5;
PPRINTER_INFO_5 = ^TPRINTER_INFO_5;
// function GetDefaultPrinterA(prnName: LPTSTR; var bufSize: DWORD): BOOL; stdcall;
// external 'winspool.drv' name 'GetDefaultPrinterA';
TGetDefaultPrinter = function(prnName: LPTSTR; var bufSize: DWORD): BOOL; stdcall;
var
lzPrinterName: array[0..1024] of Char;
function GetDefaultPrinterName(PrinterName: LPTSTR; BufferSize: DWORD): DWORD;
var
osvi: OSVERSIONINFO;
dwNeeded, dwReturned, lErr, BuffSize: DWORD;
pPII5: PPRINTER_INFO_5;
hWinSpoolDrv: THandle;
GetDefaultPrinter: TGetDefaultPrinter;
begin
GetDefaultPrinter := nil;
lErr := 0;
osvi.dwOSVersionInfoSize := sizeof(OSVERSIONINFO);
GetVersionEx(osvi);
if (osvi.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS) then
begin
if not EnumPrinters(PRINTER_ENUM_DEFAULT, nil, 5, nil, 0, dwNeeded, dwReturned)
then lerr := GetLastError;
pPII5 := PPRINTER_INFO_5(LocalAlloc(LPTR, dwNeeded));
if pPII5 = nil then
begin
lerr := GetLastError;
end else
begin
if not EnumPrinters(PRINTER_ENUM_DEFAULT, nil, 5, PBYTE(pPII5), dwNeeded,
dwNeeded, dwReturned) then
begin
lerr := GetLastError;
end else
begin
if (dwReturned > 0) then
begin
if (DWORD(lstrlen(pPII5.pPrinterName)) > BufferSize - 1) then
lerr := ERROR_INSUFFICIENT_BUFFER
else
begin
lstrcpy(PrinterName, pPII5.pPrinterName);
lerr := ERROR_SUCCESS;
end;
end
else
begin
lstrcpy(PrinterName, '0'); // !?
lerr := ERROR_SUCCESS;
end;
end;
LocalFree(DWORD(pPII5)); // !?
end;
end else
if (osvi.dwPlatformId = VER_PLATFORM_WIN32_NT) then
begin
if (osvi.dwMajorVersion >= 5) then // Windows 2000 or later
begin
BuffSize := BufferSize;
hWinSpoolDrv := LoadLibrary('WinSpool.drv');
if hWinSpoolDrv <> 0 then
@GetDefaultPrinter := GetProcAddress(hWinSpoolDrv, 'GetDefaultPrinterA');
if @GetDefaultPrinter <> nil then
begin
if not GetDefaultPrinter(PrinterName, BuffSize)
then lerr := GetLastError
else lerr := ERROR_SUCCESS;
end else
lerr := ERROR_SUCCESS;
if hWinSpoolDrv <> 0 then FreeLibrary(hWinSpoolDrv);
end
else // Windows NT 4.0 or earlier
begin
if (GetProfileString('windows', 'device', '', PrinterName, BufferSize) =
BufferSize - 1)
then lerr := ERROR_INSUFFICIENT_BUFFER
else lerr := ERROR_SUCCESS;
PrinterName[Pos(',', PrinterName)-1] := #0;
end;
end;
Result := lerr;
end;
BEGIN
GetDefaultPrinterName(lzPrinterName, sizeof(lzPrinterName));
MessageBox(0, lzPrinterName, 'DefaultPrinter:', MB_OK);
END.
Unter win 98 und XP funktioniert das schon mal, mit Win NT kann ich es leider nicht testen...
PS.: "// !?" = unsichere Stellen
|
|
Zitat
|