<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
  <channel>
    <title>'Console in GUI app and DLLs' Thread RSS Feed</title>
    <link>http://www.programmersheaven.com/</link>
    <description>Contains the latest posts from the thread 'Console in GUI app and DLLs' posted on the 'Delphi and Kylix' forum at Programmer's Heaven.</description>
    <language>en</language>
    <copyright>Copyright 2009 Programmers Heaven</copyright>
    <pubDate>Fri, 20 Nov 2009 20:52:31 -0700</pubDate>
    <lastBuildDate>Fri, 20 Nov 2009 20:52:31 -0700</lastBuildDate>
    <generator>Argotic Syndication Framework 2007.3.0.1, http://www.codeplex.com/Argotic</generator>
    <docs>http://www.rssboard.org/rss-specification</docs>
    <ttl>360</ttl>
    <image>
      <url>http://www.programmersheaven.com/images/ph.gif</url>
      <title>Programmers Heaven</title>
      <link>http://www.programmersheaven.com/</link>
      <width>88</width>
      <height>31</height>
    </image>
    <item>
      <title>Console in GUI app and DLLs</title>
      <link>http://www.programmersheaven.com/mb/delphikylix/135096/135096/console-in-gui-app-and-dlls/</link>
      <description>Is it possible to create and use console screen in a GUI app?&lt;br /&gt;
Also can you tell me how can I use DLLs in Delphi.&lt;br /&gt;
&lt;br /&gt;
Thanks.&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/delphikylix/135096/135096/console-in-gui-app-and-dlls/</guid>
      <pubDate>Tue, 27 Aug 2002 00:06:59 -0700</pubDate>
      <category>Delphi and Kylix</category>
    </item>
    <item>
      <title>Re: Console in GUI app and DLLs</title>
      <link>http://www.programmersheaven.com/mb/delphikylix/135096/135181/re-console-in-gui-app-and-dlls/#135181</link>
      <description>: Is it possible to create and use console screen in a GUI app?&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/delphikylix/135096/135181/re-console-in-gui-app-and-dlls/#135181</guid>
      <pubDate>Tue, 27 Aug 2002 05:55:12 -0700</pubDate>
      <category>Delphi and Kylix</category>
    </item>
    <item>
      <title>Re: Console in GUI app and DLLs</title>
      <link>http://www.programmersheaven.com/mb/delphikylix/135096/135244/re-console-in-gui-app-and-dlls/#135244</link>
      <description>I'm new to Delphi so I don't think I could handle it. Anyway you could help me to get started.&lt;br /&gt;
&lt;br /&gt;
I still prefere to use standart Console, so if anybody else know a way to do it please help me.&lt;br /&gt;
&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/delphikylix/135096/135244/re-console-in-gui-app-and-dlls/#135244</guid>
      <pubDate>Tue, 27 Aug 2002 10:16:28 -0700</pubDate>
      <category>Delphi and Kylix</category>
    </item>
    <item>
      <title>Re: Console in GUI app and DLLs</title>
      <link>http://www.programmersheaven.com/mb/delphikylix/135096/135324/re-console-in-gui-app-and-dlls/#135324</link>
      <description>: Is it possible to create and use console screen in a GUI app?&lt;br /&gt;
: Also can you tell me how can I use DLLs in Delphi.&lt;br /&gt;
: &lt;br /&gt;
: Thanks.&lt;br /&gt;
: &lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;Linking to a DLL:&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
function Beep(dwFreq, dwDuration: DWORD): BOOL; stdcall; external 'kernel32.dll';
&lt;/pre&gt;&lt;br /&gt;
This will automatically load the DLL into the memory, and retrieve the address of the function.&lt;br /&gt;
A more complex way to link to a DLL is called dynamic linking. Below is the same function, but then dynamically linked.&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
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&amp;gt;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;
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;Writing DLLs:&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
library MyDLL; // &amp;lt;&amp;lt;&amp;lt; 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.
&lt;/pre&gt;&lt;br /&gt;
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.&lt;br /&gt;
If you search for the word "exports" in the help files, you will find much more information about calling and writing DLLs.&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/delphikylix/135096/135324/re-console-in-gui-app-and-dlls/#135324</guid>
      <pubDate>Tue, 27 Aug 2002 16:33:05 -0700</pubDate>
      <category>Delphi and Kylix</category>
    </item>
    <item>
      <title>My solution</title>
      <link>http://www.programmersheaven.com/mb/delphikylix/135096/135410/my-solution/#135410</link>
      <description>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).&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
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&amp;lt;&amp;gt;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&amp;lt;&amp;gt;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.
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/delphikylix/135096/135410/my-solution/#135410</guid>
      <pubDate>Wed, 28 Aug 2002 03:25:18 -0700</pubDate>
      <category>Delphi and Kylix</category>
    </item>
  </channel>
</rss>