Separate versions of a Delphi 7 program have been deployed on various servers.
In order to help troubleshoot reported errors, I'm trying to write a function to identify what server the program is running from.
The following code gets me the local computer name.
sbAll.Panels.Items[1].Text := 'Server: ' + GetEnvironmentVariable('COMPUTERNAME');
Assuming that the absolute path of the program is:
\\Swingline\Programs\Folder\Program.exe
How do I get it to return Server: Swingline regardless of what computer it is run from?
You can probably use Application.ExeName, split it by the slashes and get the second element...
This is the code I ended up using based on #Zdravko's suggestion.
List := TStringList.Create;
try
ExtractStrings(['\'], [], PChar(Application.ExeName), List);
if (List.Text[2] = ':') then // On local computer, Ex. J:\Programs\Foo.exe
sbAll.Panels.Items[1].Text := 'Server: ' + ntComputer.ComputerName
else // In the case of \\Swingline\Programs\Folder\Program.exe
sbAll.Panels.Items[1].Text := 'Server: ' + UpperCase(List[0]);
finally
List.Free;
end;
You can do this without using a string list...
function ExeLocation: String;
var
S: String;
begin
S:= ParamStr(0);
if Copy(S, 2, 2) = ':\' then begin
Result:= GetEnvironmentVariable('COMPUTERNAME');
end else
if Copy(S, 1, 2) = '\\' then begin
Delete(S, 1, 2);
Result:= Copy(S, 1, Pos('\', S)-1);
end;
end;
Keep in mind that if you are referencing the file by the machine's IP address, this will only return the IP address. For example \\192.168.1.123\SomeFolder\SomeFile.exe would just return 192.168.1.123. I looked for other ways but I'm not knowledgeable enough in that department to dig deep enough for the true machine name. It might be possible, but I'm just not seeing it possible.
Related
In my program, the user completes a form and then presses Submit. Then, a textfile or a random extension file is created, in which all the user's information is written. So, whenever the user runs the application form, it will check if the file, which has all the information, exists, then it copies the information and pastes it to the form. However, it is not working for some reason (no syntax errors):
procedure TForm1.FormCreate(Sender: TObject);
var
filedest: string;
f: TextFile;
info: array[1..12] of string;
begin
filedest := ExtractFilePath(ParamStr(0)) + 'User\Identity\IdentityofMyself.txt';
if FileExists(filedest) then
begin
AssignFile(f,filedest);
Reset(f);
ReadLn(info[1], info[2], info[3], info[4], info[5], info[6], info[7],
info[8], info[9], info[10], info[11], info[12]);
Edit1.Text := info[1];
Edit2.Text := info[2];
ComboBox1.Text := info[3];
ComboBox5.Text := info[4];
ComboBox8.Text := info[4];
ComboBox6.Text := info[5];
ComboBox7.Text := info[6];
Edit3.Text := info[7];
Edit4.Text := info[8];
Edit5.Text := info[11];
Edit6.Text := info[12];
ComboBox9.Text := info[9];
ComboBox10.Text := info[10];
CloseFile(f);
end
else
begin
ShowMessage('File not found');
end;
end;
The file exists, but it shows the message File not found. I don't understand.
I took the liberty of formatting the code for you. Do you see the difference (before, after)? Also, if I were you, I would name the controls better. Instead of Edit1, Edit2, Edit3 etc. you could use eFirstName, eLastName, eEmailAddr, etc. Otherwise it will become a PITA to maintain the code, and you will be likely to confuse e.g. ComboBox7 with ComboBox4.
One concrete problem with your code is this line:
readln(info[1], info[2], info[3], info[4], info[5], info[6], info[7],
info[8], info[9], info[10], info[11], info[12]);
You forgot to specify the file f!
Also, before I formatted your code, the final end of the procedure was missing. Maybe your blocks are incorrect in your actual code, so that ShowMessage will be displayed even if the file exists? (Yet another reason to format your code properly...)
If I encountered this problem and wanted to do some quick debugging, I'd insert
ShowMessage(BoolToStr(FileExists(filedest), true));
Exit;
just after the line
filedest := ...
just to see what the returned value of FileExists(filedest) is. (Of course, you could also set a breakpoint and use the debugger.)
If you get false, you probably wonder what in the world filedest actually contains: Well, replace the 'debugging code' above with this one:
ShowMessage(filedest);
Exit;
Then use Windows Explorer (or better yet: the command prompt) to see if the file really is there or not.
I'd like to mention an another possibility to output a debug message (assuming we do not know how to operate real debugger yet):
{ ... }
filedest := ExtractFilePath(ParamStr(0)) + 'User\Identity\IdentityofMyself.txt';
AllocConsole; // create console window (uses Windows module) - required(!)
WriteLn('"' + filedest + '"'); // and output the value to verify
if FileExists(filedest) then
{ ... }
I have a small piece of code that checks if a computer is alive by pinging it. We use to have a room with 40 computer and I wanna check remotely through my program which on is alive.
Therefore I wrote a little ping function using indy
function TMainForm.Ping(const AHost : string) : Boolean;
var
MyIdIcmpClient : TIdIcmpClient;
begin
Result := True;
MyIdIcmpClient := TIdIcmpClient.Create(nil);
MyIdIcmpClient.ReceiveTimeout := 200;
MyIdIcmpClient.Host := AHost;
try
MyIdIcmpClient.Ping;
Application.ProcessMessages;
except
Result := False;
MyIdIcmpClient.Free;
Exit;
end;
if MyIdIcmpClient.ReplyStatus.ReplyStatusType <> rsEcho Then result := False;
MyIdIcmpClient.Free;
end;
So I've developped that at home on my wifi network and everthing just work fine.
When I get back to work I tested and I get an error saying
Socket Errod # 10040 Message too long
At work we have fixed IPs and all the computer and I are in the same subnet.
I tried to disconnect from the fixed IP and connect to the wifi which of course is DHCP and not in the same subnet, and it is just working fine.
I have tried searching the internet for this error and how to solve it but didn't find much info.
Of course I have tried to change the default buffer size to a larger value but it didn't change anything I still get the error on the fixed IP within same subnet.
Moreover, I don't know if this can help finding a solution, but my code treats exceptions, but in that case it takes about 3-4 seconds to raise the error whereas the Timeout is set to 200 milliseconds. And I cannot wait that long over each ping.
By the way I use delphi 2010 and I think it is indy 10. I also have tested on XE2 but same error.
Any idea
----- EDIT -----
This question is answered, now I try to have this running in multithread and I have asked another question for that
Delphi (XE2) Indy (10) Multithread Ping
Set the PacketSize property to 24:
function TMainForm.Ping(const AHost : string) : Boolean;
var
MyIdIcmpClient : TIdIcmpClient;
begin
Result := True;
MyIdIcmpClient := TIdIcmpClient.Create(self);
MyIdIcmpClient.ReceiveTimeout := 200;
MyIdIcmpClient.Host := AHost;
MyIdIcmpClient.PacketSize := 24;
MyIdIcmpClient.Protocol := 1;
MyIdIcmpClient.IPVersion := Id_IPv4;
try
MyIdIcmpClient.Ping;
// Application.ProcessMessages; // There's no need to call this!
except
Result := False;
Exit;
end;
if MyIdIcmpClient.ReplyStatus.ReplyStatusType <> rsEcho Then result := False;
MyIdIcmpClient.Free;
end;
For XE5 and Indy10 this is still a problem, even with different Packet Size.
To answer the more cryptical fix:
ABuffer := MyIdIcmpClient1.Host + StringOfChar(' ', 255);
This is a "magic" fix to get around the fact that there is a bug in the Indy10 component (if I have understood Remy Lebeau right).
My speculation is that this has some connection with the size of the receive buffer. To test my theory I can use any character and don't need to include the host address at all. Only use as many character you need for the receive buffer. I use this small code (C++ Builder XE5) to do a Ping with great success (all other values at their defaults):
AnsiString Proxy = StringOfChar('X',IcmpClient->PacketSize);
IcmpClient->Host = Host_Edit->Text;
IcmpClient->Ping(Proxy);
As you can see I create a string of the same length as the PacketSize property. What you fill it with is insignificant.
Maybe this can be of help to #RemyLebeau when he work on the fix.
use this code
ABuffer := MyIdIcmpClient1.Host + StringOfChar(' ', 255);
MyIdIcmpClient.Ping(ABuffer);
I have been receiving reports from some of my users that, when using idFTP.List() from some servers (MS FTP) then the listing is received as empty (no files) when in reality there are (non-hidden) files on the current directory. May this be a case of a missing parser? The funny think, when I use the program to get the list from MY server (MSFTP on W2003) everything seems OK but on some servers I've been hitting this problem.
Using latest Indy10 on D2010. Any idea?
IdFTPListParseWindowsNT is broken.
The function CheckListing returns false because of a bad parsing:
if sDir = ' <DI' then begin {do not localize}
sDir := Copy(SData, 27, 5);
end else begin
sDir := Copy(SData, 26,28); <---------------BAD PASRSING
Result := TextStartsWith(sDir,' <DI') or IsNumeric(TrimLeft(sDir));
if not Result then begin
Exit;
end;
end;
Commenting this part to make it work like in older versions
if sDir = ' <DI' then begin {do not localize}
sDir := Copy(SData, 27, 5);
end;
{ else begin
sDir := Copy(SData, 26,28); <---------------BAD PASRSING
Result := TextStartsWith(sDir,' <DI') or IsNumeric(TrimLeft(sDir));
if not Result then begin
Exit;
end;
end;}
Showuld solve your problem. Don't know why this change was introduced, though.
This is usually caused by something unexpected in the directory listing which makes the list parser fail. IIS might support both NT-style and Unix-style directory listings, so make sure that you're including both listing parsers in your application and picking between them using IdFTPLaistParse.pas::CheckListing. If that doesn't help it's probably a goofy date or a something in the filename; the best way to debug it is to add code to save the raw directory listing to a file so the end user can send you a copy.
Are you sure you can actually establish the data connection ? The directly listing command is usually the first occasion such a listing is requested and, if you're in the wrong mode, it's usually the point where the failure occurs (i.e. the data channel connection timesout).
Hi I have the need to find the Baud rate and other settings for a serial port, Looking about on the web, it looks like I should be using GetCommConfig, This returns a TCommConfig record with what I assume is the data I need. The problem is the function I wote returns the wrong values.
The code below looks like it is working, but the baud rate is always 1200, which looking in windows device manager (and altering port settings), is wrong.
I have tried calling it like so:
ComPort('com1');
ComPort('COM1');
ComPort('COM1:');
ComPort('COM4');
ComPort('COM9');
the first 4 are valid but return 1200 and the 5th is invalid and returns 0
function ComPort(l_port:String):TCommConfig;
{Gets the comm port settings}
var
ComFile: THandle;
PortName: array[0..80] of Char;
size: cardinal;
CommConfig:TCommConfig;
begin
FillChar(Result, SizeOf(TCommConfig), 0);//blank return value
try
StrPCopy(PortName,l_port);
ComFile := CreateFile(PortName,GENERIC_READ or GENERIC_WRITE,0,nil,OPEN_EXISTING,0{ FILE_ATTRIBUTE_NORMAL},0);
try
if (ComFile <> INVALID_HANDLE_VALUE) then
begin
FillChar(CommConfig, SizeOf(TCommConfig), 0);//blank record
CommConfig.dwSize := sizeof(TCommConfig);//set size
//CommConfig.dcb.DCBlength := SizeOf(_dcb);
size := sizeof(TCommConfig);
if (GetCommConfig(ComFile,CommConfig,size)) then
begin
Result := CommConfig;
end;
end;
finally
CloseHandle(ComFile);
end;
except
Showmessage('Unable to open port ' + l_port);
end;
end;
Stepping through the code, the first 4 always hit the line Result := CommConfig;, so the GetCommConfig is retuning a valid code, so I must be missing something.
I have tryed verious other things, such as setting the length of the dcb record, but all have the same result, as baud of 1200.
Does anyone know where I am going wrong?
The baud rate and other settings for a serial port, are set when the serial port is opened.
I think you are reading default values.
It turns out I was using the wrong function, I should have been using GetDefaultCommConfig and not the GetCommConfig that I was using.
By the look if it, and please correct me if I am wrong, GetDefaultCommConfig returns the settings from windows and GetCommConfig returns the settings of the open connection to the port, writefile opens the port up as it see fit (ignoring the default settings), which is where the 1200 baud rate was coming from.
If this helps anyone in the future, here is the function I came up with.
function ComPort(l_port:String):TCommConfig;
{Gets the comm port settings (use '\\.\' for com 10..99) }
var
size: cardinal;
CommConfig:TCommConfig;
begin
FillChar(Result, SizeOf(TCommConfig), 0);
//strip trailing : as it does not work with it
if (RightStr(l_port,1) = ':') then l_port := LeftStr(l_port,Length(l_port)-1);
try
FillChar(CommConfig, SizeOf(TCommConfig), 0);
CommConfig.dwSize := sizeof(TCommConfig);
size := sizeof(TCommConfig);
if (GetDefaultCommConfig(PChar(l_port),CommConfig,size)) then
begin
Result := CommConfig;
end
//if port is not found add unc path and check again
else if (GetDefaultCommConfig(PChar('\\.\' + l_port),CommConfig,size)) then
begin
Result := CommConfig;
end
except
Showmessage('Unable to open port ' + l_port);
end;
end;
Objective: I would like to be able to list the available COM Ports on a system in Delphi.
Homework:
I have read this SO thread on enumerating the LPT ports of a system using the registry. I have also found that the COM ports are listed in the registry at HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM but found unanswered gesticulation in the same thread that this might not be reliable on different machines and different versions of windows.
I also found articles referencing the use of QueryDosDevice() but upon trying this sample code, I found that it did not appear to list any COM ports at all.
Question: What is the most reliable way (across unknown Windows Versions) to list the COM ports on a Windows Machine?
DEVICEMAP\SERIALCOMM is good for all NT versions. You'll probably need to look under DYN_DATA for Win9x.
Use this method if you need runtime reliability.
Please go through URL which is written in C++
http://www.codeproject.com/KB/system/serial_portsenum_fifo.aspx
and same approach can be implemented in delphi too.. or somebody can convert for you in SO..
This will work for all windows versions since this works from the principle of device manager which is available for all window versions.
This is code for LINUX not for WINDOWS....
function GetSerialPortNames: string;
var
Index: Integer;
Data: string;
TmpPorts: String;
sr : TSearchRec;
begin
try
TmpPorts := '';
if FindFirst('/dev/ttyS*', $FFFFFFFF, sr) = 0 then
begin
repeat
if (sr.Attr and $FFFFFFFF) = Sr.Attr then
begin
data := sr.Name;
index := length(data);
while (index > 1) and (data[index] <> '/') do
index := index - 1;
TmpPorts := TmpPorts + ' ' + copy(data, 1, index + 1);
end;
until FindNext(sr) <> 0;
end;
FindClose(sr);
finally
Result:=TmpPorts;
end;
end;