A method exists for interfacing with C++ DLLs. The syntax of declaring a C++ module is shown below.
module module_name { ... }
Inside the module declaration the signatures of the C++ functions are declared. An example of this is shown below.
A program that interfaces with a C++ dll is shown below.
// testC.txt - Testing modules. space TestC { module TestC { test() test_double(d) test_string(s) test_pointer(p) } Test() { m = new TestC() m.test() r = m.test_double(20.5) cout << "return = " << r << "\n" r = m.test_string("hello world") p = 8 p.allocate() p[0] = 100 r = m.test_pointer(p) cout << "return = " << r << "\n" cout << "updated integer = " << p[0] << "\n" p.close() } }
The output of the program is shown below.
In test In test_double, parameter = 20.5 return = 100 In test_string, parameter = hello world In test_pointer, parameter = 100 updating it to: 1000 return = 100 updated integer = 1000
The C++ source code of the module TestC is shown below.
#include<iostream> extern "C" { __declspec(dllexport) long long test() { std::cout << "In test\n"; return 20; } __declspec(dllexport) long long test_int(long long l) { std::cout << "In test_int, parameter = " << l << "\n"; return 100; } __declspec(dllexport) long long test_double(double* d) { std::cout << "In test_double, parameter = " << *d << "\n"; return 100; } __declspec(dllexport) long long test_string(const wchar_t* s) { std::wcout << L"In test_string, parameter = " << s << L"\n"; return 100; } __declspec(dllexport) long long test_pointer(int* i) { std::cout << "In test_pointer, parameter = " << *i << " updating it to: 1000\n"; *i = 1000; return 100; } }
Note that integers are passed as value types in the form of "long long". When a decimal number is passed, it is represented by the datatype "double*". You may update the double to which the parameter points to. Strings are passed as "const wchar_t*" and they should not be updated. To pass an arbitary pointer to data, you allocate the data on the heap using the method of the integer class: allocate(). Any type of data may be allocated and passed in this way. In the above example, a 64 bit integer is allocated and passed to the C++ function test_pointer. The integer is updated by the C++ function and the updated integer is printed by the Generic function.
This is a limited interface and the datatypes used in the C++ module must be 64 bit. A maximum of 15 parameters are supported.