Author |
Topic |
|
Peter.Hors1
Germany
11 Posts |
Posted - 05/16/2003 : 05:16:30 AM
|
I have again a problem with dlls in origin c:
Someone build for me a dll which includes this header file:
extern "C" _declspec(dllexport) float h_Tp(float T, float p);
I can implement this dll and the linker finds the function "h_Tp". Implementation:
#pragma dll("C:\Daten\OriginC\DllTest_v0.dll") float __cdecl h_Tp(float T, float p); #pragma dll("C:\Daten\OriginC\DllTest_v0.dll")
Problem: Calling this function e.g. with a code like below fails!
void enthalpie_TP(void) { float temp = 330.0; float druck = 100.0e5; float enthalpie = h_Tp(temp,druck); printf("h = %3.2f",enthalpie); }
Error message: (translated from german to english) Runtime error in Origin C function, error calling external function
I tried also with different callTypes (__cdecl, __stdcall, __fastcall) and received the same error.
The dll works with a microsoft visual c++ compiler with this code: #include <iostream.h> #include <stdafx.h>
void Anwendung (void) { typedef float (function)(float,float); static HINSTANCE Dll = 0; if (Dll==0) Dll = LoadLibrary("DllTest.Dll"); static function *h_Tp = 0; if (h_Tp==0) h_Tp = (function*) GetProcAddress(Dll,"h_Tp"); float h = (*h_Tp)(330,100e5); cout << h << endl; }
void main (void) { Anwendung(); Anwendung(); }
Can anybody help me ?
Peter Horstmann Robert Bosch GmbH Germany
Edited by - Peter.Hors1 on 05/16/2003 05:20:53 AM |
|
eparent
118 Posts |
Posted - 05/16/2003 : 11:07:28 AM
|
If you can send me the DLL then I will take a quick look at it here.
EMail it to: tech@originlab.com. In the subject put: Att: EParent
|
|
|
ML
USA
63 Posts |
Posted - 05/19/2003 : 3:52:00 PM
|
Hi Peter,
I took your DLL DllTest_v0.dll, then created a simple console application using MSVC++6.0 like this:
/*---------- Beginning of code -----------------*/ #include "stdafx.h" #include <windows.h> #include <stdio.h>
typedef float (*PFNMYFUN)(float,float);
void enthalpie_TP(void) { float temp = 330.0; float druck = 100.0e5; HMODULE hDLL = LoadLibrary("C:\\C\\Vc32\\bugs\\Horstmann\\DllTest_v0.dll"); if (NULL == hDLL) return;
PFNMYFUN pfn_h_Tp = (PFNMYFUN)GetProcAddress(hDLL, "h_Tp"); if (NULL == pfn_h_Tp) return;
float enthalpie = pfn_h_Tp(temp,druck); printf("h = %3.2f",enthalpie); }
int main(int argc, char* argv[]) { enthalpie_TP(); return 0; } /*---------- End of code -----------------*/
When I run it, I get the following message in the console window: ----------------------------------------------------------- Error: File Refrigerants/Helmholtz.ini can not be opened Go on with return...
Error: File Refrigerants/Helmholtz.ini can not be opened Error: File 'Refrigerants/Helmholtz.ini' can not be opened Go on with return...
Error: File 'Refrigerants/Helmholtz.ini' can not be opened ------------------------------------------------------------
If I hit "Return" three or four times, I get the exception from within your DLL. The exception is at the exact same place as when I call from within Origin C the function enthalpie_TP() which you posted in this thread (note that your function h_Tp uses C-calling convention, which is default, so you do not need to put __cdecl in the declaration for Origin C).
Here is the machine language code with the processor instruction that produces the exception (crash):
10004F36 mov edx,dword ptr [ebx+34h] 10004F39 mov dword ptr [ebx+4],eax 10004F3C or ecx,0FFFFFFFFh 10004F3F xor eax,eax 10004F41 lea edi,[edx+1] Crash is here >>10004F44 repne scas byte ptr [edi] 10004F46 not ecx 10004F48 dec ecx 10004F49 pop edi 10004F4A mov dword ptr [ebx+0Ch],ecx
So, it appears that the problem is in your DLL. When Origin C displays the run-time error, it is because we internally guard the Origin C call to external functions inside a try-catch block, and when an exception is thrown, we just report that the call failed.
If you have source code to your DLL (and have MSVC++ 6.0 available) and can make a debug version of it, then you should be able to catch the crash exactly where it happens inside your DLL (MSVC++ 6.0 allows to hook into an Origin Process using Build>>Start Debug>>Attach to Process..).
ML
|
|
|
Peter.Hors1
Germany
11 Posts |
Posted - 05/21/2003 : 04:50:42 AM
|
Hi ML,
I have now installed Visual C++ 6.0 on my PC and tried out the file you posted. On my PC it works!
Maybe you don't have installed all the files of the DllTest.zip archive in the folder where the Dll_Testv0.dll is in (in your code in "C:\\C\\Vc32\\bugs\\Horstmann\\"). This files are called from the dll. To run your simple test code you need in your folder "C:\\C\\Vc32\\bugs\\Horstmann\\" the files: Refrigerants\Helmholtz.ini Refrigerants\R744Saturation.dat
If you don't have these files you get the error message you have described.
Greatings
Peter
Peter Horstmann Robert Bosch GmbH Germany |
|
|
ML
USA
63 Posts |
Posted - 05/21/2003 : 09:56:03 AM
|
Hi Peter,
We don't have those Refrigerants\*.* files that you mention. However, I surmise what the problem might be and why it works for you with a separate application and not when called from Origin C. This stems from the fact which I mentioned in my previous post: The crash that I got in my separate application was at the same location as the crash (exception) I got when calling it from Origin C. And since I did not have those Refrigerants\*.* files, it is plausible that the failure that you get when calling from Origin C is due to the fact that your DLL cannot find the Refrigerants\*.* files when calling your DLL function from Origin C whereas it can find them (in your case) when calling your DLL function from a separate application.
The question then is: where is your DLL looking for those Refrigerants\*.* files? I cannot answer this question since I don't know how your DLL works. Since Refrigerants\ is a relative path, it must start from some absolute path to which the DLL then tacks on the relative path Refrigerants\. I don't think it is user's folder since your DLL probably knows nothing about it. Maybe it is the path of the DLL (where the DLL is located). Or the path of Origin EXE. Those paths can be discovered from within your DLL, although, perhaps, in a non-straightforward way.
Of course, you could easily find these answers by running the debug version of your DLL and hooking into it as I described in my previous post.
ML
|
|
|
Peter.Hors1
Germany
11 Posts |
Posted - 05/23/2003 : 08:25:49 AM
|
Hi ML,
the dll is now working!
I put the Refrigerants\*.* files at the wrong place.
Finally every solution is simple if you have found it :)
Thank you for your help!
Peter
Peter Horstmann Robert Bosch GmbH Germany |
|
|
Peter.Hors1
Germany
11 Posts |
Posted - 05/26/2003 : 04:26:21 AM
|
I found a strange behaviour of the origin c-compiler / workspace configuration:
I have a C-Code with a included header-file: #include "Stoffdaten_CO2.h" or #include "C:\Program Files\OriginLab\Origin7G\OriginC\Stoffdaten_CO2.h"
In the header file a dll is called: #pragma dll(OriginC\Stoffdaten_CO2) or #pragma dll("C:\Program Files\OriginLab\Origin7G\OriginC\Stoffdaten_CO2\Stoffdaten_CO2_Tegethoff.dll")
The dll needs some files in the folder Refrigerants/*.
Now follows whta i have found "strange": 1. - create new workspace with the c-Code - compiling & linking - Program runs if the Refrigerants folder is in the OriginC folder. - save workspace
2. - close Origin and restart it - comiling & linking - Program runs only if the Refrigerants folder is in the Origin7G folder
3. - taking the C-code out of the workspace, put it in again: everything works like described under 1.
So it seams to me that in case 1 the path which uses the linked dll inside for calling other files is different from case 2. But the dll is in both cases in the same path! Using absulute path in the C and h-files doesn't change anything.
If you are interested in I can send you my files.
Peter
Peter Horstmann Robert Bosch GmbH Germany |
|
|
cpyang
USA
1406 Posts |
Posted - 05/26/2003 : 11:35:07 AM
|
I think your DLL assumes that it is called with the current working directory set to its path. This would be typically the case if the DLL was written for a DOS command prompt based program. Try adding the following lines into your Origin C source file, immediately before you call the the DLL
string strThisFile = __FILE__; // a standard C macro string strPath = GetFilePath(strThisFile); _chdir(strPath);
The above code would set the Current Working path to the same path as your C file, so if your DLL is located in the same path as your C file, then it can assume the correct Current path.
CP
|
|
|
b0211030
Bangladesh
2 Posts |
Posted - 05/18/2021 : 12:46:20 AM
|
Pls use Windows app wizard to create dll That dont need export import function. ( Visual studio 2019)
And you can use dll and auto generated resource.h file. |
|
|
|
Topic |
|
|
|