All New Wilmott Jobs Board                     (g)

Forum Navigation:

magazine

FORUMS > Technical Forum < refresh >
Topic Title: Use functions exposed in .xll file in C++ application
Created On Mon Sep 10, 07 12:09 PM
Topic View:

View thread in raw text format


Jimmy123
Junior Member

Posts: 5
Joined: Sep 2007

Mon Sep 10, 07 12:09 PM
User is offline

Hi,

I am working on Excel Addin. My basic task is to create Excel Addins in C++. these Excel Addins have extn. .xll.
I am given a .xll file which has many functions exposed. I have to create another C++ .xll where I can use the functions exposed in the already developed .xll file. Is it possible to do that.

I am told that .xll is a C++ dll with the extension renamed. Is it possible to convert this xll back to dll and then use the exposed function as we generally do in C++ dlls.

Please tell me some way through which I can use those functionality already given in the existing .xll

Any help will be highly appreciated.

Thanks,
Jimmy
 
Reply
   
Quote
   
Top
   
Bottom
     



bretth
Member

Posts: 26
Joined: Oct 2003

Mon Sep 10, 07 12:41 PM
User is offline View users profile

Hi Jimmy,

An XLL is basically a DLL with some standard functions exported with a "C" calling convention. You can load it as a DLL using LoadLibrary() and then GetProcAddress() to get the function pointers dynamically. Alternatively you could create an import library (.LIB file). Make sure you get the function calling convention and prototype definitions right.

Basically it depends on how the XLL is written, as to how easy it's going to be to use in a stand-alone application. You may need to call the "xlAutoOpen" function yourself after loading the XLL in order to initialize it, which means you might need to provide your own fake "Excel4" function. Also the XLL may pass XLOPER structures which you may need to duplicate.

Cheers, Brett
 
Reply
   
Quote
   
Top
   
Bottom
     



eredhuin
Senior Member

Posts: 262
Joined: Jul 2002

Mon Sep 10, 07 01:20 PM
User is offline

Everything you need is nicely summarized in the Excel book by Dalton. You can buy it on this site. I recommend it.
 
Reply
   
Quote
   
Top
   
Bottom
     



Jimmy123
Junior Member

Posts: 5
Joined: Sep 2007

Tue Sep 11, 07 11:36 AM
User is offline

Hi Brett,

Thanks for your help. I tried loading the xll file through LoadLibrary() function. It does not load using this function. Since xll is none other but a dll, I changed the extension of the file from .xll to .dll, then also loading is not done through this function. It throws an exception saying "Application has failed to start because XL Call32.dll was not found."
You have talked about using "xlAutoOpen" function, but the problem is I am not able to load xll file. Using its function would be second problem.

Is there something I need to do with the xll file. I mean may be some refinement or some processing to convert it back to proper dll. Please tell me some way to load this xll and to use the functions exposed by it. This is really urgent as whole of my work has stuck up because of that only.

I'll be really thankful to you for the help.

Thanks,
Jimmy
 
Reply
   
Quote
   
Top
   
Bottom
     



bretth
Member

Posts: 26
Joined: Oct 2003

Tue Sep 11, 07 12:01 PM
User is offline View users profile

The problem is that the XLL has a dependency on the XLCall32.DLL which is in the Excel directory. The XLCall32.DLL implements the Excel4 function which is used by the XLL to call back to Excel. When you try and load the DLL, Windows will automatically try and load any dependent DLLs into memory as well. Windows searches for any dependent DLLs in your current working directory, and then through the search path. Since the Excel directory is usually added to the search path when Excel is installed, would I be correct in assuming that you don't have Excel installed on your machine?

Cheers, Brett
 
Reply
   
Quote
   
Top
   
Bottom
     



Jimmy123
Junior Member

Posts: 5
Joined: Sep 2007

Tue Sep 11, 07 01:19 PM
User is offline

I am sorry to trouble you. Actually when I copied that XL Call32.dll to the current path, it didn't give the exception but still I cannot find the handle to the dll. It is still null.
I am using the following statemeny for loading the xll file.

Assuming .xll file is in C drive
HINSTANCE hGetProcIDDLL = LoadLibrary("C:\\ERAPlugin.xll");
When this statement is executed, value of "hGetProcIDDLL" is still null.

How can I proceed when I am not even getting a valid handle value.
What could be the problem.

Thanks$Regards,
Jimmy
 
Reply
   
Quote
   
Top
   
Bottom
     



bretth
Member

Posts: 26
Joined: Oct 2003

Tue Sep 11, 07 02:05 PM
User is offline View users profile

Call the GetLastError() function and see what it tells you using FormatMessage().

Cheers, Brett
 
Reply
   
Quote
   
Top
   
Bottom
     



Jimmy123
Junior Member

Posts: 5
Joined: Sep 2007

Wed Sep 12, 07 06:04 AM
User is offline

I checked with the GetLastError(). It is showing "A dynamic link library (DLL) initialization routine failed".
I am quite new to xll programming. You have told in the previous message that to initialize the DLL we have to specifically call the xlopen function of the Excel.
Please help me out as to how to do that.

Can you tell me some online references which I can take help of for the xll programming?

Thanks,
Jimmy
 
Reply
   
Quote
   
Top
   
Bottom
     



bretth
Member

