-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
API Toolkit with VBA/Excel #43
Comments
So Obviously, need to compile for 32bit which I fixed. However, curious on thoughts on to handle the EN_Project when interfacing from Excel. Should I create a function that somehow takes the Project location pointer and converts to a variable type that excel can handle? |
To clarify. Declarations as below. Long seems to allow a project to be created (and returns value). When that value is placed in any other function, it crashes Excel. Any idea why it won't take the Long. My (very basic) understanding is that it wants a project pointer which I thought was returned by See below, it crashes after creating the project.
|
Try using |
I tried as Any and still crashed for
It can only be defined To add; The first thing I tried was
|
I think I found what the problems are in getting the DLL to work with VBA (there's more than one):
Next, when you build the DLL you have to include a module definition file (
Then your VBA declarations would look as follows:
And finally, you would use the following VBA code to create a
and pass |
@LRossman this is great and way above my knowledge at this time so thanks for investigating. I'll make the updates ASAP and report back with success/failure. I'll also try to soak this in. Glad to be helpful in this endeavor and will continue to test things out. |
@LRossman don't know why I haven'e added these functions for the 2.x branch. Will check it out. |
@LRossman I've finally had a some time to focus on this. However, I'm afraid I'm a bit over my head at the moment with the module definition file and how to integrate it into the compiled dll. I've created the epanet3.def and placed it in the /src folder (wasn't 100% sure where this should go). I've researched tirelessly to better understand and am still not where I need to be to properly create the DLL with the .def How do I :
Is this an edit in the CmakeList.txt or is this an option I need to include when using the developr command line to create the DLL and EXE ( as exampled in the readme):
Apologies for the most likely simple question. |
@sandcastler I think you have to modify the CMakeLists.txt file as follows:
|
@LRossman Thanks. Compiled and the exe works. However, still fails for EN_loadProject.
Is there a disconnect between the returned long value in VBA from |
I've attached a zip file containing the VBA code I used to test running the epanet3.dll in an Excel worksheet. Maybe that will help. Full disclosure: the DLL I used was built from my current local version of the code which contains many changes from the version posted on GitHub. I don't think they affect how the API functions interface with VBA, but I might be wrong. I hope to push my local changes to the repo in the near future. |
Thanks. I've looked at your example and am doing verbatim (other than the full path of the epanet3.dll.) (FYI, didn't have the .frx file so couldn't see the form but used text editor to look at code.) Still fails. I can create a Project but subsequent functions that need Project don't work and give Error 49 (Bad DLL calling convention). I circumvented pointing to a project by testing My 'gut' is there may be some other slight modification you've made that I don't have OR I have my #define headers wrong (stdcall,ect..) Here are my files that I reckon could be wrong. I added the #defines verbatim to the epanet3.h (There weren't any #define prior). Created the epanet3.def file and placed in the project folder (same location as the cmakelist.txt) I have 32-bit excel on a x64 machine. I've compiled these x64-Debug. If you are to push your local changes to the near future, I can be patient and wait; but I'd love to know where I've gone wrong or why this fails. The positive light: I've gained more understanding on all of this 😣 🍷 |
@sandcastler I'm confused as to which platform you're building the DLL for. Up at the top of this thread you said it was 32-bit but your last post mentioned x64 which is 64-bit. The DLL has to be for 32-bit. Sorry you're having such a hard time with this. |
Excel is 32-bit version, machine is 64-bit. However, I just put DLLExport infront of my EN_RunEpanet function and it worked! I stumbled upon this which I believe you are the author for as well. Is that the correct method. After defining the DLLEXPORT, place it before the name of each function so it can correct the name convention which allows VB to play nicely with it?
and then the same in the epanet3.cpp? Edit: Just Tested |
Also, thanks for responding. 90% of me having a hard time is my lack of knowldege/experience with C++. |
Oh my gosh -- it'is the DLLEXPORT! It doesn't appear in epanet3.h in the repo master but I had added it to my own local version that I got to work with VBA:
|
Though I was banging my head, I understood it better than you just telling me ;) I consider this a victory! |
@LRossman Update: I've gone through the various function calls and think this is where I'll have to be patient with your push. So I can load the project and open the files but anything else crashes excel (see example code below with comments of functions crashing). It's weird that the NodeID can be retrieved but the NodeIndex crashes.. Also interesting that you need to define the correct size of your string before getting an ID (both and link). While the nodeID can be retrieved, really nothing else can (getting a count of nodes/links/elements crashes as well as an index). Oddly, even trying to use I noticed the code you provided has different API function call names so I assume you've resolved these in your local copy. I took a look at the functions in the code and couldn't put my finger on why Sub RunEPANET3()
/........./'previous code was called to open files and load project, they all work
NodeID = "01234567"
NodeID2 = "0123456789"
NodeID3 = "012345"
'err = EN_getCount(0, NodeCount, Pr) 'crashes
'EN_getNodeValue 1, 0, Elev, Pr'crashes
'err = EN_getNodeIndex(NodeID, NodeIndex, Pr) 'crashes
err = EN_getNodeId(1, NodeID, Pr) 'works;note the length of NodeID is 8; value=002GVYGK
err = EN_getNodeId(1, NodeID2, Pr) 'value = 002GVYGK�9 since the initial string length is longer (10), the return value adds a ghost as well as keeps the last two alphanumeric values
err = EN_getNodeId(1, NodeID3, Pr) 'concatenates the returned ID value = 002GVY
LinkID = "123456"
LinkID2 = "123"
EN_getLinkId 1, LinkID, Pr 'same issue as getNodeID
EN_getLinkId 1, LinkID2, Pr ' same issue as getNodeID
err = EN_getNodeType(1, nType, Pr) 'crashes
Do
err = EN_runSolver(currentTime, Pr) 'crashes
err = EN_getNodeValue(NodeIndex, EN_HEAD, TankLevel, Pr) 'crashes
Sheets("Interface").cell(1, 1) = TankLevel
err = EN_advanceSolver(dt, Pr)
Loop While dt > 0
EN_writeReport Pr
EN_deleteProject Pr
End Sub |
It seems I can't utilize epanet3.dll with Excel. My goal is to compare 2.0 to 3.0 and would like to 'port' my EXCEL setup for running models to 3.0.
It seems the
EN_CreateProject
function returns a EN_Project object/pointer (not sure). But regardless, i'm declaring functions from the dll and I continue to get 'file not found' error 48. Is the DLL, as written, 'friendly' with Excel or will I need a wrapper?The text was updated successfully, but these errors were encountered: