Importing external functions from a Dynamic Link Library(DLL) that has no exposed methods can easily be accomplished with the right know-how. Any C function, in C++, declared “extern C” can be called directly from CM by using an "import dll" definition. Thus, the basic solution is to write an "extern C" wrapper DLL for the desired DLL.
In the following example a function "add" will be imported into CM from an external DLL named Maths. If the Type Library doesn't exist you can create it by running the following command in the command prompt:
regasm Maths.dll /tlb:Maths.tlb
The header for the wrapper class.
MathsWrapper.h
#import "Maths.tlb" using namespace Maths extern "C" __declspec(dllexport) int add(int addend1, int addend2);
Once the type library is imported the name of the CoClass that holds the function is needed. Visual Studio will be able to expand upon the available typedefs imported from the Maths type library. (Use Maths::)
For this example, the CoClass will be called MathFunc.
MathsWrapper.cpp
#include "MathsWrapper.h" int add(int addend1, int addend2) { int sum; //run Maths.dll add function// CoInitialize(NULL); //Initialize COM MathFuncPointer m; //CoClass Pointer HRESULT hres = m.CoCreateInstance(__uuidof(MathFunc)); //Instantiate if (SUCCEEDED (hres)) { hres = m->addFunc(addend1, addened2); //call desired function a = 0; } CoUninitialize(); //Uninitialize COM return a; }
Finally, the new MathsWrapper.dll can be directly imported and called in CM.
import dll "MathsWrapper.dll" { public int add(int a1, int a2); } public class MathsClass { public constructor(); public int addUp(int an1, int an2) { return add(an1, an2); } }
Instructions on how to make a basic DLL in Visual Studio can be found here.
Comments
0 comments
Please sign in to leave a comment.