Re: Testing Delphi DLL crashes VB6 IDE
- From: Rob Kennedy <me3@xxxxxxxxxxx>
- Date: Wed, 15 Oct 2008 10:35:14 -0500
Bruce M. Axtens wrote:
Rob Kennedy wrote:
Although the type in foo is now accurate, there's still the problem that s goes out of scope before the function returns, and thus the pointer is not valid by the time it arrives at the caller.
But since the address that s held is probably still within your process's address space, and COM probably hasn't re-allocated it to anything else, it probably still contains the same characters it did a moment ago when it was still a valid pointer, so the caller will blithely read it and get what you were expecting, so it appears to work. But a program that accidentally works most of the time is still broken.
Is this getting any closer?
...
uses Windows, ActiveX;
...
procedure DMesg( sText : WideString; sHead : WideString);
begin
Windows.MessageBoxW(0, PWideChar(sText), PWideChar(sHead), 0);
end;
Looks good. One last suggestion on this one is to declare the parameters as const. That way, the function won't make local copies of the parameters. Remember that copying a WideString involves allocating an entire new buffer for the characters. Your function doesn't really need that, and adding "const" tells the compiler not to add code to make those copies.
function foo() : PWideChar;
var s : WideString;
begin
s := 'My dog''s got fleas';
result := ActiveX.SysAllocString(PWideChar(s));
end;
I think my suggestion was to change _all_ the PWideChar types to WideString. That would include the return type of foo. Then the implementation is very simple:
Result := 'My dog''s got fleas';
--
Rob
.
- Follow-Ups:
- Re: Testing Delphi DLL crashes VB6 IDE
- From: Bruce M. Axtens
- Re: Testing Delphi DLL crashes VB6 IDE
- References:
- Testing Delphi DLL crashes VB6 IDE
- From: Bruce M. Axtens
- Re: Testing Delphi DLL crashes VB6 IDE
- From: Rob Kennedy
- Re: Testing Delphi DLL crashes VB6 IDE
- From: Bruce M. Axtens
- Re: Testing Delphi DLL crashes VB6 IDE
- From: Rob Kennedy
- Re: Testing Delphi DLL crashes VB6 IDE
- From: Bruce M. Axtens
- Testing Delphi DLL crashes VB6 IDE
- Prev by Date: Re: Testing Delphi DLL crashes VB6 IDE
- Next by Date: Re: Testing Delphi DLL crashes VB6 IDE
- Previous by thread: Re: Testing Delphi DLL crashes VB6 IDE
- Next by thread: Re: Testing Delphi DLL crashes VB6 IDE
- Index(es):
Relevant Pages
|