Posts: 26
Joined: Oct 2003

Wed Sep 12, 07 09:47 AM
User is offline View users profile

I would say that either a dependent module has not been found, or the DLL is returning a failure from it's initialization routine.

You can use the "dumpbin" program in the MS Visual C directory to determine the list of DLLs it is dependent on. Open a command prompt, run the "VCVARS32.BAT" file to set the Visual C environment variables, then type in "DUMPBIN /IMPORTS xllname.xll" to see what the DLL imports.

The other alternative is that when the DLL loads, Windows itself calls a function (I forget what it's called now - DllMain maybe?) to allow the DLL to initialize itself. This happens BEFORE any Excel specific routines are called. If the DLL signals that there was an error in initialization then the DLL won't be loaded. This may happen if the DLL doesn't find any Registry entries that it depends on, for example.

Cheers, Brett
 
Reply
   
Quote
   
Top
   
Bottom
     



RedeR
Member

Posts: 75
Joined: Mar 2002

Thu Sep 13, 07 11:11 AM
User is offline View users profile

You need to create a fake xlcall32.dll, put it in the same directory as your XLL (do not put excel's own xlcall32.dll in the PATH). Here is some code:
---------------------
# include <windows.h>

typedef void* LPXLOPER;

extern "C" void __declspec(dllexport) XLCallVer ( ) {}

extern "C" int __declspec(dllexport) Excel4 (int xlfn, LPXLOPER operRes, int count,... ) { return 0; }

extern "C" int __declspec(dllexport) Excel4v(int xlfn, LPXLOPER operRes, int count, LPXLOPER far opers[]) {return 0;}
-------------
Now suppose I have an XLL called xll-dll.xll with a function called (use "depends.exe" to find out the names of the exported functions) xlAdd that well adds two doubles:
extern "C" __declspec(dllexport) XLOPER * __cdecl xlAdd(XLOPER* pA, XLOPER* pB);

The following code calls it:

------------------------
# include <windows.h>
# include <iostream>

// your own header that defines XLOPERs
# include <parser/xll/xloper.hpp>

// pointer to function taking 2 XLOPERS
typedef XLOPER * (__cdecl *xl2args) (XLOPER* , XLOPER* ) ;

void test(){
/// get the XLL address
HINSTANCE h = LoadLibrary("xll-dll.xll");
if (h != NULL){
xl2args myfunc;
/// get my xll-dll.xll function address
myfunc = (xl2args) GetProcAddress(h, "xlAdd");
if (!myfunc) { // handle the error
FreeLibrary(h); }
else { /// build some XLOPERS, call the remote function
XLOPER a,b, *c;
a.xltype = 1; a.val.num = 1. ;
b.xltype = 1; b.val.num = 2. ;
c = (*myfunc)(&a,&b);
std::cout << " call of xll " << c->val.num << std::endl; }
FreeLibrary(h); }
}

int main()
{test();}
------------------------

My exe actually works (to my own surprise), and output 3 as expected. You must have some knowledge of what your XLL actually expects for parameters. If it allocates some memory, you must check if the
#define xlbitDLLFree 0x4000
is set on your XLOPER c->type, and call back "xlAutoFree".
 
Reply
   
Quote
   
Top
   
Bottom
     



bretth
Member

Posts: 26
Joined: Oct 2003

Thu Sep 13, 07 11:32 AM
User is offline View users profile

Nice work!

I was wondering whether XLCall32.dll actually checked to see if Excel was running. I guess it has to, otherwise the Excel4 calls wouldn't work.

Cheers, Brett
 
Reply
   
Quote
   
Top
   
Bottom
     



gatechfe08
Junior Member

Posts: 11
Joined: Jan 2008

Sat Mar 08, 08 04:34 PM
User is offline

how do u incorporate a customised function in C++.. into MS excel through dll/xll??? i have tried everthng... doesnot work though... please help me out....
tried using sample codes on net... still does not work...
 
Reply
   
Quote
   
Top
   
Bottom
     



daveangel
Senior Member

Posts: 12592
Joined: Oct 2003

Sat Mar 08, 08 08:39 PM
User is offline

either buy the Excel SDK or have a look at XLW

-------------------------
Knowledge comes, but wisdom lingers.
 
Reply
   
Quote
   
Top
   
Bottom
     



eredhuin
Senior Member

Posts: 262
Joined: Jul 2002

Mon Mar 10, 08 02:33 PM
User is offline

Dave I am pretty sure the Excel 2007 SDK is free

Try here

 
Reply
   
Quote
   
Top
   
Bottom
     



daveangel
Senior Member

Posts: 12592
Joined: Oct 2003

Tue Mar 11, 08 10:19 AM
User is offline

Thanks - I didn't know this. It has been many a year since I developed anything in anger.

When I bought the xl sdk about 7 years ago, I had to buy the book as well and it was pricey. Is the book free too ? Or does one not need it any more ?

-------------------------
Knowledge comes, but wisdom lingers.
 
Reply
   
Quote
   
Top
   
Bottom
     

View thread in raw text format
FORUMS > Technical Forum < refresh >

Forum Navigation:

© All material, including contents and design, copyright Wilmott Electronic Media Limited - FuseTalk 4.01 © 1999-2014 FuseTalk Inc. Terms & Conditions