Link to home
Start Free TrialLog in
Avatar of BoofusMcGoofus
BoofusMcGoofus

asked on

Passing a string from a Delphi DLL to a VBA application

All I want to do is call a function with no parameters which returns a string I can use in VBA.

I start with code something like the following:


function policyMBox() : widestring; stdcall;
var
  s : string;
begin
  s:=InputBox('Policy number', 'Input policy number', '');
  if not isValidPolicyNumber(s) then
  begin
    ShowMessage('Invalid policy number entered');
    s:='ERROR';
  end;
  policyMBox:= widestring(s);
end;

The VBA declaration looks like:

Private Declare Function policyMBox Lib "boof.dll" () As String


If I call this from an Excel macro, I get as far as displaying the InputBox (and the Message, if appropriate), but it crashes before it returns.

I've tried making the return type a shortstring and a widestring, I've tried calling it from VB with both fixed and variable length strings.  I'm at a loss.

If someone can explain to me how to do this (and even better, why it works like that) I'll be very grateful.
Avatar of loop_until
loop_until

All I know is:

"Important note about DLL memory management: ShareMem must be the first unit in your library's USES clause AND your project's (select Project-View Source) USES clause if your DLL exports any procedures or functions that pass strings as parameters or function results. This applies to all strings passed to and from your DLL--even those that are nested in records and classes. ShareMem is the interface unit to the BORLNDMM.DLL shared memory manager, which must be deployed along with your DLL. To avoid using LNDMM.DLL, pass string information using PChar or ShortString parameters.".

So I'd say you should use PChar to pass your characters strings from the DLL to your app to avoid using the borlndmm.dll library...

I've found this:

  http://www.shagrouni.com/english/software/textdb.html

that might be of interest to you.

Hope it helps. Tell me if you need more help. I'll try to find more in that case. Good luck and have a nice day! ;-)
Hi BoofusMcGoofus!

Try PChar instead of String.
AFAIK VBA provides variables as references (pointers). Delphi provides the value of the var.

Regards,
Markus
ASKER CERTIFIED SOLUTION
Avatar of Rotbarthe
Rotbarthe

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BoofusMcGoofus

ASKER

That's exactly what I needed -- I'm not sure about the memory issues, either, but it shouldn't be called too often, so I've decided to take the Microsoft approach and consider this a memory problem the user can live with.