Question

I am looking for a way to return a url as a string from the current Internet Explorer URL.

This approach uses dde. It very fast and works well except it returns a very long string in two parts both with quotes.

uses
  ddeman;

function GetURL(Service: string): string;
var
  ClDDE: TDDEClientConv;
  temp: PAnsiChar;
begin
  Result := '';
  ClDDE := TDDEClientConv.Create(nil);
  with ClDDE do
  begin
    SetLink(Service, 'WWW_GetWindowInfo');
    temp := RequestData('0xFFFFFFFF');
    Result := StrPas(temp);
    StrDispose(temp);
    CloseLink;
  end;
  ClDDE.Free;
end;

For example this returns: "http://core2.staticworld.net/images/article/2014/01/counterfeit_android_apps1-100227383-medium.jpg","http://core2.staticworld.net/images/article/2014/01/counterfeit_android_apps1-100227383-medium.jpg"

But I am looking for just the first part before the first comma without the quotes: http://core2.staticworld.net/images/article/2014/01/counterfeit_android_apps1-100227383-medium.jpg

Any suggestions for another approach or how to parse the string to produce the result shown without quotes and just the first part of the string?

Was it helpful?

Solution

You can parse the string pretty simply yourself:

// Extract string up to position of the ,
Temp := Copy(Temp, 1, Pos(',', Temp) - 1);
// Remove double-quotes from resulting string
Temp := StringReplace(temp, '"', '', [rfReplaceAll]);

Here's a sample console app that uses the logic above on the sample response you provided, and outputs the final contents to the console:

program Project2;

{$APPTYPE CONSOLE}

uses
  SysUtils;

const
  LinkStr = '"http://core2.staticworld.net/images/article/2014/01/counterfeit_android_apps1-100227383-medium.jpg","http://core2.staticworld.net/images/article/2014/01/counterfeit_android_apps1-100227383-medium.jpg"';

var
  Temp: string;

begin
  Temp := Copy(LinkStr, 1, Pos(',', LinkStr) - 1);
  Temp := StringReplace(Temp, '"', '', [rfReplaceAll]);
  Writeln('After: ' + Temp);
  ReadLn;
end.

Output:

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top