Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Calling C function in DLL from Excel

6 views
Skip to first unread message

Nick Brook

unread,
Mar 4, 1999, 3:00:00 AM3/4/99
to
Does anyone know how to call a C (or C++) function in a DLL from within
Excel? I can get the CALL("kernal32", "GetClockTick","J") example to
work, but not when I use my own DLL (created using the simple Win32 DLL
AppWizard)?

Thanks in advance,
Nick Brook


Lori Korbas

unread,
Mar 4, 1999, 3:00:00 AM3/4/99
to


Nick,

I've just been through this. Did you use the REGISTER.ID function as
well as the CALL? You need to register the C/C++ function first. The
code should look something like this

macro1 REGISTER.ID("xxx.dll","cfunc1","PPPP")
RETURN(CALL(macro1(parm1,parm2,parm3,parm4)

Save the above Excel macro code as an .xla (add-in).

Put the .dll in your C: "system" folder.
Put the .xla in your XLSTART folder.

Assuming your parameters are correctly set up, it should work.

Lori

Nick Brook

unread,
Mar 4, 1999, 3:00:00 AM3/4/99
to
Thanks Lori. I'd forgot that you have to put an extern "C" in front of the
declaration, as C++ mangles function names. It works now.

Cheers,
Nick

=============================

Diane Gillis

unread,
Mar 4, 1999, 3:00:00 AM3/4/99
to
In the general declarations you should declare the dll as follows:

Public Declare Function nameoffunction Lib "nameofdll.DLL" (ByVal parameter1
as x, ByRef parameter2 as y, etc.) As Long (where long is the return value
of the function)
or
Public Declare Sub nameoffunction Lib "nameofdll.DLL" (ByVal parameter1 as
x, ByRef parameter2 as y, etc.) when no value is returned.

In the paramter list you need to define if the parameter is ByVal or ByRef
and the variable type. Be careful with strings, they are usually ByVal.
Also, if you are expecting a short in C then it must be defined as an int in
VB, an int in C as a long in VB, and more. You can find the translations in
the Help file.
The DLL you are calling must be in your path or must be in the current
folder/directory. The current folder/directory is the directory that you
see when you use the Open or Save buttons. If you change this directory in
between calls to the dll and the dll is not in you path it won't find it.

Hope this helps.
Diane

Nick Brook wrote in message <36DE903D...@bt.com>...

Diane Gillis

unread,
Mar 4, 1999, 3:00:00 AM3/4/99
to
0 new messages