Re: Testing Delphi DLL crashes VB6 IDE



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
.



Relevant Pages

  • Re: Is this legal ?
    ... >> to be followed again if the compiler can't guarantee that nothing that ... >address of an object into a pointer to const type version, ... to change it (by casting away const from a pointer to it, ...
    (comp.lang.c)
  • Re: Why is the kfree() argument const?
    ... is there any reason why kfreetakes a const pointer just to degrade it ... In the particular case of kfree(), the pointer argument *should* be const, ... If a struct has a constant pointer to it, ...
    (Linux-Kernel)
  • Re: modifying const qualified object indirectly
    ... through a const struct pointer. ... const-ness) are correct generally depend on the qualification of the definition ... void flip ...
    (comp.lang.c)
  • Re: How to convert Infix notation to postfix notation
    ... correctness and maintainability are what's needed, ... I did point out that const really doesn't accomplish much. ... references to the pointer declared const, ...
    (comp.lang.c)
  • Re: Why is the kfree() argument const?
    ... int main(int argc, char **argv) ... proper application of const pointers: ... And accepting a pointer to a const as an argument does _only_ say: ...
    (Linux-Kernel)