Delphi: get current URL from MSEdge by using DDE (Window 10 22H2)

191 views Asked by At

I want to convert the functions below written in C++ into Delphi. The C++ source code is from https://github.com/aardappel/procrastitracker in the files ddeutil.h and timercallback.h:

DWORD idInst = 0;
HDDEDATA CALLBACK DdeCallback(UINT uType, UINT uFmt, HCONV hconv, HSZ hsz1, HSZ hsz2,
                              HDDEDATA hdata, DWORD dwData1, DWORD dwData2) {
    return 0;
}
bool ddeinit() {
    return DdeInitialize(&idInst, (PFNCALLBACK)DdeCallback, APPCLASS_STANDARD | APPCMD_CLIENTONLY,
                         0) == DMLERR_NO_ERROR;
}
void ddeclean() {
    if (idInst) DdeUninitialize(idInst);
}

void ddereq(char *server, char *topic, char *item, char *buf, int len) {
    buf[0] = 0;
    HSZ hszApp = DdeCreateStringHandleA(idInst, server, 0);
    HSZ hszTopic = DdeCreateStringHandleA(idInst, topic, 0);
    HCONV hConv = DdeConnect(idInst, hszApp, hszTopic, NULL);
    DdeFreeStringHandle(idInst, hszApp);
    DdeFreeStringHandle(idInst, hszTopic);
    if (hConv == NULL) {
        // OutputDebugF("dde error: %x\n", DdeGetLastError(idInst));
        // DMLERR_NO_CONV_ESTABLISHED on chrome, see
        // https://bugs.chromium.org/p/chromium/issues/detail?id=70184
        return;
    }

    HSZ hszItem = DdeCreateStringHandleA(idInst, item, 0);
    HDDEDATA hData =
        DdeClientTransaction(NULL, 0, hConv, hszItem, CF_TEXT, XTYP_REQUEST, 5000, NULL);
    if (hData != NULL) {
        DdeGetData(hData, (unsigned char *)buf, len, 0);
        buf[len - 1] = 0;
        DdeFreeDataHandle(hData);
    } else {
        // OutputDebugF("dde error: %x\n", DdeGetLastError(idInst));
    }

    DdeFreeStringHandle(idInst, hszItem);

    DdeDisconnect(hConv);
}

Using the ddereq() function to get a URL from MSEdge (in C++):

ddereq("msedge", "WWW_GetWindowInfo", "0xFFFFFFFF", url, MAXTMPSTR);

I converted these functions to Delphi:

var idInst : LongInt=0;

function DDECallback(CallType, Fmt: UINT; Conv: HConv; hsz1, hsz2: HSZ;
    Data: HDDEData; Data1, Data2: DWORD): HDDEData; stdcall;
begin
   Result:=0;
end;

function ddeinit:Boolean;
begin
    Result:=DdeInitializeA(idInst, @DDECallback, APPCLASS_STANDARD or APPCMD_CLIENTONLY,0)=DMLERR_NO_ERROR;
end;

procedure ddeclean;
begin
    if idInst<>0 then DdeUninitialize(idInst);
end;

procedure ddereq(server, topic, item : PAnsiChar;var Buff:String);
var
  hszApp,
  hszTopic : THandle;
  hConv    : THandle;
  hszItem  : THandle;
  hData    : HDDEDATA;
begin    
    hszApp := DdeCreateStringHandleA(idInst, server, 0);
    hszTopic := DdeCreateStringHandleA(idInst, topic, 0);
    hConv := DdeConnect(idInst, hszApp, hszTopic, nil);
    DdeFreeStringHandle(idInst, hszApp);
    DdeFreeStringHandle(idInst, hszTopic);
    if hConv = 0 then begin exit; end;

    hszItem := DdeCreateStringHandleA(idInst, item, 0);
    hData :=  DdeClientTransaction(nil, 0, hConv, hszItem, CF_TEXT, XTYP_REQUEST, 5000, nil);
    if hData <> 0 then begin
        DdeGetData(hData,@buff, SizeOf(Buff), 0);
        DdeFreeDataHandle(hData);
    end else ;

    DdeFreeStringHandle(idInst, hszItem);
    DdeDisconnect(hConv);

end;

I used the ddereq() function to get a URL from MSEdge (in Delphi):

ddereq('msedge', 'WWW_GetWindowInfo', '0xFFFFFFFF', url);

but the function always returns a blank URL because DdeConnect() always returns 0 at this line:

hConv := DdeConnect(idInst, hszApp, hszTopic, nil);

Please help me fix these functions in Delphi.

0

There are 0 answers