1.19.1 Calling Third Party DLL Functions


Origin C can make calls to functions ( C linkage only ) in external DLLs created by C, C++, C++(.Net), C# or Fortran compilers. To do this, you need to provide the prototype of a function in a header file and tell Origin C which DLL file contains the function body. Assume the functions are declared in a header file named myFunc.h. You should include this file in your Origin C file where you want to call those functions, like:

#include <myFunc.h> //in the \OriginC\System folder
#include "myFunc.h" //in the same folder as your Origin C code
#include "C:\myFile.h" //in specified path

Loading DLL

Then you should tell Origin C where to link the function body, and you must include the following Origin C pragma directive in the header file myFunc.h, just before your external DLL function declarations. Assume your DLL file is UserFunc.dll:

#pragma dll(UserFunc) //in the Origin exe folder
#pragma dll(C:\UserFunc) //in specified path
#pragma dll(UserFunc, header) //in the same folder as this .h file
#pragma dll(UserFunc, system) //in the Windows system folder

The Origin C compiler supports three calling conventions: __cdecl(default), __stdcall and __fastcall. These calling conventions determine the order in which arguments are passed to the stack as well as whether the calling function or the called external function cleans the arguments from the stack.

Notes: you don't need to include the .dll extension in the file name. And all function declarations after the pragma directive will be considered external and from the specified DLL. This assumption is made until a second #pragma dll(filename) directive appears, or the end of the file is reached.

Version Control

To make sure the external dll works correctly, the 32-bit dll is for 32-bit version of Origin, and the same for the 64-bit version. #ifdef _OWIN64 is used to detect which version (32-bit or 64-bit) of current Origin is, so to determine which version of dll to be loaded. For example,

#ifdef	_OWIN64
#pragma dll(UserFunc_64, header)
#pragma dll(UserFunc, header)
#endif	//_OWIN64


A good and complete example of how to access an external DLL is Accessing SQLite Database. There are other Origin sample projects demonstrating how to call a function from a C dll, a Matlab dll or a Fortran dll in Origin C. These examples can be found in this zip file, under the \Programming Guide\Calling Fortran, \Programming Guide\Calling MATLAB DLL and \Programming Guide\Calling C DLL subfolders.

This section covers the following topics: