 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
A.Afonso Guest
|
Posted: Thu Dec 30, 2004 3:10 pm Post subject: DLL function calls in Excel |
|
|
Hi everybody,
I'm trying to connect from Excel to a funcion declared in a DLL (made in
delphi code). I have no problems when using integer or double types. But
using concat strings I'm desperate.
Simplifying the process, I've defined in Delphi a function like this (inside
a MyDll.dll):
function ShowText(Text: PChar): PChar; export; stdcall;
begin
Result := PChar('My text ' + Text);
end;
And I've defined a macro in VBA in Excel like this:
Declare Function VBShowText Lib "MyDll.dll" Alias "ShowText" (ByVal a As
String) As String
If I include in a cell the call: =VBShowText(A1), the cell shows always
rubbish (some chinesse symbols I think...). I've tried a lot of combinations
of PChar, array of Char, string, Concat, StrPCopy, etc. Same or worst
results.
Note: The function works ok from an exe Delphi program. Also works ok if it
is called from an exe VS C# program.
Another Note: If I substitute Result := PChar('My text ' + Text); by Result
:= Text; everything is ok. The problem is when I concat the strings.
Thanks in advance,
Augusto.
|
|
| Back to top |
|
 |
Avatar Zondertau Guest
|
Posted: Thu Dec 30, 2004 3:35 pm Post subject: Re: DLL function calls in Excel |
|
|
| Quote: | I'm trying to connect from Excel to a funcion declared in a DLL (made
in delphi code). I have no problems when using integer or double
types. But using concat strings I'm desperate.
Simplifying the process, I've defined in Delphi a function like this
(inside a MyDll.dll):
function ShowText(Text: PChar): PChar; export; stdcall;
begin
Result := PChar('My text ' + Text);
end;
And I've defined a macro in VBA in Excel like this:
Declare Function VBShowText Lib "MyDll.dll" Alias "ShowText" (ByVal a
As String) As String
If I include in a cell the call: =VBShowText(A1), the cell shows
always rubbish (some chinesse symbols I think...). I've tried a lot
of combinations of PChar, array of Char, string, Concat, StrPCopy,
etc. Same or worst results.
|
Ouch! Looking at this code hurts my eyes!
This is not because of the bug you're referring to. This is probably
because to my knowledge Visual Basic uses Unicode strings instead of
Ansi strings. That would probably be solved by replacing each
string-related type (string, PChar, Char) by it's Unicode equivalent
(WideString, PWideChar, WideChar).
A major problem with your code is, however, the way you're mixing
strings and PChars. The code you're using may result in memory leaks or
random access violations.
Rudy Velhuis' article will explain the trouble you'll run into when
mixing Delphi strings and PChars:
http://rvelthuis.bei.t-online.de/articles/articles-pchars.htm
Unfortunately I don't know a direct approach for solving this bug. In
any case returning a PChar should be avoided. Generally the caller
should allocate a buffer, pass it and it's length to the callee and
then the callee should write it's result to that buffer. I don't know
if Excel is capable of doing this.
|
|
| Back to top |
|
 |
George Birbilis Guest
|
Posted: Thu Dec 30, 2004 5:19 pm Post subject: Re: DLL function calls in Excel |
|
|
try PAnsiChar, not PChar
|
|
| Back to top |
|
 |
Avatar Zondertau Guest
|
Posted: Thu Dec 30, 2004 5:24 pm Post subject: Re: DLL function calls in Excel |
|
|
| Quote: | try PAnsiChar, not PChar
|
PAnsiChar is the same as PChar. It's PWideChar that should be used
instead of PChar.
This doesn't solve the other, IMHO worse, problem though.
|
|
| Back to top |
|
 |
George Birbilis Guest
|
Posted: Thu Dec 30, 2004 6:35 pm Post subject: Re: DLL function calls in Excel |
|
|
| Quote: | If I substitute Result := PChar('My text ' + Text); by Result
:= Text; everything is ok. The problem is when I concat the strings.
|
* try
Result:=PChar('My text' + String(Text));
* else
var s:string;
....
s:=Text;
Result:=PChar('My text'+s);
does either of these help?
-----
George Birbilis (birbilis (AT) kagi (DOT) com)
http://www.kagi.com/birbilis
--------------
|
|
| Back to top |
|
 |
A.Afonso Guest
|
Posted: Fri Dec 31, 2004 10:25 am Post subject: Re: DLL function calls in Excel |
|
|
Many thanks Avatar and George,
I've been trying your suggestions. No success at all. Also I read the Rudy
Velhuis' article and I've been trying more options. Nothing.
I'm considering hardly to rewrite the dll as VBA functions. It is a pitty,
because de dll has many functions involved.
Thanks again,
A.Afonso
|
|
| Back to top |
|
 |
