Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Console in GUI app and DLLs

PrzemekG_PrzemekG_ Member Posts: 595
Is it possible to create and use console screen in a GUI app?
Also can you tell me how can I use DLLs in Delphi.

Thanks.

Comments

  • ManningManning Member Posts: 1,621
    : Is it possible to create and use console screen in a GUI app?

    You can create a component that acts like a console screen. There is one nice one available at www.torry.net written by Danny Thorpe (a guy who works on Delphi over at Borland). I personally didnt like any of the ones available so wrote my own...if you are interested in writing your own as well I can give you more information on where to begin.
  • PrzemekG_PrzemekG_ Member Posts: 595
    I'm new to Delphi so I don't think I could handle it. Anyway you could help me to get started.

    I still prefere to use standart Console, so if anybody else know a way to do it please help me.

  • zibadianzibadian Member Posts: 6,349
    : Is it possible to create and use console screen in a GUI app?
    : Also can you tell me how can I use DLLs in Delphi.
    :
    : Thanks.
    :

    [b]Linking to a DLL:[/b]

    If you want to import a routine from a DLL, the easiest way is a static link. Here is an example of a static link from the Windows unit:
    [code]
    function Beep(dwFreq, dwDuration: DWORD): BOOL; stdcall; external 'kernel32.dll';
    [/code]
    This will automatically load the DLL into the memory, and retrieve the address of the function.
    A more complex way to link to a DLL is called dynamic linking. Below is the same function, but then dynamically linked.
    [code]
    type
    TBeep=function(dwFreq, dwDuration: DWORD): BOOL; stdcall;
    var
    BeepFunc: TBeep = nil;
    KernelDLL: THandle;
    begin
    KernelDLL := LoadLibrary('kernel32.dll'); // load the DLL into memory
    if KernelDLL>HINSTANCE_ERROR then begin
    BeepFunc := GetProcAddress(FHandle,'Beep'); // Retrieve the procedure address
    if BeepFunc=nil then
    ShowMessage('Error finding beep in kernel32.dll')
    else
    Beep(44000, 1000); // Play a tone for 1 second
    FreeLibrary(KernelDLL); // Remove the DLL from memory
    end else
    ShowMessage('Error loading kernel32.dll');
    end;
    [/code]

    [b]Writing DLLs:[/b]

    Below is a very simple example of a DLL project. As you can see this DLL isn't very useful, but it shows several features of a DLL.
    [code]
    library MyDLL; // <<< Note the word "library" instead of "program"

    uses
    SysUtils;

    function MyIntToStr(i: integer): string;
    begin
    Result := IntToStr(i);
    end;

    function MyStrToInt(s: string): integer;
    begin
    Result := StrToInt(s);
    end;

    exports
    MyStrToInt,
    MyIntToStr; // list of exported procedures

    begin
    // No program code
    end.
    [/code]
    The procedures and functions are written as if they are part of a program. The names of the procedures to be exported are listed in the exports clause. These procedures can be linked as described above.
    If you search for the word "exports" in the help files, you will find much more information about calling and writing DLLs.
  • PrzemekG_PrzemekG_ Member Posts: 595
    I've created that class and it's working. I had problems with ReadConsole so I didn't include it. Since I'm a beginner in Delphi programming and I only spended few hours on it, I will finish this class later (add Read, colors, GotoXY and more).
    [code]
    unit console;

    interface

    type TConsoleWnd = class
    private
    isCaption:string; {Caption}
    ibConsole:boolean; {the console already created ?}
    iiHIn,iiHOut,iiHErr:integer; {default i/o and error handles}

    procedure SetCaption(Value:string);
    public
    function CreateCon():boolean;
    function DestroyCon():boolean;
    function Write(str:string):boolean;
    function WriteLn(str:string):boolean;
    function Read(var buff:string):boolean;
    property Caption:string read isCaption write SetCaption;
    end;

    implementation
    uses SysUtils;

    function AllocConsole():integer; stdcall; external 'kernel32.dll';
    function FreeConsole():integer; stdcall; external 'kernel32.dll';
    function GetStdHandle(nStdHandle:integer):integer; stdcall; external 'kernel32.dll';
    const STD_INPUT_HANDLE :integer = -10;
    const STD_OUTPUT_HANDLE :integer = -11;
    const STD_ERROR_HANDLE :integer = -12;


    function WriteConsole(hConsoleOutput:integer; lpBuffer:pchar;
    nNumberOfCharsToWrite:integer;
    var lpNumberOfCharsWritten:integer;
    lpReserved:integer
    ):integer; stdcall; external 'kernel32.dll' name 'WriteConsoleA';

    function SetConsoleTitle (lpConsoleTitle:string):integer; stdcall; external 'kernel32.dll' name 'SetConsoleTitleA'


    function TConsoleWnd.CreateCon():boolean;
    begin
    if ibConsole then
    CreateCon:=false
    else begin
    AllocConsole;
    iiHIn:=GetStdHandle(STD_INPUT_HANDLE);
    iiHOut:=GetStdHandle(STD_OUTPUT_HANDLE);
    iiHErr:=GetStdHandle(STD_ERROR_HANDLE);
    ibConsole:=true;
    CreateCon:=true;
    end;
    end;

    function TConsoleWnd.DestroyCon():boolean;
    begin
    if ibConsole then begin
    FreeConsole;
    ibConsole:=false;
    DestroyCon:=true;
    end else DestroyCon:=false;
    end;

    function TConsoleWnd.Write(str:string):boolean;
    var num,l:integer;
    begin
    if ibConsole then begin
    l:=length(str);
    WriteConsole(iiHOut, pchar(str), l,num,0);
    if l<>num then
    Write:=false
    else
    Write:=true;
    end else
    Write:=false;
    end;

    function TConsoleWnd.WriteLn(str:string):boolean;
    var num,l:integer;
    begin
    if ibConsole then begin
    l:=length(str);
    WriteConsole(iiHOut, pchar(str+#13+#10), l+2,num,0);
    if l<>num then
    WriteLn:=false
    else
    WriteLn:=true;
    end else
    WriteLn:=false;
    end;

    function TConsoleWnd.Read(var buff:string):boolean;
    begin

    end;


    procedure TConsoleWnd.SetCaption(Value:string);
    begin
    if ibConsole then begin
    isCaption:=Value;
    SetConsoleTitle(Value);
    end;
    end;

    end.
    [/code]

Sign In or Register to comment.