Building a DLL for use in Excel

Suppose I have a function written in C++, say

double GenerateRandomNumber(double seed)

I want to compile it as a DLL and attach it to an Excel spreadsheet, so I can use it like an ordinary VBA function. My questions:

1. How do I change the settings in Visual Studio so that the code gets compiled as a DLL?
2. How do I use the DLL in Excel?
RothbardAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
Check out http://support.microsoft.com/kb/178474 ("How To Build an Add-in (XLL) for Excel Using Visual C++ ")
0
jkrCommented:
BTW, more sample code here:

http://www.codeproject.com/KB/macros/InterpolationAddin.aspx ("Sample Excel Function Add-in in C")
http://www.codeproject.com/KB/macros/easyif.aspx ("Excel Add-in Using Excel C API and COM")
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RothbardAuthor Commented:
Is there any way of doing this without having to buy the book "Microsoft Excel 97 Developer's Kit"?
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

RothbardAuthor Commented:
By the way, I tried to load the XLL file provided on the Code Project website, but it didn't work in my copy of Excel 2003. I get the error "Interp32.dll is not a valid add-in".
0
RothbardAuthor Commented:
Is there a way of calling a DLL from Excel without actually making an Add-In out of it?
0
RothbardAuthor Commented:
I forgot to add that I don't need to use the DLL in an Excel sheet, it's enough to be able to use it in VBA.
0
jkrCommented:
>>Is there a way of calling a DLL from Excel without actually making an Add-In
>>out of it?

I strongly doubt that, even from VBA. Except maybe if you can call COM objects from VBA, them that would be an option.
0
RothbardAuthor Commented:
So if I make it a COM DLL I should be able to call it from VBA? How can I do that?
0
jkrCommented:
WeGood question. I don't even know VBA well enough to know if that is possible at all...
0
Rory ArchibaldCommented:
Set a reference to it in your VBA Project, then you should be able to use its methods and objects etc.
Regards,
Rory
0
RothbardAuthor Commented:
How do I set the reference? I don't have a project by the way, only a module in an Excel spreadsheet.
0
Rory ArchibaldCommented:
Every workbook contains/is a project. You set a reference via the Tools->References option of the menu. You will need to browse to your dll.
Rory
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.