willem van Deursen Guest
|
Posted: Fri Dec 31, 2004 3:30 pm Post subject: Re: DLL function calls in Excel |
|
|
I hope you do know already this article, but otherwise, it might contain
some useful information for you, and it allows you to bybass your VBA
code, maybe, maybe.
http://www.howtodothings.com/showarticle.asp?article=187
Hope this helps
Willem
A.Afonso wrote:
| Quote: | Many thanks Avatar and George,
I've been trying your suggestions. No success at all. Also I read the Rudy
Velhuis' article and I've been trying more options. Nothing.
I'm considering hardly to rewrite the dll as VBA functions. It is a pitty,
because de dll has many functions involved.
Thanks again,
A.Afonso
|
--
Willem van Deursen, The Netherlands
[email]wvandeursen_nospam (AT) nospam_carthago (DOT) nl[/email]
replace _nospam@nospam_ for @ to get a valid email address
www.carthago.nl
|
|
| Back to top |
|
 |
A Pham Guest
|
Posted: Sat Jan 01, 2005 7:45 pm Post subject: Re: DLL function calls in Excel |
|
|
This code will never work. It is lucky that you get no access violation or
garbage.
Delphi string is a automatically freed when out of scope or no more
reference count.
Need to change to codes below and remember to free when receive or allocate
enough space and pass it to this function
function ShowText(Text: PChar): PChar; export; stdcall;
var
S: string;
begin
S := 'My text ' + Text;
GetMem(Result, Length(S) + 1);
Move(Pointer(S)^, Result^, Length(S));
PChar(Integer(Result) + Length(S))^ := #0;
end;
Cheers
A Pham
"A.Afonso" <aafonso (AT) ids-sa (DOT) es> wrote
| Quote: | Hi everybody,
I'm trying to connect from Excel to a funcion declared in a DLL (made in
delphi code). I have no problems when using integer or double types. But
using concat strings I'm desperate.
Simplifying the process, I've defined in Delphi a function like this
(inside
a MyDll.dll):
function ShowText(Text: PChar): PChar; export; stdcall;
begin
Result := PChar('My text ' + Text);
end;
And I've defined a macro in VBA in Excel like this:
Declare Function VBShowText Lib "MyDll.dll" Alias "ShowText" (ByVal a As
String) As String
If I include in a cell the call: =VBShowText(A1), the cell shows always
rubbish (some chinesse symbols I think...). I've tried a lot of
combinations
of PChar, array of Char, string, Concat, StrPCopy, etc. Same or worst
results.
Note: The function works ok from an exe Delphi program. Also works ok if
it
is called from an exe VS C# program.
Another Note: If I substitute Result := PChar('My text ' + Text); by
Result
:= Text; everything is ok. The problem is when I concat the strings.
Thanks in advance,
Augusto.
|
|
|
| Back to top |
|
 |
George Birbilis Guest
|
Posted: Mon Jan 10, 2005 8:40 pm Post subject: Re: DLL function calls in Excel |
|
|
btw, autogenerated Delphi DLLs (from "File/New" menu) have a comment that
says about "Borland Memory Manager DLL". See if that comment gives any help
in your case
-----
George Birbilis (birbilis (AT) kagi (DOT) com)
http://www.kagi.com/birbilis
--------------
| Quote: | This code will never work. It is lucky that you get no access violation or
garbage.
Delphi string is a automatically freed when out of scope or no more
reference count.
Need to change to codes below and remember to free when receive or
allocate
enough space and pass it to this function
function ShowText(Text: PChar): PChar; export; stdcall;
var
S: string;
begin
S := 'My text ' + Text;
GetMem(Result, Length(S) + 1);
Move(Pointer(S)^, Result^, Length(S));
PChar(Integer(Result) + Length(S))^ := #0;
end;
Cheers
A Pham
"A.Afonso" <aafonso (AT) ids-sa (DOT) es> wrote in message
news:41d41967 (AT) newsgroups (DOT) borland.com...
Hi everybody,
I'm trying to connect from Excel to a funcion declared in a DLL (made in
delphi code). I have no problems when using integer or double types. But
using concat strings I'm desperate.
Simplifying the process, I've defined in Delphi a function like this
(inside
a MyDll.dll):
function ShowText(Text: PChar): PChar; export; stdcall;
begin
Result := PChar('My text ' + Text);
end;
And I've defined a macro in VBA in Excel like this:
Declare Function VBShowText Lib "MyDll.dll" Alias "ShowText" (ByVal a As
String) As String
If I include in a cell the call: =VBShowText(A1), the cell shows always
rubbish (some chinesse symbols I think...). I've tried a lot of
combinations
of PChar, array of Char, string, Concat, StrPCopy, etc. Same or worst
results.
Note: The function works ok from an exe Delphi program. Also works ok if
it
is called from an exe VS C# program.
Another Note: If I substitute Result := PChar('My text ' + Text); by
Result
:= Text; everything is ok. The problem is when I concat the strings.
Thanks in advance,
Augusto.
|
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|