Okay, just when you think you've gotten over the hurdle in a mix-language programming project something always comes out of left field to bring you to a grinding halt.
I have one project involving calling DLLs from Excel that I've been using for several years now. The Fortran was originally coded using Compaq Visual Fortran. I have strings that I pass back and forth between Exce and Fortran using the "CVF" hidden string length convention; the hidden stringth length arguments in the call to Fortran immediately follows the string arguments in the calling statement. No problem.
When I migrated to Intel Visual Fortran, the convention changed to putting all the hidden string arguments "After All Arguments" in the calling statement. Instead of making a lot of code changes at the time I discovered that I could tell IVF to use the CVF calling convention. This has worked just fine ever since.
I am now designing another Excel/Fortran DLL application modeled closely on the on above. The difference is, I need to make a call to a C routine from the Fortran DLL (yes, this is the same project that is a subject of my previous threads; I have been making progress). I've discovered in working the C/Fortran issues that C (in Visual Studio 2005 or Visual Studio itself) apparently needs to see this hidden string length using the IVF convention in the calls between C and Fortran; i.e. with all the hidden length arguments "After All Arguments" in the calling statement. I don't think I can change the calling convention on the C side.
No problem, I just went into the Fortran andput all the hidden length arguments in my call statements to the"end of the line" sort of speak and also changed "Project/Confiuration Properties/Fortran/External Procedures/Calling Convention" setting to "Default". No problem. Remember, though this was all done on the C/Fortran side; debugging from the Fortran side by having the debuggerin Visual Studio start up Excel etc. Ialso wentinto Excel and changed my declaration and call statements to reflect this, although as I point out below, teh first routine I tried calling from Excel only has one string to pass.
Now the problem. When I call any Fortran procedure that involves passing strings in the dll I get a "Run-time error '49': Bad DLL calling convention".
First of all: The first call I tried from Excel involves only ONE string. I simply cannot get the actual argument and hidden length argument in the wrong order when there is only one string to pass.
Second of all: I have discovered that the problem appears to be happening on exiting of theFortran routine, not when it's entered.
How do I know this? First of all, Ihave no problems whatsoever when I debug from the Fortran side; Excelcomes up I, start the Macro and I go right intoand past the call statement on the Fortran side and all the values of the arguments in the Fortransubroutinestatement look good.
Second: I've discovered that if I have a run-time erroron the Fortran side and run directly from Excel I get a severe Fortran error and Excel crasheswithout the "Run-time 49" error, implying that I'm getting past the Subroutine statement and into the Fortran code. So I then purposelyput a couple of lines of code right before theRETURN and END SUBROUTINE lines in theFortran code to give a run-time error: i.e. I set an integer variable to say 199 and then set another variable to somearray("integer variable=199") when somearray is only declared to have, say, 150 elements. When I run from Excel I get a severe Fortran message that tells me I can't have an array index of "199" when the array has been declared to have only 150 elements.
I comment the above run-time error code out and when I run from Excel I get the "Run-time 49" error. I appear to be gettting this error when exiting the Fortran and code on attempting to return to Excel/VBA, not when the Fortran routine is initially called by Excel.
What's up with this??
One possible solution: Under "Project/Configuration Properties/Fortran/External Procedures/Calling Convention" setting to "Default" there is a note that says: "Calling Convention Selects the default calling convention for an application (can be overridden by INTERFACE)...."
I could set the Calling Convention to CVF to satisfy Excel and I have an INTERFACE block inside a Module in my Fortran routine that interacts with the C code (thanks to IanH) but I don't have the faintest idea (and I've searched) what code to put in my interface block for "overriding" the default calling convention to satisfy the C code.
Below is the thread that contains the Module/Interface block example given to me by IanH which I'm using: search page for "Simple version".