Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Button caption from a Windows dll? How?

Posted on 2004-08-16
8
Medium Priority
?
276 Views
Last Modified: 2010-04-05
Hi all!

How can i read a control text from a Windows dll? For example i'd like to get the caption of a dialog button. Windows\System32\msgina.dll (under XP)

Dialog\1800\

CONTROL "&Change Password...", 1803, BUTTON, BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE | WS_TABSTOP, 7, 98, 84, 14 (with ResourceHacker, in DIALOGEX)

I want to read this button's caption into an Edit.

Thanks!

(Please show me some source code)
0
Comment
Question by:rogerrr
8 Comments
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 11808458
procedure TForm1.Button1Click(Sender: TObject);
var
  buffer: array[0..255] of Char;
  DLL_HANDLE : THandle;
begin
  DLL_HANDLE := LoadLibrary('msgina.dll');
  Loadstring(DLL_HANDLE, 1, @buffer, 255);  //1 = first string
  label1.Caption := StrPas(buffer);
end;
0
 

Author Comment

by:rogerrr
ID: 11810214
Thanks, but this code works only with Stringtable resources...
0
 
LVL 3

Expert Comment

by:Tyrsis
ID: 11814922
I would suggest looking at the source code for the Resource Explorer and the Resource Utility components that are on the following page:

http://www.wilsonc.demon.co.uk/delphi.htm

All source code is included from that web page on how to find and read resources from DLL/EXEs in Delphi.  And it is all free.

I hope this helps put you on the right track.

Tyrsis
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 7

Expert Comment

by:LRHGuy
ID: 11817276
Try this...you'll have to fill in a few items...

const
  Flags=format_message_from_hmodule or format_message_allocate_buffer;
  dllname='netmsg.dll';  //name of DLL to get message from
  msgno=12; //message number
var
  DLL:HModule;
  Msg:pChar;
begin
  dll:=loadlibraryex(dllname,0,load_library_as_datafile);
  if dll<>0 then begin
    if formatmessage(flags,pointer(dll),msgno,0,@msg,0,nil)>0 then begin
      showmessage(msg);
      localfree(cardinal(msg));
    end;
    freelibrary(dll);
  end;
0
 

Author Comment

by:rogerrr
ID: 11822214
Hi all!

It as good as done!:-)

I found a code but this doesn't works if the dialog type is DIALOGEX.

Code:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

  type
  TDialogItem = record
    ControlID: dWord;
    Title: WideString;
  end;


type
  TForm1 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
    FDlgItems: array of TDialogItem;
    procedure ReadDialogItems(Stream: TStream);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

function ReadChar(Stream: TStream; var Ch: WideChar): Boolean;
begin
  Stream.ReadBuffer(Ch, SizeOf(Ch));
  Result := Word(Ch) <> $0000;
end;

function ReadString(Stream: TStream; var StrBuf: WideString): Boolean;
var
  Ch: WideChar;
begin
  Result := True;
  StrBuf := '';
  if ReadChar(Stream, Ch) then begin
    Result := Word(Ch) <> $FFFF;
    if Result then begin
      StrBuf := StrBuf + Ch;
      while (Stream.Position <> Stream.Size) and ReadChar(Stream, Ch) do
        StrBuf := StrBuf + Ch;
    end
    else begin
      SetLength(StrBuf, 1);
      ReadChar(Stream, StrBuf[1]);
    end;
  end;
end;


procedure TForm1.Button1Click(Sender: TObject);
var
  I: Integer;
  msgina: HModule;
  Stream: TStream;
begin
  msgina := LoadLibrary('msgina.dll');
  try
    Stream := TResourceStream.CreateFromID(msgina, 1800, RT_DIALOG);
    try
      ReadDialogItems(Stream);
    finally
      Stream.Free;
    end;
  finally
    FreeLibrary(msgina);
  end;
  for I := 0 to Length(FDlgItems) - 1 do
    if FDlgItems[I].ControlID = 1803 then begin
      label1.caption:=FDlgItems[0].Title;
      Break;
    end;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
Finalize(FDlgItems);
end;

procedure TForm1.ReadDialogItems(Stream: TStream);
var
  DataSize: Word;
  DlgItem: TDlgItemTemplate;
  DlgTemplate: TDlgTemplate;
  I: Integer;
  StrBuf: WideString;
begin
  Stream.ReadBuffer(DlgTemplate, SizeOf(DlgTemplate));
  ReadString(Stream, StrBuf);
  ReadString(Stream, StrBuf);
  ReadString(Stream, StrBuf);
  if (DlgTemplate.style and DS_SETFONT) <> 0 then begin
  Stream.Seek(SizeOf(SmallInt), soFromCurrent);
    ReadString(Stream, StrBuf);
  end;
  Finalize(FDlgItems);
  for I := 0 to DlgTemplate.cdit - 1 do begin
    SetLength(FDlgItems, I + 1);
    Stream.Seek(Stream.Position mod SizeOf(DWORD), soFromCurrent);
    Stream.ReadBuffer(DlgItem, SizeOf(DlgItem));
    FDlgItems[I].ControlID:= DlgItem.id;
   ReadString(Stream, StrBuf);
    ReadString(Stream, FDlgItems[I].Title);
   Stream.ReadBuffer(DataSize, SizeOf(DataSize));
    Stream.Seek(DataSize, soFromCurrent);
  end;
end;

end.



2500 is a simple DIALOG. I changed 1800 to 2500 and 1803 to 2501. Label1.Caption will be -->OK (Under XP)



What's the problem if i read a DIALOGEX type????????
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 13404446
PAQed with points refunded (110)

modulo
Community Support Moderator
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question