Solved

How to use the SendMessageTimeOut function ?

Posted on 1998-11-25
10
669 Views
Last Modified: 2010-04-04
Hello !
In my code I make modifications in the registry.
After those modifications I have to notify all others applications of those changes by broadcasting the WM_WININICHANGE message.
Under win95 I have to use the SendMessageTimeOut function.
The SDK give this example :
SendMessageTimeOut(HWND_BROADCAST,WM_WININICHANGE,0,0L,(LPARAM)(LPCTSTR)"windows",SMTO_NORMAL,1000,NULL);
But it doesn't work !
Does anybody know how to use this function ?
If you have some examples, it will be great !
Thanks in advance.
0
Comment
Question by:nk51
  • 6
  • 4
10 Comments
 
LVL 20

Expert Comment

by:Madshi
ID: 1347855
Hmmm. I don't think, you can use SendMessage or SendMessageTimeout to send broadcast messages. Don't know why it stands in the SDK.
Please try to use "PostMessage(HWND_BROADCAST,WM_WININICHANGE,...)".

Regards, Madshi.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1347856
P.S: Try

(1) PostMessage(HWND_BROADCAST,WM_SETTINGCHANGE,0,lparam(pointer('Windows')));
or
(2) PostMessage(HWND_BROADCAST,WM_SETTINGCHANGE,0,0);

WM_WININICHANGE is obsolete.
0
 

Author Comment

by:nk51
ID: 1347857
if I use :
PostMessage(HWND_BROADCAST,WM_SETTINGCHANGE,0,lparam(pointer('Windows')));
the compilation return the message :
"incorrect cast"
if I use :
PostMessage(HWND_BROADCAST,WM_SETTINGCHANGE,0,0);
the compilation is OK but it doesn't work :
all others applications aren't notify about the changes I made in the registry.
Have you an explication about the compilation error ?
thanks.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1347858
nk51,

I looked again at the documentation. I'm sorry. The SDK is right. I was wrong...   :-(
Now here comes the working solution:

procedure TForm1.Button1Click(Sender: TObject);
var s1 : string;
    c1 : cardinal;
begin
  s1:='windows';
  SendMessageTimeout(HWND_BROADCAST,WM_SETTINGCHANGE,0,integer(pointer(s1)),SMTO_NORMAL,1000,c1);
end;

Regards, Madshi.

P.S: This works with Delphi4. If you have Delphi3 or lower, perhaps you have to change "c1: cardinal" to "c1: integer".
0
 

Author Comment

by:nk51
ID: 1347859
You're answer seems to be good but the SendMessageTimeout function erase the modification I made in the registry and restore the previous value.
Have you an idea about what's happening ?

My code :

procedure TForm1.Button3Click(Sender: TObject);
var s1 : string;
    c1 : integer;
begin
   //Modification de la registry
   Imprimante := TStringList.Create;
   RegImprimante := TRegistry.Create;
   RegImprimante.RootKey := HKEY_LOCAL_MACHINE;
   if RegImprimante.OpenKey('Config\0001\System\CurrentControlSet\Control\Print\Printers',False)
   then
   begin
      RegImprimante.WriteString('Default','Tertiaire');
      RegImprimante.LazyWrite := False;
      RegImprimante.CloseKey;
   end;
   RegImprimante.free;

   s1:='windows';
   SendMessageTimeout(HWND_BROADCAST,WM_WININICHANGE,0,integer(pointer(s1)),SMTO_NORMAL,1000,c1);

end;
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 20

Accepted Solution

by:
Madshi earned 50 total points
ID: 1347860
Try

WritePrivateProfileString('windows','device','Tertiaire,driver,LPT1:','win.ini');

This seems to be one of the cases where the win.ini settings dominate the registry settings...

Regards, Madshi.
0
 

Author Comment

by:nk51
ID: 1347861
Your answer is excellent.
But to use the WritePrivateProfileString function, I need to know the driver and the port of the printer I want to put as default.
So I use the GetPrivateProfileString function to retrieve those parameters. But I have a problem of Access violation because of the PChar I use. Have you an idea about this problem ?

Thanks.

My code :

function TForm1.PrinterParams : string;
var
   buffer : pChar;
begin

    GetPrivateProfileString('devices','Tertiaire','',buffer,sizeof(buffer),'win.ini');
    result := buffer;

end;
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1347862
Your "buffer" is only a pointer to a non-defined memory address. So what happens is: Windows writes 4 bytes (sizeOf(pointer)=4) to a non-defined memory address.
You can either allocate (and then of course free) a pchar using GetMem/FreeMem.
Or you can use "my" method:

begin
  SetLength(result,MAX_PATH);
  GetPrivateProfileString('devices','Tertiaire','',pchar(result),MAX_PATH,'win.ini');
  result:=string(pchar(result));  // This line shortens the string by looking at the #0 char...
end;

Regards, Madshi.
0
 

Author Comment

by:nk51
ID: 1347863
Thank, you're great Madshi !
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1347864
No problem...  :-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

867 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now