Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi everyone,

I call successfully a dll function written in c++ from a macro created in EXCEL VBA.

the function is called with a Declare statement:

Private Declare PtrSafe Function FourierSerieCoeffs Lib "D:\OutBin\ISLib.dll" (ByVal T As Double, ByVal n As Long, ByVal nEq As Long, ByRef x As Double, ByRef fx As Double, ByRef outC As Double) As Long


After the first execution of the macro it is impossible for me to rebuild the dll because it seems that Excel is still using it.

The only way to rebuild the dll project with the excel file opened is to add at the end of the macro these lines:

hMod = GetModuleHandle("ISLib.dll")
Do Until FreeLibrary(hMod) = 0
   
Loop


with these lines i can compile the c++ project but if I execute again the macro excel crashes.

Any suggestion to release the dll without make excel crash in the next executions?

Thanks

What I have tried:

I tried calling the library again with LoadLibrary at the beginning of the macro but it doesn't work.
Posted
Updated 23-Feb-17 2:41am

I'm not sure if this helps and it's is along time ago since I have used VBA but you might give it a try.

When you don't close Excel and restart the macro, it is not necessarily compiled completely again (the generated byte code is still in memory and executed).

To check this, edit the macro source or choose "Compile" in the Debug menu of the VBA editor after updating the DLL to ensure that it is compiled.

With old Office versions there was a "Compile on Demand" option which may also ensure re-compilation when turned off.
 
Share this answer
 
Comments
Optimistic76 24-Feb-17 3:05am    
well, your suggestion help me to make me look deeper in excel options!
Finally I found that if I turn on the Design Mode everything works fine!!!
so thank you!
Jochen Arndt 24-Feb-17 3:24am    
Fine that it is solved and thank you for your feedback.

Just out of interest:
Can you also omit freeing the library when in design mode?

I guess that when in design mode the code is always re-compiled (and libraries might be unloaded).
Optimistic76 28-Feb-17 9:11am    
sorry for the delay,
there is no need to free the library.
After the macro is executed the Design mode unsets automatically and i need to set it again in order to compile the c++ dll
Jochen Arndt 28-Feb-17 9:18am    
No problem with the delay.

It is just helpful for others visiting this thread.

Because it is used only during development, activating the design mode after updating the DLL should be acceptable.
 
Share this answer
 
Comments
Optimistic76 23-Feb-17 4:42am    
Believe me Graeme_Grant, i tried the solutions that i found on google but no one seems to work!
Graeme_Grant 23-Feb-17 4:48am    
I had a quick look in the links proved in the search used above and they look good.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900