How to get TWebBrowser error information - delphi

I'm implementing the Exec method of TWebBrowser based on this answer. This method is triggered whenever a script error occurs. Now I need to get error information.
I first get hold of the event object of the TWebBrowser.
var
Doc: IHTMLDocument2;
HTMLWindow: IHTMLWindow2;
Event: IHTMLEventObj;
MethodName: String;
MethodResult: OleVariant;
DispatchId: Integer;
Param: array of OleVariant;
begin
//Avoid non-error calls
if nCmdID != OLECMDID_SHOWSCRIPTERROR then
Exit;
//Get hold of the event object
Doc := MapForm.WebBrowser.Document as IHTMLDocument2;
if not Assigned(Doc) then
Exit;
HTMLWindow := Doc.parentWindow;
if not Assigned(HTMLWindow) then
Exit;
//Get the current event
Event := HTMLWindow.event;
And then I'm trying to get the information I need (as demonstrated in this link) using GetIDsOfNames and Invoke functions of the interface. A working Delphi code for using these methods are in this documentation link.
Here is how I use these functions on the Event object.
MethodName := 'errorMessage';
Result := Event.GetIDsOfNames(GUID_NULL, #MethodName, 1, SysLocale.DefaultLCID, #DispatchId);
Result := Event.Invoke(DispatchId, GUID_NULL, SysLocale.DefaultLCID, DISPATCH_METHOD, Param, #MethodResult, nil, nil);
The GetIDsOfNames fuGetIDsOfNames function executes properly, outputs an acceptable integer to DispatchId and returns S_OK.
But the Invoke function just fails. It returns some negative integer as HRESULT and doesn't output anything to MethodResult.
How can I work around this?

The error values you are trying to access are not object methods, they are properties, so Invoke() is going to fail due to your use of DISPATCH_METHOD. Use DISPATCH_PROPERTYGET instead.
However, OleVariant (and Variant) has built-in support for IDispatch.Invoke(), so you don't need to mess with it manually at all. You can call object methods and read/write object properties normally, and the compiler will produce the necessary IDispatch calls for you.
Try something more like this:
function TWebBrowser.Exec(CmdGroup: PGUID; nCmdID, nCmdexecopt: DWORD;
const vaIn: OleVariant; var vaOut: OleVariant): HRESULT; stdcall;
var
Event: OleVariant;
ErrorLine: Integer;
ErrorChar: Char;
ErrorCode: Integer;
ErrorMsg: String;
ErrorUrl: String;
begin
if (CmdGroup = nil) or not IsEqualGUID(CmdGroup^, CGID_DocHostCommandHandler) then
begin
Result := OLECMDERR_E_UNKNOWNGROUP;
Exit;
end;
if nCmdID <> OLECMDID_SHOWSCRIPTERROR then
begin
Result := OLECMDERR_E_NOTSUPPORTED;
Exit;
end;
Event := (IUnknown(vaIn) as IHTMLDocument2).parentWindow.event;
ErrorLine := Event.errorLine;
ErrorChar := Event.errorCharacter;
ErrorCode := Event.errorCode;
ErrorMsg := Event.errorMessage;
ErrorUrl := Event.errorUrl;
...
if (should continue running scripts) then
begin
vaOut := True;
end else
begin
vaOut := False;
end;
Result := S_OK;
end;

Related

How to invoke a VBscript function from pointer obtained by Getref in a Delphi COM server app

I have a COM server App that and need to link callbacks to specific events which are late bound.
My test VB script is as follows
Sub Main
dim Frm
Set Frm=NewForm("Form1")
Frm.OnActivate = getRef("Frm_OnActivate")
a= Frm.Showmodal
end Sub
sub Frm_OnActivate
MsgBox("Activate")
end Sub
My com server has the onActivate property which is of type OleVariant.
function TALform.Get_OnActivate: OleVariant;
begin
result:=FonActivate;
end;
procedure TALform.Set_OnActivate(Value: OleVariant);
begin
FonActivate:=Value;
Fform.OnActivate:=OnactivateEx
end;
My question is, having got that value, how do I call the VBscript function from the value stored in the Olevariant (which the debugger shows to be of type VarDispatch) ?
Try something like this:
var
Param: TDispParams;
MethodResult: OleVariant;
Result: HRESULT;
begin
Param.rgvarg := nil;
Param.rgdispidNamedArgs := nil;
Param.cArgs := 0;
Param.cNamedArgs := 0;
Result := IDispatch(FonActivate).Invoke(0, GUID_NULL, SysLocale.DefaultLCID, DISPATCH_METHOD, Param, #MethodResult, nil, nil);
end;

How to determine the size of a buffer for a DLL call when the result comes from the DLL

Using both Delphi 10.2 Tokyo and Delphi XE2.
I have a DLL that posts XML data to a site. The DLL is built with Delphi 10 in order to use TLS 1.2, which is not available with Delphi XE2.
The call to the DLL comes from a Delphi XE2 EXE, but I don't believe that is relevant, but I am noting it nonetheless.
The call to post data to a site will often return text data. Sometimes very large amounts of text data. Greater than 150K characters.
My original DLL convention was basically not correct, as I returned the contents of the returned text data as a PChar. In my readings here and elsewhere, that's a big no-no.
That "bad" methodology worked well until I started to get very large amounts of data returned. I tested it, and it failed on anything greater than 132,365 characters.
I restructured my DLL and calling code to pass in a buffer as a PChar to fill in, but I get an error trying to fill the output value!
Secondly, since I never know how big the returned data will be, how to I specify how big a buffer to fill from my calling method?
My DLL code where I get the error:
library TestDLL;
uses
SysUtils,
Classes,
Windows,
Messages,
vcl.Dialogs,
IdSSLOpenSSL, IdHTTP, IdIOHandlerStack, IdURI,
IdCompressorZLib;
{$R *.res}
function PostAdminDataViaDll(body, method, url: PChar; OutData : PChar; OutLen : integer): integer; stdcall
var HTTPReq : TIdHTTP;
var Response: TStringStream;
var SendStream : TStringStream;
var IdSSLIOHandler : TIdSSLIOHandlerSocketOpenSSL;
var Uri : TIdURI;
var s : string;
begin
Result := -1;
try
HTTPReq := TIdHTTP.Create(nil);
IdSSLIOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
IdSSLIOHandler.SSLOptions.Mode := sslmClient;
IdSSLIOHandler.SSLOptions.SSLVersions := [sslvTLSv1_2, sslvTLSv1_1];
if Assigned(HTTPReq) then begin
HTTPReq.Compressor := TIdCompressorZLib.Create(HTTPReq);
HTTPReq.IOHandler := IdSSLIOHandler;
HTTPReq.ReadTimeout := 180000;//set read timeout to 3 minutes
HTTPReq.Request.ContentType := 'text/xml;charset=UTF-8';
HTTPReq.Request.Accept := 'text/xml';
HTTPReq.Request.CustomHeaders.AddValue('SOAPAction', 'http://tempuri.org/Administration/' + method);
HTTPReq.HTTPOptions := [];
end;
SendStream := TStringStream.Create(Body);
Response := TStringStream.Create(EmptyStr);
try
HTTPReq.Request.ContentLength := Length(Body);
Uri := TiDUri.Create(url);
try
HTTPReq.Request.Host := Uri.Host;
finally
Uri.Free;
end;
HTTPReq.Post(url + 'admin.asmx', SendStream,Response);
if Response.Size > 0 then begin
if assigned(OutData) then begin
s := Response.DataString;// Redundant? Probably can just use Response.DataString?
StrPLCopy(OutData, s, OutLen);// <- ACCESS VIOLATION HERE
//StrPLCopy(OutData, s, Response.Size);// <- ACCESS VIOLATION HERE
Result := 0;
end;
end
else begin
Result := -2;
end;
finally
Response.Free;
SendStream.Free;
IdSSLIOHandler.Free;
HTTPReq.Free;
end;
except
on E:Exception do begin
ShowMessage(E.Message);
Result := 1;
end;
end;
end;
exports
PostAdminDataViaDll;
begin
end.
My Calling method code:
function PostAdminData(body, method, url : string): IXMLDOMDocument;
type
TMyPost = function (body, method, url: PChar; OutData : PChar; OutLen : integer): integer; stdcall;
var Handle : THandle;
var MyPost : TMyPost;
var dataString : string;
var returnData : string;
begin
if not (FileExists(ExtractFilePath(Application.ExeName) + 'TestDLL.DLL')) then begin
Application.MessageBox(pchar('Unable to find TestDLL.DLL.'), pchar('Error posting'),MB_ICONERROR + MB_OK);
Exit;
end;
dataString := EmptyStr;
returnData := '';
Handle := LoadLibrary(PChar(ExtractFilePath(Application.ExeName) + 'TestDLL.DLL'));
if Handle <> 0 then begin
try
try
MyPost := GetProcAddress(Handle, 'PostAdminDataViaDll');
if #MyPost <> nil then begin
// NOTE 32767 is not big enough for the returned data! Help!
if MyPost(PChar(body), PChar(method), PChar(url), PChar(returnData), 32767) = 0 then begin
dataString := returnData;
end;
end;
except
end;
finally
FreeLibrary(Handle);
end;
end
else begin
Application.MessageBox(pchar('Unable to find TestDLL.DLL.'), pchar('Error posting'),MB_ICONERROR + MB_OK);
end;
if not sametext(dataString, EmptyStr) then begin
try
Result := CreateOleObject('Microsoft.XMLDOM') as IXMLDOMDocument;
Result.async := False;
Result.loadXML(dataString);
except
end;
end;
end;
I have a DLL that posts XML data to a site. The DLL is built with Delphi 10 in order to use TLS 1.2, which is not available with Delphi XE2.
Why not simply update Indy in XE2 to a newer version that supports TLS 1.2? Then you don't need the DLL at all.
My original DLL convention was basically not correct, as I returned the contents of the returned text data as a PChar. In my readings here and elsewhere, that's a big no-no.
It is not a "big no-no", especially if the response data is dynamic in nature. Returning a pointer to dynamically allocated data is perfectly fine. You would simply have to export an extra function to free the data when the caller is done using it, that's all. The "big no-no" is that this does introduce a potential memory leak, if the caller forgets to call the 2nd function. But that is what try..finally is good for.
That "bad" methodology worked well until I started to get very large amounts of data returned. I tested it, and it failed on anything greater than 132,365 characters.
That is not a lot of memory. Any failure you were getting with it was likely due to you simply misusing the memory.
I restructured my DLL and calling code to pass in a buffer as a PChar to fill in, but I get an error trying to fill the output value!
That is because you are not filling in the memory correctly.
Secondly, since I never know how big the returned data will be, how to I specify how big a buffer to fill from my calling method?
You can't, when using POST. You would have to cache the response data somewhere off to the side, and then expose ways to let the caller query that cache for its size and data afterwards.
My DLL code where I get the error:
My Calling method code:
I see a number of logic mistakes in that code.
But, the most important reason for the Access Violation error is that your EXE is simply not allocating any memory for its returnData variable.
Casting a string to a PChar never produces a nil pointer. If the input string is not empty, a pointer to the string's first Char is returned. Otherwise, a pointer to a static #0 Char is returned instead. This ensures that a string casted to PChar always results in a non-nil, null-terminated, C style character string.
Your EXE is telling the DLL that returnData can hold up to 32767 chars, but in reality it can't hold any chars at all! In the DLL, OutData is not nil, and OutLen is wrong.
Also, StrPLCopy() always null-terminates the output, but the MaxLen parameter does not include the null-terminator, so the caller must allocate room for MaxLen+1 characters. This is stated in the StrPLCopy() documentation.
With all of this said, try something more like this:
library TestDLL;
uses
SysUtils,
Classes,
Windows,
Messages,
Vcl.Dialogs,
IdIOHandlerStack, IdSSLOpenSSL, IdHTTP, IdCompressorZLib;
{$R *.res}
function PostAdminDataViaDll(body, method, url: PChar;
var OutData : PChar): integer; stdcall;
var
HTTPReq : TIdHTTP;
SendStream : TStringStream;
IdSSLIOHandler : TIdSSLIOHandlerSocketOpenSSL;
s : string;
begin
OutData := nil;
try
HTTPReq := TIdHTTP.Create(nil);
try
IdSSLIOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(HTTPReq);
IdSSLIOHandler.SSLOptions.Mode := sslmClient;
IdSSLIOHandler.SSLOptions.SSLVersions := [sslvTLSv1, sslvTLSv1_1, sslvTLSv1_2];
HTTPReq.IOHandler := IdSSLIOHandler;
HTTPReq.Compressor := TIdCompressorZLib.Create(HTTPReq);
HTTPReq.ReadTimeout := 180000;//set read timeout to 3 minutes
HTTPReq.HTTPOptions := [];
HTTPReq.Request.ContentType := 'text/xml';
HTTPReq.Request.Charset := 'UTF-8';
HTTPReq.Request.Accept := 'text/xml';
HTTPReq.Request.CustomHeaders.AddValue('SOAPAction', 'http://tempuri.org/Administration/' + method);
SendStream := TStringStream.Create(Body, TEncoding.UTF8);
try
s := HTTPReq.Post(string(url) + 'admin.asmx', SendStream);
finally
SendStream.Free;
end;
Result := Length(s);
if Result > 0 then begin
GetMem(OutData, (Result + 1) * Sizeof(Char));
Move(PChar(s)^, OutData^, (Result + 1) * Sizeof(Char));
end;
finally
HTTPReq.Free;
end;
except
on E: Exception do begin
ShowMessage(E.Message);
Result := -1;
end;
end;
end;
function FreeDataViaDll(Data : Pointer): integer; stdcall;
begin
try
FreeMem(Data);
Result := 0;
except
on E: Exception do begin
ShowMessage(E.Message);
Result := -1;
end;
end;
end;
exports
PostAdminDataToCenPosViaDll,
FreeDataViaDll;
begin
end.
function PostAdminData(body, method, url : string): IXMLDOMDocument;
type
TMyPost = function (body, method, url: PChar; var OutData : PChar): integer; stdcall;
TMyFree = function (Data Pointer): integer; stdcall;
var
hDll : THandle;
MyPost : TMyPost;
MyFree : TMyFree;
dataString : string;
returnData : PChar;
returnLen : Integer;
begin
hDll := LoadLibrary(PChar(ExtractFilePath(Application.ExeName) + 'TestDLL.DLL'));
if hDll = 0 then begin
Application.MessageBox('Unable to load TestDLL.DLL.', 'Error posting', MB_ICONERROR or MB_OK);
Exit;
end;
try
try
MyPost := GetProcAddress(hDll, 'PostAdminDataViaDll');
MyFree := GetProcAddress(hDll, 'FreeDataViaDll');
if Assigned(MyPost) and Assigned(MyFree) then begin
returnLen := MyPost(PChar(body), PChar(method), PChar(url), returnData);
if returnLen > 0 then begin
try
SetString(dataString, returnData, returnLen);
finally
MyFree(returnData);
end;
end;
end;
finally
FreeLibrary(hDll);
end;
except
end;
if dataString <> '' then begin
try
Result := CreateOleObject('Microsoft.XMLDOM') as IXMLDOMDocument;
Result.async := False;
Result.loadXML(dataString);
except
end;
end;
end;

Bizarre behaviour in simple Delphi code

I am passing TProcessItem structure as a return value from my function. While in GetProcessFromHandle function, the ExeFile property of the structure returned by FindByID, contains the expected executable name, but in the structure returned by GetProcessFromHandle it somehow becomes empty. I added two Messageboxes to demonstrate it. Is anyone able to explain what is happening here?
function GetProcessFromHandle(hWnd: HWND): TProcessItem;
var
ProcessInfo: TProcessInfo;
PID: Cardinal;
begin
Result := nil;
GetWindowThreadProcessId(hWnd, #PID);
ProcessInfo := TProcessInfo.Create(nil);
try
Result := ProcessInfo.RunningProcesses.FindByID(PID);
if Assigned(Result) then ShowMessage(Result.ExeFile); //first message
finally
ProcessInfo.Free;
end;
end;
procedure Test;
var
Process: TProcessItem;
begin
Process := GetProcessFromHandle(FindWindow(nil, 'My App'));
if Assigned(Process) ShowMessage(Process.ExeFile); //second message
end;
The TProcessInfo object owns the TProcessItem object that GetProcessFromHandle() is returning a pointer to. As such, when the TProcessInfo object is freed, so is the TProcessItem object, and thus the pointer being used by Test() is invalid. So you run into undefined behavior. You are lucky your code did not just crash instead.
If you want to return the path to the EXE file, return only that value, eg:
function GetProcessExeFileFromHandle(hWnd: HWND): string;
var
ProcessInfo: TProcessInfo;
ProcessItem: TProcessItem;
PID: DWORD;
begin
Result := '';
GetWindowThreadProcessId(hWnd, #PID);
ProcessInfo := TProcessInfo.Create(nil);
try
ProcessItem := ProcessInfo.RunningProcesses.FindByID(PID);
if Assigned(ProcessItem) then
Result := ProcessItem.ExeFile;
finally
ProcessInfo.Free;
end;
end;
procedure Test;
var
ProcessExeFile: string;
begin
ProcessExeFile := GetProcessExeFileFromHandle(FindWindow(nil, 'My App'));
ShowMessage(ProcessExeFile);
end;

How to use SuperObject to invoke methods that uses an Object as parameter in Delphi?

We can use the SuperObject library to invoke methods of a certain object by its name and giving its parameters as a json string using the SOInvoker method like in this answer
I'd like to know how do I send a created object as a parameter. I tried to send it like
LObjectList := TObjectList.Create;
LSuperRttiCtx := TSuperRttiContext.Create;
LSuperObjectParameter := LObjectList.ToJson(LSuperRttiCtx);
SOInvoke(MyInstantiatedObject, 'MyMethod', LSuperObjectParameter);
but inside MyMethod the LObjectList reference is lost.
What am I doing wrong?
The superobject library can be downloaded here
It will works if you use array of records intead of object list.
If you still want to use object list you will have to write encoders and decoders like this. I have written encoder/decoder for TObjectList, you will have to do the same for your objects and embed the class name somewhere.
ctx.SerialToJson.Add(TypeInfo(TObjectList), ObjectListToJSON);
ctx.SerialFromJson.Add(TypeInfo(TObjectList), JSONToObjectList);
function ObjectListToJSON(ctx: TSuperRttiContext; var value: TValue;
const index: ISuperObject): ISuperObject;
var
list: TObjectList;
i: Integer;
begin
list := TObjectList(value.AsObject);
if list <> nil then
begin
Result := TSuperObject.Create(stArray);
for i := 0 to list.Count - 1 do
Result.AsArray.Add(encodeyourobject(list[i]));
end else
Result := nil;
end;
function JSONToObjectList(ctx: TSuperRttiContext; const obj: ISuperObject; var Value: TValue): Boolean;
var
list: TObjectList;
i: Integer;
begin
list := nil;
case ObjectGetType(obj) of
stNull:
begin
Value := nil;
Result := True;
end;
stArray:
begin
list := TObjectList.Create;
for i := 0 to obj.AsArray.Length - 1 do
list.Add(decodeyourobject(obj.AsArray[i]));
Value := list;
Result := True;
end;
else
result := False;
end;
end;

What is the correct usage of GetLastError and FormatMessage in Delphi?

I have a problem using a third-party component in Delphi 2006 (also Delphi 7), in which I get an "Unspecified Error" when executing a function call to that component. Do you have example code that utilises GetLastError and FormatMessage in Delphi, that would allow me to access more information about the error ? TIA :)
There is an integrated helper function in Delphi: SysErrorMessage. It's essentially a wrapper to FormatMessage, but much simpler to use in your case. Just provide the error code you need a textual description for.
For example you can use this to display the last error:
ShowMessage(SysErrorMessage(GetLastError))
If you want to raise an exception with this message, it's even simpler:
RaiseLastOSError;
Important: Make sure that there is no additional API call between the failing function and your call of GetLastError, otherwise the last error will be reset.
While DR is correct, there is a problem with this approach: It does not allow you to specify the context in which the error occurred. Ever seen the error "An API function failed." whithout being any wiser which function it was and where it happended?
That's why I wrote the RaiseLastOsErrorEx and Win32CheckEx functions:
procedure RaiseLastOsErrorEx(const _Format: string);
begin
RaiseLastOsErrorEx(GetLastError, _Format);
end;
procedure RaiseLastOsErrorEx(_ErrorCode: integer; _Format: string); overload;
var
Error: EOSError;
begin
if _ErrorCode <> ERROR_SUCCESS then
Error := EOSError.CreateFmt(_Format, [_ErrorCode, SysErrorMessage(_ErrorCode)])
else
Error := EOsError.CreateFmt(_Format, [_ErrorCode, _('unknown OS error')]);
Error.ErrorCode := _ErrorCode;
raise Error;
end;
function GetLastOsError(out _Error: string; const _Format: string = ''): DWORD;
begin
Result := GetLastOsError(GetLastError, _Error, _Format);
end;
function GetLastOsError(_ErrCode: integer; out _Error: string; const _Format: string = ''): DWORD;
var
s: string;
begin
Result := _ErrCode;
if Result <> ERROR_SUCCESS then
s := SysErrorMessage(Result)
else
s := _('unknown OS error');
if _Format <> '' then
try
_Error := Format(_Format, [Result, s])
except
_Error := s;
end else
_Error := s;
end;
function Win32CheckEx(_RetVal: BOOL; out _ErrorCode: DWORD; out _Error: string;
const _Format: string = ''): BOOL;
begin
Result := _RetVal;
if not Result then
_ErrorCode := GetLastOsError(_Error, _Format);
end;
(They are part of unit u_dzMiscUtils of my dzLib library available here:
https://osdn.net/projects/dzlib-tools/svn/view/dzlib/trunk/src/u_dzMiscUtils.pas?view=markup&root=dzlib-tools#l313

Resources